Undefined reference to. In cmake: lib to lib [duplicate]

眉间皱痕 提交于 2021-01-29 12:53:45

问题


Windows 7 x86, cmake version 3.15.2, 4.10 qtcreator, Qt5.12.4 MinGW. I build a project with two libraries, and in one of them I call a class from the other. As a result, I get an error.

...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

Minimal reproducible example: githab.com Src.


回答1:


The Linker can not resolve the constructor for myclass1. This could mean no definition or you did not Link the library. Make sure you have a function body for the constructor and that the given library file is linked.




回答2:


In main CMakeLists.txt:

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

Or in lib2 CMakeList.txt:

target_link_libraries(${PROJECT_NAME} lib1)

Or in main.cpp:

auto m1 = new MyClass1;


来源:https://stackoverflow.com/questions/58004057/undefined-reference-to-in-cmake-lib-to-lib

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!