将外部库添加到 CMakeList.txt C++

2021-12-26 00:00:00 libraries cmake c++

我有我的外部库,如下图所示,我在之后创建符号链接:

I have my external library as shown in this picture that I create the symbolic links after:

以及其他文件中与库相关的头文件:

and the headers related to the library in other file:

我正在使用 ROS ubuntu,我需要将这些库添加到我的包中到 CmakeList.txt:

I'm working with ROS ubuntu and I need to add these libraries to my package to CmakeList.txt:

cmake_minimum_required(VERSION 2.4.6)
include($ENV{ROS_ROOT}/core/rosbuild/rosbuild.cmake)

rosbuild_init()

#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

#common commands for building c++ executables and libraries
#rosbuild_add_library(${PROJECT_NAME} src/example.cpp)
#target_link_libraries(${PROJECT_NAME} another_library)
#rosbuild_add_boost_directories()
#rosbuild_link_boost(${PROJECT_NAME} thread)
#rosbuild_add_executable(example examples/example.cpp)
#target_link_libraries(example ${PROJECT_NAME})

rosbuild_add_executable(kinectueye src/kinect_ueye.cpp)

所以我的问题是如何将这些文件夹(我认为我需要添加的第一个文件夹我不确定)添加到我的 CmakeList.txt 文件中,以便我可以使用这些类以及我程序中的方法.

So my question is how can I add these folders (I think the first one that I need to add I'm not sure) to my CmakeList.txt file so as I can use the classes and the methods in my program.

推荐答案

我会从升级 CMAKE 版本开始.

I would start with upgrade of CMAKE version.

您可以使用 INCLUDE_DIRECTORIES 作为标题位置和 LINK_DIRECTORIES + TARGET_LINK_LIBRARIES 图书馆

You can use INCLUDE_DIRECTORIES for header location and LINK_DIRECTORIES + TARGET_LINK_LIBRARIES for libraries

INCLUDE_DIRECTORIES(your/header/dir)
LINK_DIRECTORIES(your/library/dir)
rosbuild_add_executable(kinectueye src/kinect_ueye.cpp)
TARGET_LINK_LIBRARIES(kinectueye lib1 lib2 lib2 ...)

注意 lib1 被扩展为 liblib1.so(在 Linux 上),所以 使用 ln 创建适当的链接,以防您没有它们

note that lib1 is expanded to liblib1.so (on Linux), so use ln to create appropriate links in case you do not have them

相关文章