CMake详解CMakeLists.txt

本文详细介绍CMake的基础概念及使用方法,包括CMakeLists.txt文件结构、常见命令介绍、实际案例演示等内容,帮助读者掌握CMake的基本操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

【快速查询】https://cmake.org/cmake/help/v2.8.8/cmake.html#section_Commands

1、CMake简介

  CMake是跨平台编译工具,比make更高级一些。其编译的主要工作是生成CMakeLists.txt文件,然后根据该文件生成Makefile,最后调用make来生成可执行程序或者动态库。所以基本步骤就只有两步:(1)cmake生成CMakeLists.txt文件;(2)make执行编译工作。

  下面一张图对比一下AutoTools与CMake的工作流程(可见CMake比较清晰简洁):

 

2、CMakeLists.txt文件

详情参考:http://wiki.ros.org/catkin/CMakeLists.txt

复制代码
1、Required CMake Version (cmake_minimum_required)
2、Package Name (project())
3、Find other CMake/Catkin packages needed for build (find_package())
4、Message/Service/Action Generators (add_message_files(), add_service_files(), add_action_files())
5、Invoke message/service/action generation (generate_messages())
6、Specify package build info export (catkin_package())
7、Libraries/Executables to build (add_library()/add_executable()/target_link_libraries())
8、Tests to build (catkin_add_gtest())
9、Install rules (install())
复制代码

(1)CMake Version: 
每一个 catkin CMakeLists.txt 必须以 CMake 需要的版本开始,Catkin 需要版本 2.8.3 或者更高

cmake_minimum_required(VERSION 2.8.3)

(2)Package name: 
CMake project function 指定的文件名。

project(robot_brain)

在 CMake script 文件中,引用 CMake package 可以使用变量 ${PROJECT_NAME}

(3)依赖功能包:

寻找需要用到的其他 CMake packages,用函数 find_package。 
至少依赖一个关于 catkin 的功能包

find_package(catkin REQUIRED)

如果要使用 C++ 和 Boost,则需要引用 find_package 包含 Boost,并且指明 Boost 的类型,如使用 Boost threads,则:

find_package(Boost REQUIRED COMPONENTS thread)

 (4)catkin_package() 

catkin_package() 是 catkin 支持的 CMake 宏指令。用来向编译系统指明 catkin-specific 的信息,而编译系统来生成 pkg-config and CMake files。

该函数必须用在用 add_library() or add_executable() 声明之前。 
有5个可选参数:

INCLUDE_DIRS - The exported include paths (i.e. cflags) for the package
LIBRARIES - The exported libraries from the project
CATKIN_DEPENDS - Other catkin projects that this project depends on
DEPENDS - Non-catkin CMake projects that this project depends on
CFG_EXTRAS - Additional configuration options

 例如:

catkin_package(
   INCLUDE_DIRS include
   LIBRARIES ${PROJECT_NAME}
   CATKIN_DEPENDS roscpp nodelet
   DEPENDS eigen opencv)

 (5)Specifying Build Targets

3、一个简单的CMakeLists.txt入门示例

  外部编译:单独建立一个空目录专门用于cmake编译,便于管理编译中间文件。在空目录中用 cmake ../(CMakeLists.txt所在目录)就行了,中间文件生成在当前目录。

  工程结构:

复制代码
.
├── build
├── CMakeLists.txt
├── include
│   └── math_lib.h
└── src
    ├── main.cpp
    └── math_lib.cpp

3 directories, 4 files

复制代码

  CMakeLists.txt示例(借鉴网友配置):

复制代码
# 1.cmake verson,指定cmake版本 
cmake_minimum_required(VERSION 3.4.1)

# 2.project name,指定项目的名称,一般和项目的文件夹名称对应
PROJECT(test_math_lib)

# 3.head file path,头文件目录
INCLUDE_DIRECTORIES(include)

# 4.source directory,源文件目录
AUX_SOURCE_DIRECTORY(src DIR_SRCS)

# 5.set environment variable,设置环境变量,编译用到的源文件全部都要放到这里,否则编译能够通过,但是执行的时候会出现各种问题,比如"symbol lookup error xxxxx , undefined symbol"
SET(TEST_MATH ${DIR_SRCS})

# 6.add executable file,添加要编译的可执行文件
ADD_EXECUTABLE(${PROJECT_NAME} ${TEST_MATH})

# 7.add link library,添加可执行文件所需要的库,比如我们用到了libm.so(命名规则:lib+name+.so),就添加该库的名称
TARGET_LINK_LIBRARIES(${PROJECT_NAME} m)

复制代码

   编译方法(直接生成目标程序):

复制代码
kuliuheng@ubuntu:~/_8GB_EXT/workspace/cpp/testCmake$ cd build/
kuliuheng@ubuntu:~/_8GB_EXT/workspace/cpp/testCmake/build$ cmake ../
-- The C compiler identification is GNU 7.4.0
-- The CXX compiler identification is GNU 7.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/kuliuheng/_8GB_EXT/workspace/cpp/testCmake/build
复制代码

4、示例:如何给动态库添加三方库依赖

方法一:用LINK_DIRECTORIES 来指定三方库路径,然后通过库名称去链接。需要注意一个坑就是LINK_DIRECTORIES 要放到 ADD_LIBRARY 前面才行。

复制代码
file(GLOB_RECURSE header1_files "src/main/cpp/3rd/*.h")
file(GLOB_RECURSE source1_files "src/main/cpp/3rd/*.cpp")

LINK_DIRECTORIES(“libs/${ANDROID_ABI}”) # 添加三方库的绝对/相对路径,用于搜索三方库。但是千万要注意一点:要放到ADD_LIBRARY之前,否则不生效!

