简体   繁体   English

Qmake项目文件

[英]Qmake Project File

I have a class file (header and cpp) that I made, that I want to use in my main.cpp file. 我有一个类文件(header和cpp),我想在main.cpp文件中使用它。 I generated a qmake project file (from the current directory of my main.cpp) and added the header and cpp with: 我生成了一个qmake项目文件(来自我的main.cpp的当前目录)并添加了标题和cpp:

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 当我运行makefile时,它似乎工作,直到它到达我的main.cpp的一部分,其中我包括头文件,然后它说:致命错误,没有这样的文件或目录

I feel like i'm making a really basic mistake, but I can't seem to figure it out. 我觉得我犯了一个非常基本的错误,但我似乎无法弄明白。

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. 如果该类是项目的一部分,但位于另一个目录中,请使用#include "relative/path/myclass.h"语法在项目文件和#include指令中使用#include "relative/path/myclass.h"

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:

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. 然后你可以把你的类包含为#include <myclass.h> ,注意<>语法。

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

i ended up figuring it out with a little help from @Sergey Tachenov. 我最终在@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 我还修改了main.cpp包含文件

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

after making these changes, it compiled and ran correctly. 进行这些更改后,它编译并正确运行。

Thanks! 谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM