Qmake 项目文件

发布于 2024-10-16 13:11:48 字数 385 浏览 0 评论 0原文

我有一个我制作的类文件(头文件和 cpp),我想在 main.cpp 文件中使用它。我生成了一个 qmake 项目文件(从我的 main.cpp 的当前目录),并添加了标头和 cpp:

HEADERS += $$quote(/home/myusername/projects/src/myclass.h)
SOURCES += $$quote(/home/myusername/projects/src/myclass.cpp)
SOURCES += main.cpp

当我运行 makefile 时,它​​似乎一直有效,直到它到达我的 main.cpp 中我包含的部分头文件,然后它说:致命错误,没有这样的文件或目录

我觉得我犯了一个非常基本的错误,但我似乎无法弄清楚。

I have a class file (header and cpp) that I made, that I want to use in my main.cpp file. I generated a qmake project file (from the current directory of my main.cpp) and added the header and cpp with:

HEADERS += $quote(/home/myusername/projects/src/myclass.h)
SOURCES += $quote(/home/myusername/projects/src/myclass.cpp)
SOURCES += main.cpp

when I run the makefile, it seems to work until it gets to the part of my main.cpp where i include the header file and then it says: fatal error, no such file or directory

I feel like i'm making a really basic mistake, but I can't seem to figure it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笔落惊风雨 2024-10-23 13:11:48

首先,在项目文件中使用绝对路径绝对是一个坏主意。

如果该类是项目的一部分,但位于另一个目录中,请在项目文件和 #include 指令中使用相对路径,使用 #include "relative/path/ myclass.h" 语法。

如果该类不是项目的一部分,那么您应该将其编译为库,然后使用带有以下选项的 qmake:

qmake INCLUDEPATH+=/path/to/the/header LIBS+=-L/path/to/the/library

并将库名称添加到项目文件中:

LIBS += -llibraryname

然后您可以将您的类包含为 #include,注意 <> 语法。

请注意,特定于工作站的内容将转到命令行,但与工作站无关的库名称将转到项目文件。如果您想提供一些合理的默认位置,您可以使用以下技巧:

unix { # default path for the Unix systems
  isEmpty(MYLIB_PATH): MYLIB_PATH = /usr/local
}
INCLUDEPATH += $MYLIB_PATH/include
LIBS += -L$MYLIB_PATH/lib

然后,如果您愿意,您仍然可以从命令行覆盖路径:

qmake MYLIB_PATH=/home/myusername/mylib

First, using absolute paths in a project file is definitely a bad idea.

If that class is a part of the project, but is located in another directory, use relative paths both in the project file and in the #include directive, using #include "relative/path/myclass.h" syntax.

If that class is not a part of the project, then you should compile it as a library, then use qmake with the following options:

qmake INCLUDEPATH+=/path/to/the/header LIBS+=-L/path/to/the/library

And add the library name to the project file:

LIBS += -llibraryname

Then you may include your class as #include <myclass.h>, note the <> syntax.

Note that workstation-specific things go to the command line, but the workstation-independent library name goes to the project file. If you want to provide some sensible default location, you could use the following trick:

unix { # default path for the Unix systems
  isEmpty(MYLIB_PATH): MYLIB_PATH = /usr/local
}
INCLUDEPATH += $MYLIB_PATH/include
LIBS += -L$MYLIB_PATH/lib

Then, if you want, you can still override the path from the command line:

qmake MYLIB_PATH=/home/myusername/mylib
菊凝晚露 2024-10-23 13:11:48

我最终在@Sergey Tachenov 的帮助下解决了这个问题。我使用“../”将其从绝对路径更改为相对路径。

HEADERS += ../src/classfile.h
SOURCES += ../src/classfile.cpp
SOURCES += main.cpp

我还修改了 main.cpp 包含文件,以便在进行

#include "../src/classfile.h"

这些更改后,它可以正确编译并运行。

谢谢!

i ended up figuring it out with a little help from @Sergey Tachenov. I changed it from an absolute path to a relative path by using "../".

HEADERS += ../src/classfile.h
SOURCES += ../src/classfile.cpp
SOURCES += main.cpp

I also modified the main.cpp include file so that it was

#include "../src/classfile.h"

after making these changes, it compiled and ran correctly.

Thanks!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文