ADD_LIBRARY (
my_lib  # 设置生成动态库的名称
SHARED  # 设置库类型,STATIC 或 SHARED

         <span style="color: rgba(0, 128, 0, 1)"># 需要编译的c</span></span><span style="color: rgba(0, 128, 0, 1)">/c++</span><span style="color: rgba(0, 0, 0, 1)"><span style="color: rgba(0, 128, 0, 1)"> 文件</span>
         ${source1_files}
         src</span>/main/<span style="color: rgba(0, 0, 255, 1)">cpp</span>/jni_main.<span style="color: rgba(0, 0, 255, 1)">cpp</span><span style="color: rgba(0, 0, 0, 1)">
         )

target_link_libraries(
my_lib  # 链接目标

                   # Links the target library to the log library included in the NDK</span><span style="color: rgba(0, 0, 0, 1)">
                   ${log</span>-<span style="color: rgba(0, 0, 0, 1)">lib}

            plug_ins_project        <span style="color: rgba(0, 128, 0, 1)"># 这个就是对应libplug_ins_project.so库文件</span>
    )    </span></pre>
复制代码

 方法二:通过ADD_LIBRARY 结合 SET_TARGET_PROPERTIER 来指定第三方库绝对路径信息,然后连接库名即可。

TIPS:可以用MESSAGE打印调试日志—— MESSAGE(WARNING "${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI}")

复制代码
# 通过add_library 可以引入三方库,通过set_target_properties 指定具体三方库的属性
add_library(plug_ins
        SHARED
        IMPORTED)
set_target_properties( plug_ins
        PROPERTIES IMPORTED_LOCATION
        ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI}/libplug_ins_project.so)

target_link_libraries( # Specifies the target library.
# 指定链接的目标库
my_lib

                   # Links the target library to the log library
                   # included </span><span style="color: rgba(0, 0, 255, 1)">in</span><span style="color: rgba(0, 0, 0, 1)"> the NDK.
                   ${log</span>-<span style="color: rgba(0, 0, 0, 1)">lib}
    plug_ins
    )</span></pre>
复制代码

 

<think>嗯,用户想了解如何在ROS1中修改CMakeLists.txt文件的属性。首先,我需要回忆一下ROS1中的CMakeLists.txt的结构和常用配置。根据之前的引用资料,CMakeLists.txt在catkin项目中是标准的,但有一些限制。 首先,用户提到的“属性”可能指的是编译选项、链接库、目标属性等。比如设置C++标准、添加依赖库、自定义编译选项等。比如引用3中提到用add_library来生成链接库,这可能涉及到目标属性的修改。 接下来,我需要整理常见的修改点,比如设置C++版本,可以通过set(CMAKE_CXX_STANDARD 14)。然后,添加依赖库的话,需要用到find_package和target_link_libraries,如引用1和引用2提到的结构。 另外,用户可能需要修改生成的可执行文件的安装路径,这涉及install命令。还有自定义编译选项,比如在引用3中的add_library,可能需要设置编译选项,比如add_compile_options。 需要注意的是,ROS1基于catkin,所以CMakeLists.txt需要包含必要的catkin宏,比如find_package(catkin REQUIRED)和catkin_package(),这些可能在引用1和引用2中有提到。 然后,用户可能需要如何调整这些属性,比如修改编译标志,或者调整优化选项。这时候可能需要使用set_target_properties或者add_compile_options。 还需要考虑错误处理,比如确保依赖包正确找到,否则编译会失败。同时,编译后的安装路径设置是否正确,比如使用install()命令指定安装位置。 最后,生成相关问题的时候,需要围绕CMakeLists.txt的常见配置问题,比如如何添加依赖,如何设置C++版本,如何处理链接库,安装路径设置等。</think>### ROS1中修改CMakeLists.txt文件属性的方法 在ROS1中,`CMakeLists.txt`是构建ROS软件包的核心配置文件[^1]。修改其属性主要通过调整CMake语法指令实现,以下是具体方法和常见应用场景: --- #### 一、基础属性配置 1. **设置C++标准** ```cmake set(CMAKE_CXX_STANDARD 14) set(CMAKE_CXX_STANDARD_REQUIRED ON) ``` 2. **定义包依赖** ```cmake find_package(catkin REQUIRED COMPONENTS roscpp std_msgs sensor_msgs ) ``` 3. **修改编译选项** ```cmake add_compile_options(-Wall -Wextra -O2) ``` --- #### 二、目标属性修改 1. **可执行文件属性** ```cmake add_executable(my_node src/my_node.cpp) target_link_libraries(my_node ${catkin_LIBRARIES}) target_include_directories(my_node PRIVATE include) ``` 2. **动态库属性** ```cmake add_library(my_library SHARED src/my_library.cpp) set_target_properties(my_library PROPERTIES VERSION 1.0.0 SOVERSION 1 ) ``` --- #### 三、安装路径设置 ```cmake install(TARGETS my_node my_library ARCHIVE DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} LIBRARY DESTINATION ${CATKIN_PACKAGE_LIB_DESTINATION} RUNTIME DESTINATION ${CATKIN_PACKAGE_BIN_DESTINATION} ) ``` --- #### 四、自定义编译选项 ```cmake option(ENABLE_DEBUG "Enable debug mode" OFF) if(ENABLE_DEBUG) add_compile_definitions(DEBUG_MODE) endif() ``` --- #### 关键注意事项 1. 必须包含`catkin_package()`声明包依赖关系[^2] 2. 使用`${catkin_LIBRARIES}`自动处理ROS依赖库的链接顺序 3. 修改后必须重新运行`catkin_make`或`catkin build`才能生效 ---
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值