target_link_libraries() 出现 undefined reference to 的问题

这里写自定义目录标题

问题描述

使用cmake编译工程,代码编译正常,在link阶段出现问题:

[100%] Linking CXX executable fca_unit_test
CMakeFiles/fca_unit_test.dir/UnitTest/SseTest.cpp.o: In function `____C_A_T_C_H____T_E_S_T____0()':
/home/illusion/workspace/00_shared_folder/audio_processing_plugin/src/UnitTest/SseTest.cpp:400: undefined reference to `Eb_AudioProcessing_Create(void**, t_audio_pro_type, unsigned int, unsigned int, void*)'
/home/illusion/workspace/00_shared_folder/audio_processing_plugin/src/UnitTest/SseTest.cpp:552: undefined reference to `EB_AudioProcessing_Apply(void*, t_audio_pro_type, void*)'
collect2: error: ld returned 1 exit status
CMakeFiles/fca_unit_test.dir/build.make:121: recipe for target 'fca_unit_test' failed
make[2]: *** [fca_unit_test] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/fca_unit_test.dir/all' failed
make[1]: *** [CMakeFiles/fca_unit_test.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2

问题解决

查看CMake.txt文件

add_library(${PROJECT_OUTPUT} SHARED ${SOURCE_LIST})
target_compile_definitions(${PROJECT_OUTPUT} PUBLIC ${PROJECT_GLOBAL_DEFINITION})
target_include_directories(${PROJECT_OUTPUT} PUBLIC ${PROJECT_GLOBAL_INCLUDE})
target_link_libraries(${PROJECT_OUTPUT} "-Wl,--whole-archive" ${PROJECT_LOCAL_LIBRARY} "-Wl,-no-whole-archive" ${PROJECT_SYSTEM_LIBRARY})

# build unit test
set(UNIT_TEST_INCLUDE "")

add_executable(${EXEXUTABLE_BIN_OUTPUT} ${TEST_SOURCE_LIST})
target_compile_definitions(${EXEXUTABLE_BIN_OUTPUT} PUBLIC ${PROJECT_GLOBAL_DEFINITION})
target_include_directories(${EXEXUTABLE_BIN_OUTPUT} PUBLIC ${PROJECT_GLOBAL_INCLUDE} ${UNIT_TEST_INCLUDE})
target_link_libraries(${EXEXUTABLE_BIN_OUTPUT} ${PROJECT_OUTPUT})

可以看出没什么问题,但是考虑到这个${EXEXUTABLE_BIN_OUTPUT} 是cpp 写的,而库文件是c写的,那么可能存在链接解析的问题,只要在引用的库文件中添加 extern C的标志就可以了。

#ifdef __cplusplus
extern "C"
{
#include ""
}
#endif

Reference

https://blog.csdn.net/henry860916/article/details/50434228

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值