GNU开发工具——CMake快速入门

GNU开发工具——CMake快速入门

1、CMake简介

不一样Make工具,如GNU Make、QT的qmake、微软的MS nmake、BSD Make(pmake)等,遵循着不一样的规范和标准,所执行的Makefile格式也不一样。若是软件想跨平台,必需要保证可以在不一样平台编译。而若是使用Make工具,必须为不一样的Make工具编写不一样的Makefile。
CMake是一个比Make工具更高级的编译配置工具,是一个跨平台的、开源的构建系统(BuildSystem)。CMake容许开发者编写一种平台无关的CMakeList.txt文件来定制整个编译流程,而后再根据目标用户的平台进一步生成所需的本地化Makefile和工程文件,如:为Unix平台生成Makefile文件(使用GCC编译),为Windows MSVC生成projects/workspaces(使用VS IDE编译)或Makefile文件(使用nmake编译)。使用CMake做为项目架构系统的知名开源项目有VTK、ITK、KDE、OpenCV、OSG等。数组

2、CMake管理工程

在Linux平台下使用CMake生成Makefile并编译的流程以下:
A、编写CMake配置文件CMakeLists.txt
B、执行命令cmake PATH生成Makefile,PATH是CMakeLists.txt所在的目录。
C、使用make命令进行编译。架构

3、单个源文件工程

一、源文件编写

假设项目test中只有一个main.cpp源文件,程序用途是计算一个数的指数幂。ide

#include <stdio.h>
#include <stdlib.h>
/**
 * power - Calculate the power of number.
 * @param base: Base value.
 * @param exponent: Exponent value.
 *
 * @return base raised to the power exponent.
 */
double power(double base, int exponent)
{
    int result = base;
    int i;

    if (exponent == 0)
    {
        return 1;
    }

    for(i = 1; i < exponent; ++i)
    {
        result = result * base;
    }
    return result;
}
int main(int argc, char *argv[])
{
    if(argc < 3)
    {
        printf("Usage: %s base exponent \n", argv[0]);
        return 1;
    }
    double base = atof(argv[1]);
    int exponent = atoi(argv[2]);
    double result = power(base, exponent);
    printf("%g ^ %d is %g\n", base, exponent, result);
    return 0;
}

二、编写CMakeLists.txt

在main.cpp源文件目录test下编写CMakeLists.txt文件。函数

#CMake最低版本号要求
cmake_minimum_required (VERSION 2.8)
#项目信息
project (demo)
#指定生成目标
add_executable(demomain.cpp)

CMakeLists.txt由命令、注释和空格组成,其中命令是不区分大小写。符号#后的内容被认为是注释。命令由命令名称、小括号和参数组成,参数之间使用空格进行间隔。
本例中CMakeLists.txt文件的命令以下:
cmake_minimum_required:指定运行本配置文件所需的CMake的最低版本;
project:参数值是demo,表示项目的名称是demo。
add_executable:将名为main.cpp的源文件编译成一个名称为demo的可执行文件。工具

三、编译工程

在源码根目录下建立一个build目录,进入build目录,执行cmake ..,生成Makefile,再使用make命令编译获得demo可执行文件。
一般,建议在源码根目录下建立一个独立的build构建编译目录,将构建过程产生的临时文件等文件与源码隔离,避免源码被污染。开发工具

4、单目录多源文件工程

一、源文件编写

假如把power函数单独写进一个名为MathFunctions.cpp的源文件里,使得这个工程变成以下的形式:
GNU开发工具——CMake快速入门
MathFunctions.h文件:测试

/**
 * power - Calculate the power of number.
 * @param base: Base value.
 * @param exponent: Exponent value.
 *
 * @return base raised to the power exponent.
 */

double power(double base, int exponent);

MathFunctions.cpp文件:ui

double power(double base, int exponent)
{
    int result = base;
    int i;

    if (exponent == 0)
    {
        return 1;
    }

    for(i = 1; i < exponent; ++i)
    {
        result = result * base;
    }
    return result;
}

main.cpp文件:this

#include <stdio.h>
#include <stdlib.h>
#include "MathFunctions.h"

int main(int argc, char *argv[])
{
    if(argc < 3)
    {
        printf("Usage: %s base exponent \n", argv[0]);
        return 1;
    }

    double base = atof(argv[1]);
    int exponent = atoi(argv[2]);
    double result = power(base, exponent);
    printf("%g ^ %d is %g\n", base, exponent, result);

    return 0;
}

