c-在CMake〔linux〕中链接静态库的问题



我在项目中使用预先构建的静态库。我在链接中遇到问题。详情如下。

cmake

link_directories(${CMAKE_CURRENT_DIR}/lib)
add_executable(main isotp_send.c)
target_link_libraries(main -static-libgcc -static dl
pthread
${CMAKE_CURRENT_DIR}/lib/libIsoTp.a
${CMAKE_CURRENT_DIR}/lib/libBL.a
${CMAKE_CURRENT_DIR}/lib/libCanTp.a
${CMAKE_CURRENT_DIR}/lib/libDcm.a
${CMAKE_CURRENT_DIR}/lib/libEa.a
${CMAKE_CURRENT_DIR}/lib/libFls.a
${CMAKE_CURRENT_DIR}/lib/libNvM.a
${CMAKE_CURRENT_DIR}/lib/libSd.a
${CMAKE_CURRENT_DIR}/lib/libSRec.a
${CMAKE_CURRENT_DIR}/lib/libCanLib.a
${CMAKE_CURRENT_DIR}/lib/libCom.a
${CMAKE_CURRENT_DIR}/lib/libDem.a
${CMAKE_CURRENT_DIR}/lib/libEep.a
${CMAKE_CURRENT_DIR}/lib/libOsekNm.a
${CMAKE_CURRENT_DIR}/lib/libSoAd.a
${CMAKE_CURRENT_DIR}/lib/libStdBit.a
${CMAKE_CURRENT_DIR}/lib/libCanNm.a
${CMAKE_CURRENT_DIR}/lib/libCrc.a
${CMAKE_CURRENT_DIR}/lib/libDoIP.a
${CMAKE_CURRENT_DIR}/lib/libFee.a
${CMAKE_CURRENT_DIR}/lib/libLoader.a
${CMAKE_CURRENT_DIR}/lib/libRingBuffer.a
${CMAKE_CURRENT_DIR}/lib/libSomeIp.a
${CMAKE_CURRENT_DIR}/lib/libStdTimer.a

)

而建筑面临问题。以下日志

