Qmake Project File
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开发者_如何学Python/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.
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
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!
精彩评论