二、编写CMakeLists.txt

#CMake最低版本号要求
cmake_minimum_required(VERSION 2.8)
#项目信息
project(demo)
#指定生成目标
add_executable(demomain.cpp MathFunctions.cpp)

add_executable命令中增长了一个MathFunctions.cpp源文件,但若是源文件不少,可使用aux_source_directory命令,aux_source_directory命令会查找指定目录下的全部源文件,而后将结果存进指定变量名。其语法以下:
aux_source_directory(dir variable)
修改后CMakeLists.txt以下:spa

#CMake最低版本号要求
cmake_minimum_required(VERSION 2.8)
# 项目信息
project(demo)
#查找当前目录下的全部源文件
#并将名称保存到DIR_SRCS变量
aux_source_directory(. DIR_SRCS)
#指定生成目标
add_executable(demo${DIR_SRCS})

CMake会将当前目录全部源文件的文件名赋值给变量DIR_SRCS ,再指示变量DIR_SRCS中的源文件须要编译成一个名称为demo的可执行文件。

5、多文件多目录工程

一、源码文件编写

建立一个math目录,将MathFunctions.h和MathFunctions.cpp文件移动到math目录下。在工程目录根目录test和子目录math里各编写一个CMakeLists.txt文件,能够先将math目录里的文件编译成静态库再由main函数调用。
GNU开发工具——CMake快速入门
math子目录:
MathFunctions.h文件:

/**
 * power - Calculate the power of number.
 * @param base: Base value.
 * @param exponent: Exponent value.
 *
 * @return base raised to the power exponent.
 */

double power(double base, int exponent);

MathFunctions.cpp文件:

double power(double base, int exponent)
{
    int result = base;
    int i;

    if (exponent == 0)
    {
        return 1;
    }

    for(i = 1; i < exponent; ++i)
    {
        result = result * base;
    }
    return result;
}

根目录源文件:

#include <stdio.h>
#include <stdlib.h>
#include "math/MathFunctions.h"

int main(int argc, char *argv[])
{
    if(argc < 3)
    {
        printf("Usage: %s base exponent \n", argv[0]);
        return 1;
    }

    double base = atof(argv[1]);
    int exponent = atoi(argv[2]);
    double result = power(base, exponent);
    printf("%g ^ %d is %g\n", base, exponent, result);

    return 0;
}

二、CMakeLists.txt文件编写

根目录的CMakeLists.txt文件:

# CMake最低版本号要求
cmake_minimum_required(VERSION 2.8)
# 项目信息
project(demo)
#查找当前目录下的全部源文件
#并将名称保存到DIR_SRCS变量
aux_source_directory(. DIR_SRCS)
#添加math子目录
add_subdirectory(math)
#指定生成目标 
add_executable(demo${DIR_SRCS})
# 添加连接库
target_link_libraries(demoMathFunctions)

add_subdirectory命令指明本工程包含一个子目录math,math目录下的 CMakeLists.txt文件和源代码也会被处理 。target_link_libraries命令指明可执行文件demo须要链接一个名为MathFunctions的连接库 。
math子目录的CMakeLists.txt文件:

#查找当前目录下的全部源文件
#并将名称保存到DIR_LIB_SRCS变量
aux_source_directory(. DIR_LIB_SRCS)
#生成连接库
add_library(MathFunctions ${DIR_LIB_SRCS})

add_library命令将math目录中的源文件编译为静态连接库。

6、自定义编译选项

一、自定义编译选项简介

CMake容许为工程增长编译选项,从而能够根据用户的环境和需求选择最合适的编译方案。
例如,能够将MathFunctions库设为一个可选的库,若是该选项为ON ,就使用MathFunctions库定义的数学函数来进行运算,不然就调用标准库中的数学函数库。

二、CMakeLists 文件编写

在根目录的CMakeLists.txt文件指定自定义编译选项:

# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (demo)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# 加入一个配置头文件,用于处理 CMake 对源码的设置
configure_file (
  "${PROJECT_SOURCE_DIR}/config.h.in"
  "${PROJECT_BINARY_DIR}/config.h"
  )
# 是否使用本身的MathFunctions库
option (USE_MYMATH
           "Use provided math implementation" ON)
# 是否加入 MathFunctions 库
if (USE_MYMATH)
  include_directories ("${PROJECT_SOURCE_DIR}/math")
  add_subdirectory (math)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (USE_MYMATH)

