CMake链接依赖

链接依赖传递

socmonitor --> libsoccommgr.so --> libdatabus.so

在CMakelist已经写了

TARGET_LINK_LIBRARIES(soccommgr databus)

CMake 构建libsoccommgr.so正常输出,在构建socmonitor 提示找不到libdatabus.so的函数

Scanning dependencies of target socmonitor
[ 85%] Building CXX object soc_monitor/CMakeFiles/socmonitor.dir/src/soc_monitor.cpp.o
[100%] Linking CXX executable socmonitor
../soc_com_mgr/libsoccommgr.so: undefined reference to `DataBusShutdown()'
../soc_com_mgr/libsoccommgr.so: undefined reference to `DataBusSendData(unsigned long, void*, int)'
../soc_com_mgr/libsoccommgr.so: undefined reference to `DataBusInit()'
../soc_com_mgr/libsoccommgr.so: undefined reference to `DataBusRegisterDataReader(unsigned long, void (*)(unsigned long, void*, int))'
collect2: error: ld returned 1 exit status
soc_monitor/CMakeFiles/socmonitor.dir/build.make:85: recipe for target 'soc_monitor/socmonitor' failed
make[2]: *** [soc_monitor/socmonitor] Error 1
CMakeFiles/Makefile2:190: recipe for target 'soc_monitor/CMakeFiles/socmonitor.dir/all' failed
make[1]: *** [soc_monitor/CMakeFiles/socmonitor.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2

使用ldd命令提示如下,缺少libdatabus.so的链接依赖

build$ ldd soc_com_mgr/libsoccommgr.so 
	linux-vdso.so.1 (0x00007ffd21bc7000)
	libcomm.so => /usr/lib/libcomm.so (0x00007fb2aeff7000)
	libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fb2aec6e000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb2ae87d000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb2ae65e000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb2ae2c0000)
	/lib64/ld-linux-x86-64.so.2 (0x00007fb2af40d000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb2ae0a8000)

使用ld命令提示如下

build$ ld soc_com_mgr/libsoccommgr.so 
ld: warning: cannot find entry symbol _start; not setting start address
soc_com_mgr/libsoccommgr.so: undefined reference to `DataBusShutdown()'
soc_com_mgr/libsoccommgr.so: undefined reference to `DataBusSendData(unsigned long, void*, int)'
soc_com_mgr/libsoccommgr.so: undefined reference to `DataBusInit()'
soc_com_mgr/libsoccommgr.so: undefined reference to `DataBusRegisterDataReader(unsigned long, void (*)(unsigned long, void*, int))'

查找了几种方法:
1、尝试用LINK_DIRECTORIES 指令将/usr/lib添加到共享库搜索路径
2、检查一下用户环境变量LD_LIBRARY_PATH, 为空,将/usr/lib加到环境变量中
3、最后查到/etc/ld.so.conf这个文件,此文件记录了编译时使用的动态库的路径,也就是加载so库的路径。将/usr/lib加入到文件/etc/ld.so.conf中,执行ldconfig命令,刷新库文件缓存
4、C++调用C混编,需要在库文件的头文件中加上下面的语句,用以指定在编译C++的时候被调用的代码仍然使用C编译选项

#ifndef __DATA_BUS_H__
#define __DATA_BUS_H__

#include <stdint.h>

#ifdef __cplusplus
extern "C"
{
#endif

void DataBusShutdown();

#ifdef __cplusplus
}
#endif

#endif  // __DATA_BUS_H__

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值