【快速查询】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

、Required CMake Version (cmake_minimum_required)
、Package Name (project())
、Find other CMake/Catkin packages needed for build (find_package())
、Message/Service/Action Generators (add_message_files(), add_service_files(), add_action_files())
、Invoke message/service/action generation (generate_messages())
、Specify package build info export (catkin_package())
、Libraries/Executables to build (add_library()/add_executable()/target_link_libraries())
、Tests to build (catkin_add_gtest())
、Install rules (install())

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

cmake_minimum_required(VERSION 2.8.)

(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 directories, 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.
-- The CXX compiler identification is GNU 7.4.
-- 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

[CMAKE] 详解CMakeLists.txt文件的更多相关文章

  1. Cmake知识----编写CMakeLists.txt文件编译C/C++程序

    1.CMake编译原理 CMake是一种跨平台编译工具,比make更为高级,使用起来要方便得多.CMake主要是编写CMakeLists.txt文件,然后用cmake命令将CMakeLists.txt ...

  2. Cmake知识----编写CMakeLists.txt文件编译C/C++程序(转)

    1.CMake编译原理 CMake是一种跨平台编译工具,比make更为高级,使用起来要方便得多.CMake主要是编写CMakeLists.txt文件,然后用cmake命令将CMakeLists.txt ...

  3. Spring配置文件详解 – applicationContext.xml文件路径

    Spring配置文件详解 – applicationContext.xml文件路径 Java编程                 spring的配置文件applicationContext.xml的默 ...

  4. CMAKE 生成VS2008静态库工程 与 CMAKE使用,CMakeLists.txt编写总结

    cmake -G"Visual Studio 9 2008 Win64" 以上命令得用cd命令切换到顶层CMakeLists.txt的当前目录,才能生效 以下是CMakeLists ...

  5. Ros学习——Cmakelists.txt文件解读

    1.过程 .Required CMake Version (cmake_minimum_required) //CMake 需要的版本 .Package Name (project()) //#定义工 ...

  6. 简单CMakeLists.txt文件

    #CMakeLists.txt cmake_minimum_required(VERSION 2.8) project(server) #添加包含目录 include_directories(./in ...

  7. extern的使用详解(多文件编程)——C语言

    extern——关键字 extern是C语言中的一个关键字,一般用在变量名前或函数名前,作用是用来说明“此变量/函数是在别处定义的,要在此处引用”,extern这个关键字大部分读者应该是在变量的存储类 ...

  8. 用PHP实现浏览器点击下载各种格式文档的方法详解【txt apk等等】

    [[注:其他文件想设置成下载文件,和下面介绍的方法一致]] 由于现在的浏览器已经可以识别txt文档格式,如果只给txt文档做一个文字链接的话,点击后只是打开一个新窗口显示txt文件的内容,并不能实现点 ...

  9. ROS知识(8)----CMakeLists.txt文件编写的理解

    ROS(Indigo)编程必须要理解CMakeList.txt的编写规则,教程地址:catkin/CMakeLists.txt,官网有相关的教程,中文的翻译版本写的很不错,教程地址:ROS中的CMak ...

随机推荐

  1. nginx 重定向与反向代理

    server{    listen       80;    server_name  dianxi.test.net; #将地址重定向为新的ip地址    #rewrite  "^/env ...

  2. [LeetCode&Python] Problem 690. Employee Importance

    You are given a data structure of employee information, which includes the employee's unique id, his ...

  3. 20155219 2016-2017-2 《Java程序设计》第9周学习总结

    20155219 2016-2017-2 <Java程序设计>第9周学习总结 教材学习内容总结 JDBC入门 JDBC简介 1.JDBC是java联机数据库的标准规范,它定义了一组标准类与 ...

  4. day 023-python 包

    包 : 我 们创建的每个文件夹都可以被称之为包. 但是我们要注意, 在python2中规定.中包内必须存在 __init__.py文件.  python3可有可无,但一般要求写上.创建包的目的不是为了 ...

  5. pip3 install scrap报错

    mac系统 pip3 install scrapy 失败 No local packages or working download links found for incremental>=1 ...

  6. 迭代加深搜索(以Power Calculus POJ--3134 UVa--1374为例)

    本题代码如下: #include<cstdio> #include<cstring> #include<algorithm> using namespace std ...

  7. Android系统备忘1

    Android的4种模式 模式 功能 ADB调试system 正常使用 开发者模式开启usb调试recovery 备份,恢复模式 卡刷模式 twrp下开启ADB Sideloadfastboot 线刷 ...

  8. 当爬虫遇到js加密

    当爬虫遇到js加密 我们在做python爬虫的时候经常会遇到许多的反爬措施,js加密就是其中一种. 破解js加密的方法也有很多种: 1.直接驱动浏览器抓取数据,无视js加密. 2.找到本地加密的js代 ...

  9. (9)模板层 - templates(模板语言、语法、取值、过滤器、变量的使用)

    django的模板语言:DTL 模板语言的变量传入 这个是标签 {{ 变量名 }} {{ 变量名 }}   #模板语言的替换可以在模板中的任意位置生效 PS:通过 . 可以做深度查询 模板语言的过滤器 ...

  10. JPI中常使用的类介绍:

    Math类: java.lang包下的 final,不可被继承, 其中的方法和属性都是静态的 其构造方法私有化了,其他类不可以使用构造方法. 向上取整:Math.ceil(double d); 向下取 ...