【问题标题】:Undefined reference to. In cmake: lib to lib [duplicate]未定义的引用。在cmake中:lib到lib [重复]
【发布时间】:2019-09-19 05:07:04
【问题描述】:

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

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

【问题讨论】:

标签: windows qt cmake qt-creator


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    在主 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-07
      • 2018-02-08
      • 1970-01-01
      • 2012-12-06
      • 2022-12-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多