注册。使用 CMake 将外部库链接到项目

Posted

技术标签:

【中文标题】注册。使用 CMake 将外部库链接到项目【英文标题】:Reg. Linking External Library to Project using CMake 【发布时间】:2017-11-24 14:54:39 【问题描述】:

我正在尝试使用外部库构建项目,但系统一直认为它需要使用损坏的 usr/lib 中的库。我希望改用地址中构建的库:/home/CMake-hdf5-1.8.18/build/HDF5-1.8.18-Linux/HDF_Group/HDF5/1.8.18/include。这是 CMakeLists.txt。我想要的解决方案是 #Add hdf5 库下的两行。

cmake_minimum_required(VERSION 2.8.3)
project(scan_to_cloud_converter)

# List C++ dependencies on ros packages
set( ROS_CXX_DEPENDENCIES
  roscpp
  pcl_ros
  pcl_conversions)

# Find catkin and all required ROS components
find_package(catkin REQUIRED COMPONENTS $ROS_CXX_DEPENDENCIES)
find_package(PCL REQUIRED QUIET)

# Set include directories
include_directories(include $catkin_INCLUDE_DIRS $PCL_INCLUDE_DIRS)

# Declare info that other packages need to import library generated here
catkin_package( )

#Create node
add_executable( scan_to_cloud_converter_node
src/scan_to_cloud_converter_node.cpp
src/scan_to_cloud_converter.cpp )

#Add hdf5 library
link_directories(/home/CMake-hdf5-1.8.18/build/HDF5-1.8.18-Linux/HDF_Group/HDF5/1.8.18/include)
target_link_libraries(scan_to_cloud_converter_node libhdf5)

# No need to link against pcl (using header only libraries)
target_link_libraries( scan_to_cloud_converter_node $catkin_LIBRARIES)

add_dependencies(scan_to_cloud_converter_node $catkin_EXPORTED_TARGETS)

#Install node
install(TARGETS scan_to_cloud_converter_node
RUNTIME DESTINATION $CATKIN_PACKAGE_BIN_DESTINATION )

这是我仍然收到的错误消息:

make[2]: *** No rule to make target '/usr/lib/x86_64-linux-gnu/hdf5/serial/lib/libhdf5.so', needed by '/home/catkin_ws/devel/lib/scan_to_cloud_converter/scan_to_cloud_converter_node'.  Stop.

我已经对此错误进行了研究,这是因为它仍在查看 /usr/lib,并且没有库 libhdf5.so,因为符号链接已损坏。那么我如何让它查看该库的另一个地址呢?任何帮助消除此错误将不胜感激

【问题讨论】:

奇怪的是你的图书馆在.../include而不是.../lib。反正你不应该写target_link_libraries(scan_to_cloud_converter_node hdf5)吗? 嗯,实际上有一个带有 libhdf5.a 的 /lib 文件夹,我应该将链接重定向到这个吗?我应该有那个 target_link_libraries 行吗? 所以我也尝试了 /lib 文件夹,但得到了同样的错误。我的预感是它仍在 /usr/lib 中查找而没有被重定向到这个新地址,所以不知道如何更改它。 你可以试试target_link_libraries(scan_to_cloud_converter_node /home/CMake-hdf5-1.8.18/build/HDF5-1.8.18-Linux/HDF_Group/HDF5/1.8.18/lib/libhdf5.a)。你在某处有libhdf5.so 吗? make[2]: *** 没有规则来制作目标 '/home//CMake-hdf5-1.8.18/build/HDF5-1.8.18-Linux/HDF_Group/HD‌​F5/ 1.8.18/lib/libhdf‌​5.a','/home/cakin_ws/devel/lib/scan_to_cloud_converter/scan_to_cloud_converter_node'需要。停止。是我在上面尝试时得到的错误^ 【参考方案1】:

为了将cmake项目与hdf5链接,我建议使用cmake的find_package功能。

包含该行

find_package(HDF5)

CMakeLists.txt.

然后,您可以使用target_link_libraries(your_lib $HDF5_C_LIBRARIES) 进行适当的链接。包含目录存储在$HDF5_INCLUDE_DIRS

现在,关于选择特定 HDF5 位置的问题,发出命令

export HDF5_ROOT=/home/CMake-hdf5-1.8.18/build/HDF5-1.8.18-Linux/HDF_Group/HD‌​F5/1.8.18

在发布 cmake 之前。你需要有一个清晰的缓存才能工作:

rm -r CMakeCache.txt CMakeFiles

【讨论】:

以上是关于注册。使用 CMake 将外部库链接到项目的主要内容,如果未能解决你的问题,请参考以下文章

cmake 将外部库与 IMPORT_SONAME ro IMPORT_LOCATION 链接

CMake 链接到外部库

CMake:将第三方库链接到项目库

使用 CMake 只构建一次外部库

无法将 sndfile 库链接到 cmake 项目(MacOS)

是否可以将 cmake 项目链接到子项目?