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.
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
#includedirective, 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: