未定义的引用。在 cmake 中:lib 到 lib

Undefined reference to. In cmake: lib to lib

Windows 7 x86,cmake 版本 3.15.2,qtcreator 4.10,Qt5.12.4 MinGW。我用两个库构建了一个项目,在其中一个库中,我从另一个库中调用 class 。结果,我得到一个错误。

...file2.cpp:-1: ошибка: undefined reference to `MyClass1::MyClass1()'
collect2.exe:-1: ошибка: error: ld returned 1 exit status

[ 91%] Linking CXX executable flasher.exe
lib2/liblib2.a(file2.cpp.obj):file2.cpp:(.text+0x18): undefined reference to `MyClass1::MyClass1()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[2]: *** [CMakeFiles\my.dir\build.make:107: flasher.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:83: CMakeFiles/my.dir/all] Error 2
mingw32-make.exe: *** [Makefile:83: all] Error 2

最小可重现示例:githab.com Src.

Linker 无法解析 myclass1 的构造函数。这可能意味着没有定义,或者您没有 Link 库。确保您有构造函数的函数体,并且给定的库文件已链接。

主要CMakeLists.txt:

target_link_libraries(${PROJECT_NAME} Qt5::Widgets
    lib2 lib1)

或在 lib2 CMakeList.txt:

target_link_libraries(${PROJECT_NAME} lib1)

或在main.cpp:

auto m1 = new MyClass1;