【快速查询】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. Oracle密码概要文件,密码过期时间180天修改为3天,相关用户密码是否过期

    #Oracle用户密码,概要文件修改测试 #默认的用户使用概要文件,默认概要文件密码过期时间参数180天,修改为3天,对于老的用户来说,是密码过期,还是未发生改变, 对于新用户来说,新设置的密码过期时 ...

  2. Linux关闭透明大页配置

      一.为何要关闭透明大页 A--MOS获取 . #翻译 由于透明超大页面已知会导致意外的节点重新启动并导致RAC出现性能问题,因此Oracle强烈建议禁用透明超大页面. 另外,即使在单实例数据库环境 ...

  3. 回收机制GC

    .NET 之 垃圾回收机制GC 一.GC的必要性 1.应用程序对资源操作,通常简单分为以下几个步骤:为对应的资源分配内存 → 初始化内存 → 使用资源 → 清理资源 → 释放内存. 2.应用程序对资源 ...

  4. 栈与队列(Stack and Queue)

    1.定义 栈:后进先出(LIFO-last in first out):最后插入的元素最先出来. 队列:先进先出(FIFO-first in first out):最先插入的元素最先出来. 2.用数组 ...

  5. 在学习linux基础入门时的一些问题总结(1)

    本周在实验楼完成了<linux基础入门>的21个实验,虽然之前已经学习过linux的相关课程,对linux下的命令也有一些了解和实践,但完成这21个实验以及35个练习题仍然遇到了许多的问题 ...

  6. debian 配置linuxptp 软件时间戳

    编程之路刚刚开始,错误难免,希望大家能够指出. ntp,ptp,ntp,ptp 本文只说软件时间戳 先上几个推荐的网址,可以更好的了解ptp: https://docs.fedoraproject.o ...

  7. Go Example--通道遍历

    package main import ( "fmt" ) func main() { queue := make(chan string, 2) queue <- &quo ...

  8. linux忘记root密码

    在选择系统界面选中要修改的系统(我的就是默认的第一个),按e建进入修改,在修改界面一直下到文件末尾,在末尾前一行左右,找到UTF-8那一行,在这一行敲一个空格,然后打init=/bin/sh 修改完成 ...

  9. AspNetCore+Swagger 生成Model描述

    AspNetCore+Swagger 生成Model 描述 前言: 本篇文章实现是基于上一篇文章,进下补充:多余的就不多说了,只是为了实现Model的描述生成:有兴趣的可以结合上一篇的进行实现:如有更 ...

  10. oracle-db安装

    在LINUX平台上手动创建多个实例(oracle11g) http://blog.csdn.net/sunchenglu7/article/details/39676659 安装完桌面与数据库软件后, ...