# 查找当前目录下的全部源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 指定生成目标
add_executable (demo${DIR_SRCS})
target_link_libraries (demo  ${EXTRA_LIBS})

configure_file命令用于加入一个配置头文件config.h,config.h文件由CMake从config.h.in生成,经过预约义一些参数和变量来控制代码的生成。
option命令添加了一个USE_MYMATH选项,而且默认值为ON。
根据USE_MYMATH变量的值来决定是否使用本身编写的MathFunctions库。

三、修改源文件的调用

修改main.cpp文件,让其根据USE_MYMATH的预约义值来决定是否调用标准库仍是MathFunctions库。

#include <cstdio>
#include <cstdlib>
#include <config.h>

#ifdef USE_MYMATH
#include <MathFunctions.h>
#else
#include <cmath>
#endif

int main(int argc, char *argv[])
{
    if (argc < 3)
    {
        printf("Usage: %s base exponent \n", argv[0]);
        return 1;
    }

    double base = atof(argv[1]);
    int exponent = atoi(argv[2]);

#ifdef USE_MYMATH
    printf("Now we use our own Math library. \n");
    double result = power(base, exponent);
#else
    printf("Now we use the standard library. \n");
    double result = pow(base, exponent);
#endif

    printf("%g ^ %d is %g\n", base, exponent, result);
    return 0;
}

四、编写config.h.in文件

main.cpp文件包含了一个config.h文件,config.h文件预约义了USE_MYMATH 的值。但不会直接编写config.h文件,为了方便从CMakeLists.txt中导入配置,一般编写一个config.h.in文件,内容以下:
#cmakedefine USE_MYMATH
CMake会自动根据CMakeLists.txt配置文件中的设置自动生成config.h文件。

五、编译工程

修改CMakeLists.txt文件,USE_MYMATH为OFF,使用标准库。

# 是否使用本身的MathFunctions库
option (USE_MYMATH
           "Use provided math implementation" OFF)

在build目录下cmake ..,make,执行程序:
GNU开发工具——CMake快速入门

7、安装和测试

一、定制安装规则

在math/CMakeLists.txt文件指定MathFunctions库的安装规则:

#指定MathFunctions库的安装路径
install(TARGETS MathFunctions DESTINATION bin)
install(FILES MathFunctions.h DESTINATION include)

修改根目录的CMakeLists.txt文件指定目标文件的安装规则:

#指定安装路径
install(TARGETS test DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/config.h"
         DESTINATION include)

经过对安装规则的定制,生成的目标文件和MathFunctions函数库 libMathFunctions.o文件将会被拷贝到/usr/local/bin中,而MathFunctions.h和生成的config.h文件则会被复制到/usr/local/include中。
/usr/local是默认安装到的根目录,能够经过修改 CMAKE_INSTALL_PREFIX 变量的值来指定文件应该拷贝到哪一个根目录。

二、为工程添加测试

CMake提供了一个CTest测试工具。在项目根目录的CMakeLists.txt文件中调用一系列的add_test 命令。

#启用测试
     enable_testing()
     #测试程序是否成功运行
     add_test(test_run demo 5 2)
     #测试帮助信息是否能够正常提示
     add_test(test_usage demo)
     set_tests_properties(test_usage
       PROPERTIES PASS_REGULAR_EXPRESSION "Usage: .* base exponent")
     #测试5的平方
     add_test(test_5_2 demo 5 2)
     set_tests_properties(test_5_2
      PROPERTIES PASS_REGULAR_EXPRESSION "is 25")
     #测试10的5次方
     add_test(test_10_5 demo 10 5)
     set_tests_properties(test_10_5
      PROPERTIES PASS_REGULAR_EXPRESSION "is 100000")
     #测试2的10次方
     add_test(test_2_10 demo 2 10)
     set_tests_properties(test_2_10
      PROPERTIES PASS_REGULAR_EXPRESSION "is 1024")

GNU开发工具——CMake快速入门
第一个测试test_run用来测试程序是否成功运行并返回0值。剩下的三个测试分别用来测试 5 的 平方、10 的 5 次方、2 的 10 次方是否都能获得正确的结果。其中PASS_REGULAR_EXPRESSION用来测试输出是否包含后面跟着的字符串。
若是要测试更多的输入数据,能够经过编写宏来实现:

