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

时间:2019-09-19 05:07:04

标签: windows qt cmake qt-creator

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

在主CMakeLists.txt中:

import numpy as np
from scipy.ndimage import convolve1d

# just for printing:
import numpy.lib.recfunctions as nlr
def show(x): print(nlr.unstructured_to_structured(x))

a = "00000","00100","10000","03502","00010"
A = np.array(a).view('U1').reshape(5,5,1).astype(float)

show(A)
# [[(0.,) (0.,) (0.,) (0.,) (0.,)]
#  [(0.,) (0.,) (1.,) (0.,) (0.,)]
#  [(1.,) (0.,) (0.,) (0.,) (0.,)]
#  [(0.,) (3.,) (5.,) (0.,) (2.,)]
#  [(0.,) (0.,) (0.,) (1.,) (0.,)]]

show(convolve1d(A,np.r_[np.logspace(2,0,3,base=0.99),0,0],axis=0,mode="constant"))
# [[(0.9801,) (0.    ,) (0.99  ,) (0.    ,) (0.    ,)]
#  [(0.99  ,) (2.9403,) (5.9005,) (0.    ,) (1.9602,)]
#  [(1.    ,) (2.97  ,) (4.95  ,) (0.9801,) (1.98  ,)]
#  [(0.    ,) (3.    ,) (5.    ,) (0.99  ,) (2.    ,)]
#  [(0.    ,) (0.    ,) (0.    ,) (1.    ,) (0.    ,)]]

或者在lib2 CMakeList.txt中:

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

或在main.cpp中:

target_link_libraries(${PROJECT_NAME} lib1)