Qmake 项目文件
我有一个我制作的类文件(头文件和 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,在项目文件中使用绝对路径绝对是一个坏主意。
如果该类是项目的一部分,但位于另一个目录中,请在项目文件和
#include
指令中使用相对路径,使用#include "relative/path/ myclass.h"
语法。如果该类不是项目的一部分,那么您应该将其编译为库,然后使用带有以下选项的 qmake:
并将库名称添加到项目文件中:
然后您可以将您的类包含为
#include
,注意<>
语法。请注意,特定于工作站的内容将转到命令行,但与工作站无关的库名称将转到项目文件。如果您想提供一些合理的默认位置,您可以使用以下技巧:
然后,如果您愿意,您仍然可以从命令行覆盖路径:
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:
And add the library name to the project file:
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:
Then, if you want, you can still override the path from the command line:
我最终在@Sergey Tachenov 的帮助下解决了这个问题。我使用“../”将其从绝对路径更改为相对路径。
我还修改了 main.cpp 包含文件,以便在进行
这些更改后,它可以正确编译并运行。
谢谢!
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 "../".
I also modified the main.cpp include file so that it was
after making these changes, it compiled and ran correctly.
Thanks!