链接依赖传递
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__