I have three projects: A, B, and C. Project A is an executable/DLL, and projects B and C are static libraries. There is a function Add(int, int)
in B, which is called by A. The implementation of Add
depends on a function AddImpl
from C.
As per my understanding of C++, I only need to link B when compiling A because all the contents of AddImpl
should have been copied when compiling B. This works correctly in Visual Studio 2019/2022, but it doesn’t work in Qt Creator, even though the compiler is MSVC in both cases.
Is this a bug in QMake, or am I missing something in C++?
Qt project:
A.pro
QT -= gui
CONFIG += c++11
SOURCES += A.cpp
LIBS += -Lpath/to/B -lB
INCLUDEPATH += path/to/Bheader
A.cpp
#include <B.h>
int main() {
Add(1, 2);
return 0;
}
B.pro
QT -= gui
CONFIG += c++11
SOURCES += B.cpp
LIBS += -Lpath/to/C -lC
INCLUDEPATH += path/to/Cheader
B.h
int Add(int, int);
B.cpp
#include <C.h>
#include "B.h"
int Add(int a, int b) {
return AddImpl(a, b);
}
C.pro
QT -= gui
CONFIG += c++11
SOURCES += C.cpp
// C.h
int AddImpl(int, int);
// C.cpp
int AddImpl(int a, int b) {
return a + b;
}
In Visual Studio, I set it up by right click on project, going to Properties/Librarian and set C.lib to ‘Additional Dependencies’ and set output path of C to ‘Additional Library Directories’. The ‘Additional Dependencies’ of A is under Properties/Linker/General and the ‘Additional Library Directories’ is under Properties/Linker/Input.
Also, I noticed that qmake uses ‘link.exe’ in link stage to compile static lib while msbuild use ‘lib.exe’ to do the same thing. I don’t know if this can explain the different behavior.
4