# 启用测试
     enable_testing()

     # 测试程序是否成功运行
     add_test (test_run demo 5 2)

     # 测试帮助信息是否能够正常提示
     add_test (test_usage demo)
     set_tests_properties (test_usage
       PROPERTIES PASS_REGULAR_EXPRESSION "Usage: .* base exponent")

     # 测试 5 的平方
     # add_test (test_5_2 Demo 5 2)

     # set_tests_properties (test_5_2
     #  PROPERTIES PASS_REGULAR_EXPRESSION "is 25")

     # 测试 10 的 5 次方
     # add_test (test_10_5 Demo 10 5)

     # set_tests_properties (test_10_5
     #  PROPERTIES PASS_REGULAR_EXPRESSION "is 100000")

     # 测试 2 的 10 次方
     # add_test (test_2_10 Demo 2 10)

     # set_tests_properties (test_2_10
     #  PROPERTIES PASS_REGULAR_EXPRESSION "is 1024")

     # 定义一个宏,用来简化测试工做
     macro (do_test arg1 arg2 result)
       add_test (test_${arg1}_${arg2} demo ${arg1} ${arg2})
       set_tests_properties (test_${arg1}_${arg2}
         PROPERTIES PASS_REGULAR_EXPRESSION ${result})
     endmacro (do_test)

     # 利用 do_test 宏,测试一系列数据
     do_test (5 2 "is 25")
     do_test (10 5 "is 100000")
     do_test (2 10 "is 1024")

GNU开发工具——CMake快速入门

8、GDB支持

让CMake支持gdb的设置只须要指定Debug模式下开启-g选项:

set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

生成的程序能够直接使用gdb来调试。

9、添加环境检查

使用平台相关的特性时,须要对系统环境作检查。检查系统是否自带pow函数,若是有pow函数,就使用;不然使用自定义的power函数。

一、添加 CheckFunctionExists 宏

首先在顶层CMakeLists.txt文件中添加CheckFunctionExists.cmake 宏,并调用check_function_exists命令测试连接器是否可以在连接阶段找到 pow函数。

