Reputation: 4087
I have a project with 2 subproject (Sub1 and Sub2):
ESI
|
|__Sub1
|
|__Sub2
In a implementation file (.cpp) of a class in the Sub2 (demo1.cpp) project i've the needs of include a class of the Sub1 project. So in the demo1.cpp i've declared:
#include "../sub1/controller.h"
Why the linker doesn't find the constructor of the controller? (says undefined reference)
Upvotes: 2
Views: 96
Reputation: 12600
Your #include
tells the Compiler where it can find the definition of your controller
class (i.e. the content of your controller.h
file).
In order to link towards the implementation of your controller
class, the linker requires a library which contains the already compiled implementation. - The Sub2
project does not compile the contents of Sub1
again, unless you include the .cpp
files of Sub1
in your Sub2
project which would be bad practice and could lead to further problems, however.
Assuming Sub1
is a library which will be generated as sub1.lib
:
You need to add the following lines to your .pro
file of your Sub2
project:
LIBS += -L../Sub1 \
-lsub1
This tells the linker that there is a "../Sub1" directory which contains libraries and that there is a library called "sub1.lib". The advantage of using the -L
and -l
syntax is that this works on all platforms (Windows, Linux, Mac, etc).
If both Sub1
and Sub2
are executables (i.e. the projects produce a .exe
) file, you need to extract the common content into a separate project Sub3
which has TEMPLATE = lib
instead of TEMPLATE = app
in its .pro
file.
Upvotes: 2