[  9%] Building C object CMakeFiles/main.dir/isotp_send.c.o
[ 18%] Building C object CMakeFiles/main.dir/src/Can.c.o
[ 27%] Building C object CMakeFiles/main.dir/src/Can_Lcfg.c.o
[ 36%] Building C object CMakeFiles/main.dir/src/critical.c.o
[ 45%] Building C object CMakeFiles/main.dir/src/Dem_Stub.c.o
[ 54%] Building C object CMakeFiles/main.dir/src/EepAc.c.o
[ 63%] Building C object CMakeFiles/main.dir/src/Flash.c.o
[ 72%] Building C object CMakeFiles/main.dir/src/FlsAc.c.o
[ 81%] Building C object CMakeFiles/main.dir/src/Key.c.o
[ 90%] Building C object CMakeFiles/main.dir/src/simulator.c.o
[100%] Linking C executable main
/usr/bin/ld: ../lib/libIsoTp.a(isotp_can.o):isotp_can.c:(.text+0x32): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: ../lib/libIsoTp.a(isotp_can.o):isotp_can.c:(.text+0x43): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: ../lib/libIsoTp.a(isotp_can.o):isotp_can.c:(.text+0x45f): undefined reference to `sem_init'
/usr/bin/ld: ../lib/libIsoTp.a(Dcm_Cfg.o):Dcm_Cfg.c:(.text+0x32): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: ../lib/libIsoTp.a(Dcm_Cfg.o):Dcm_Cfg.c:(.text+0x43): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: ../lib/libCanTp.a(CanTp.o):CanTp.c:(.text+0x32): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: ../lib/libCanTp.a(CanTp.o):CanTp.c:(.text+0x43): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: ../lib/libDcm.a(Dcm.o):Dcm.c:(.text+0x32): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: ../lib/libDcm.a(Dcm.o):Dcm.c:(.text+0x43): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: ../lib/libDcm.a(Dcm_Dsd.o):Dcm_Dsd.c:(.text+0x32): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: ../lib/libDcm.a(Dcm_Dsd.o):Dcm_Dsd.c:(.text+0x43): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: ../lib/libDcm.a(Dcm_Dsl.o):Dcm_Dsl.c:(.text+0x32): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: ../lib/libDcm.a(Dcm_Dsl.o):Dcm_Dsl.c:(.text+0x43): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: ../lib/libCanLib.a(canlib.o):canlib.c:(.text+0x33): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: ../lib/libCanLib.a(canlib.o):canlib.c:(.text+0x75): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: ../lib/libCanLib.a(canlib.o):canlib.c:(.text+0x86): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: ../lib/libCanLib.a(canlib.o):canlib.c:(.text+0xaa): undefined reference to `__imp__time64'
/usr/bin/ld: ../lib/libCanLib.a(canlib.o):canlib.c:(.text+0xc9): undefined reference to `__imp__localtime64'
/usr/bin/ld: ../lib/libCanLib.a(canlib.o):canlib.c:(.text+0xd3b): undefined reference to `__imp_isprint'
/usr/bin/ld: ../lib/libCanLib.a(peak_can.o):peak_can.c:(.text+0x32): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: ../lib/libCanLib.a(peak_can.o):peak_can.c:(.text+0x43): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: ../lib/libCanLib.a(peak_can.o):peak_can.c:(.text+0x258): undefined reference to `CAN_Initialize'
/usr/bin/ld: ../lib/libCanLib.a(peak_can.o):peak_can.c:(.text+0x2cf): undefined reference to `__imp__assert'
/usr/bin/ld: ../lib/libCanLib.a(peak_can.o):peak_can.c:(.text+0x3a8): undefined reference to `__imp__assert'
/usr/bin/ld: ../lib/libCanLib.a(peak_can.o):peak_can.c:(.text+0x433): undefined reference to `CAN_Write'
/usr/bin/ld: ../lib/libCanLib.a(peak_can.o):peak_can.c:(.text+0x5c7): undefined reference to `CAN_Read'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x32): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x43): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x140): undefined reference to `__imp_WSAStartup'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x1d0): undefined reference to `__imp_inet_addr'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x1ed): undefined reference to `__imp_htons'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x20d): undefined reference to `__imp_socket'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x278): undefined reference to `__imp_connect'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x2a3): undefined reference to `__imp_WSAGetLastError'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x2c5): undefined reference to `__imp_closesocket'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x2f0): undefined reference to `__imp_WSAGetLastError'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x33b): undefined reference to `__imp_ioctlsocket'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x380): undefined reference to `__imp__assert'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x4af): undefined reference to `__imp__assert'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x549): undefined reference to `__imp__assert'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x585): undefined reference to `__imp_send'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x6cf): undefined reference to `__imp_closesocket'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x756): undefined reference to `__imp_recvfrom'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x7c2): undefined reference to `__imp_WSAGetLastError'
/usr/bin/ld: ../lib/libCanLib.a(simulator_can.o):simulator_can.c:(.text+0x804): undefined reference to `__imp_WSAGetLastError'
/usr/bin/ld: ../lib/libCanLib.a(vxl_can.o):vxl_can.c:(.text+0x33): undefined reference to `__mingw_vsscanf'
/usr/bin/ld: ../lib/libCanLib.a(vxl_can.o):vxl_can.c:(.text+0x75): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: ../lib/libCanLib.a(vxl_can.o):vxl_can.c:(.text+0x86): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: ../lib/libCanLib.a(vxl_can.o):vxl_can.c:(.text+0xca): undefined reference to `__mingw_vsprintf'
/usr/bin/ld: ../lib/libCanLib.a(vxl_can.o):vxl_can.c:(.text+0x205): undefined reference to `__imp_strncpy_s'
/usr/bin/ld: ../lib/libCanLib.a(vxl_can.o):vxl_can.c:(.text+0x287): undefined reference to `__imp_strncpy_s'
/usr/bin/ld: ../lib/libCanLib.a(vxl_can.o):vxl_can.c:(.text+0x67c): undefined reference to `__imp__assert'
/usr/bin/ld: ../lib/libCanLib.a(vxl_can.o):vxl_can.c:(.text+0x79d): undefined reference to `__imp__assert'
/usr/bin/ld: ../lib/libCanLib.a(vxl_can.o):vxl_can.c:(.text+0xbcd): undefined reference to `__imp__assert'
/usr/bin/ld: ../lib/libCanLib.a(zlg_can.o):zlg_can.c:(.text+0x32): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: ../lib/libCanLib.a(zlg_can.o):zlg_can.c:(.text+0x43): undefined reference to `__mingw_vfprintf'
/usr/bin/ld: ../lib/libCanLib.a(zlg_can.o):zlg_can.c:(.text+0x73): undefined reference to `dlopen'
/usr/bin/ld: ../lib/libCanLib.a(zlg_can.o):zlg_can.c:(.text+0xcc): undefined reference to `dlsym'
/usr/bin/ld: ../lib/libCanLib.a(zlg_can.o):zlg_can.c:(.text+0x121): undefined reference to `dlsym'
/usr/bin/ld: ../lib/libCanLib.a(zlg_can.o):zlg_can.c:(.text+0x176): undefined reference to `dlsym'
/usr/bin/ld: ../lib/libCanLib.a(zlg_can.o):zlg_can.c:(.text+0x1cb): undefined reference to `dlsym'
/usr/bin/ld: ../lib/libCanLib.a(zlg_can.o):zlg_can.c:(.text+0x220): undefined reference to `dlsym'
/usr/bin/ld: ../lib/libCanLib.a(zlg_can.o):zlg_can.c:(.text+0x275): more undefined references to `dlsym' follow
/usr/bin/ld: ../lib/libCanLib.a(zlg_can.o):zlg_can.c:(.text+0x322): undefined reference to `dlclose'
/usr/bin/ld: ../lib/libCanLib.a(zlg_can.o):zlg_can.c:(.text+0x734): undefined reference to `__imp__assert'
/usr/bin/ld: ../lib/libCanLib.a(zlg_can.o):zlg_can.c:(.text+0x817): undefined reference to `__imp__assert'
/usr/bin/ld: ../lib/libCanLib.a(xlLoadlib.o):xlLoadlib.cpp:(.text+0x1f): undefined reference to `__imp_LoadLibraryA'
/usr/bin/ld: ../lib/libCanLib.a(xlLoadlib.o):xlLoadlib.cpp:(.text+0x69): undefined reference to `__imp_GetProcAddress'
/usr/bin/ld: ../lib/libCanLib.a(xlLoadlib.o):xlLoadlib.cpp:(.text+0xa2): undefined reference to `__imp_GetProcAddress'
/usr/bin/ld: ../lib/libCanLib.a(xlLoadlib.o):xlLoadlib.cpp:(.text+0xdb): undefined reference to `__imp_GetProcAddress'
/usr/bin/ld: ../lib/libCanLib.a(xlLoadlib.o):xlLoadlib.cpp:(.text+0x114): undefined reference to `__imp_GetProcAddress'
/usr/bin/ld: ../lib/libCanLib.a(xlLoadlib.o):xlLoadlib.cpp:(.text+0x14d): undefined reference to `__imp_GetProcAddress'
/usr/bin/ld: ../lib/libCanLib.a(xlLoadlib.o):xlLoadlib.cpp:(.text+0x186): more undefined references to `__imp_GetProcAddress' follow
/usr/bin/ld: ../lib/libCanLib.a(xlLoadlib.o):xlLoadlib.cpp:(.text+0x2b15): undefined reference to `__imp_FreeLibrary'
/usr/bin/ld: ../lib/libStdTimer.a(std_timer.o):std_timer.c:(.text+0xe6): undefined reference to `__imp__assert'
/usr/bin/ld: ../lib/libCanLib.a(xlLoadlib.o):xlLoadlib.cpp:(.text$_Z6printfPKcz[_Z6printfPKcz]+0x32): undefined reference to `__imp___acrt_iob_func'
/usr/bin/ld: ../lib/libCanLib.a(xlLoadlib.o):xlLoadlib.cpp:(.text$_Z6printfPKcz[_Z6printfPKcz]+0x43): undefined reference to `__mingw_vfprintf'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:243: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2 

作为参考,我使用的是Ubuntu 20.0,我已经按照其他答案的建议重新安装了Mingw。此外,dl库链接就在target_link_directories中。

提前谢谢。

库顺序很重要,尤其是对于静态链接。由于一些错误消息与未从libdl中找到函数有关,因此请在所有本地构建的lib之后移动该库。

大多数其他错误似乎和MinGW标准库和GCC构建中的内部函数有关,所以它们可能和-static-libgcc选项有关。您应该能够通过删除该选项来验证这一点。通过将该选项移动到末尾,您可能可以在不删除该选项的情况下修复它,但您不应该首先需要它,因为您正在执行整体-static链接。

此外,不要链接libpthread。如果您的程序是pthreads程序,则将-pthread添加到C编译标志和链接器标志中(与在链接库中指定-lpthread不同(。

然而,这里有一些非常可疑的东西。您正在将libdl链接到静态链接的可执行文件中,但它提供的函数是动态链接器的接口,这不适用于静态二进制文件。

相关内容

  • 没有找到相关文章

最新更新