#检查系统是否支持 pow 函数
include (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
check_function_exists (pow HAVE_POW)
check_function_exists须要放在configure_file命令前。

二、预约义相关宏变量

修改 config.h.in 文件,预约义相关的宏变量。

// does the platform provide pow function?
#cmakedefine HAVE_POW

三、在代码中使用宏和函数

修改 main.cpp文件 ,在代码中使用宏和函数。

#include <stdio.h>
#include <stdlib.h>
#include <config.h>

#ifdef HAVE_POW
#include <math.h>
#else
#include <MathFunctions.h>
#endif

int main(int argc, char *argv[])
{
    if (argc < 3)
    {
        printf("Usage: %s base exponent \n", argv[0]);
        return 1;
    }
    double base = atof(argv[1]);
    int exponent = atoi(argv[2]);

#ifdef HAVE_POW
    printf("Now we use the standard library. \n");
    double result = pow(base, exponent);
#else
    printf("Now we use our own Math library. \n");
    double result = power(base, exponent);
#endif

    printf("%g ^ %d is %g\n", base, exponent, result);
    return 0;
}

10、添加版本号

修改顶层CMakeLists.txt文件,在project命令后分别指定当前的项目的主版本号和副版本号。

# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (demo)

set (Demo_VERSION_MAJOR 1)
set (Demo_VERSION_MINOR 0)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

#检查系统是否支持 pow 函数
include (${CMAKE_ROOT}/Modules/CheckFunctionExists.cmake)
check_function_exists (pow HAVE_POW)

# 加入一个配置头文件,用于处理 CMake 对源码的设置
configure_file (
  "${PROJECT_SOURCE_DIR}/config.h.in"
  "${PROJECT_BINARY_DIR}/config.h"
  )

# 是否加入 MathFunctions 库
if (NOT HAVE_POW)
  include_directories ("${PROJECT_SOURCE_DIR}/math")
  add_subdirectory (math)
  set (EXTRA_LIBS ${EXTRA_LIBS} MathFunctions)
endif (NOT HAVE_POW)

# 查找当前目录下的全部源文件
# 并将名称保存到 DIR_SRCS 变量
aux_source_directory(. DIR_SRCS)
# 指定生成目标
add_executable (demo ${DIR_SRCS})
target_link_libraries (demo  ${EXTRA_LIBS})

#指定安装路径
install(TARGETS demo DESTINATION bin)
install(FILES "${PROJECT_BINARY_DIR}/config.h"
         DESTINATION include)

     # 启用测试
     enable_testing()

     # 测试程序是否成功运行
     add_test (test_run demo 5 2)

     # 测试帮助信息是否能够正常提示
     add_test (test_usage demo)
     set_tests_properties (test_usage
       PROPERTIES PASS_REGULAR_EXPRESSION "Usage: .* base exponent")

     # 测试 5 的平方
     # add_test (test_5_2 Demo 5 2)

     # set_tests_properties (test_5_2
     #  PROPERTIES PASS_REGULAR_EXPRESSION "is 25")

     # 测试 10 的 5 次方
     # add_test (test_10_5 Demo 10 5)

     # set_tests_properties (test_10_5
     #  PROPERTIES PASS_REGULAR_EXPRESSION "is 100000")

     # 测试 2 的 10 次方
     # add_test (test_2_10 Demo 2 10)

     # set_tests_properties (test_2_10
     #  PROPERTIES PASS_REGULAR_EXPRESSION "is 1024")

     # 定义一个宏,用来简化测试工做
     macro (do_test arg1 arg2 result)
       add_test (test_${arg1}_${arg2} demo ${arg1} ${arg2})
       set_tests_properties (test_${arg1}_${arg2}
         PROPERTIES PASS_REGULAR_EXPRESSION ${result})
     endmacro (do_test)

     # 利用 do_test 宏,测试一系列数据
     do_test (5 2 "is 25")
     do_test (10 5 "is 100000")
     do_test (2 10 "is 1024")

分别指定当前的项目的主版本号和副版本号。
为了在代码中获取版本信息,能够修改 config.h.in 文件,添加两个预约义变量:

// the configured options and settings for Tutorial
#define Demo_VERSION_MAJOR @Demo_VERSION_MAJOR@
#define Demo_VERSION_MINOR @Demo_VERSION_MINOR@

// does the platform provide pow function?
#cmakedefine HAVE_POW

直接在源码中使用:

#include <stdio.h>
#include <stdlib.h>
#include <config.h>

#ifdef HAVE_POW
#include <math.h>
#else
#include <MathFunctions.h>
#endif

int main(int argc, char *argv[])
{
    if (argc < 3)
    {
        // print version info
        printf("%s Version %d.%d\n",
               argv[0],
               Demo_VERSION_MAJOR,
               Demo_VERSION_MINOR);
        printf("Usage: %s base exponent \n", argv[0]);
        return 1;
    }

    double base = atof(argv[1]);
    int exponent = atoi(argv[2]);

#ifdef HAVE_POW
    printf("Now we use the standard library. \n");
    double result = pow(base, exponent);
#else
    printf("Now we use our own Math library. \n");
    double result = power(base, exponent);
#endif

    printf("%g ^ %d is %g\n", base, exponent, result);
    return 0;
}

11、生成安装包

一、增长CPack模块

CMake提供了一个专门用于打包的工具CPack,用于配置生成各类平台上的安装包,包括二进制安装包和源码安装包。
首先在顶层的CMakeLists.txt文件尾部添加下面几行:

# 构建一个 CPack 安装包
include (InstallRequiredSystemLibraries)
set (CPACK_RESOURCE_FILE_LICENSE
  "${CMAKE_CURRENT_SOURCE_DIR}/License.txt")
set (CPACK_PACKAGE_VERSION_MAJOR "${Demo_VERSION_MAJOR}")
set (CPACK_PACKAGE_VERSION_MINOR "${Demo_VERSION_MINOR}")
include (CPack)

导入InstallRequiredSystemLibraries模块,便于导入CPack模块;
设置一些CPack相关变量,包括版权信息和版本信息
导入CPack模块。
在顶层目录下建立License.txt文件内如以下:

The MIT License (MIT)

Copyright (c) 2018 Scorpio Studio

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

二、生成安装包

生成二进制安装包:
cpack -C CPackConfig.cmake
生成源码安装包:
cpack -C CPackSourceConfig.cmake上述两个命令都会在目录下建立3个不一样格式的二进制包文件:demo-1.0.1-Linux.tar.gzdemo-1.0.1-Linux.tar.Zdemo-1.0.1-Linux.sh3个二进制包文件所包含的内容是彻底相同的。

相关文章
相关标签/搜索