cmake使用教程(一)-起步

60,902 阅读3分钟

【cmake系列使用教程】

cmake使用教程(一)-起步

cmake使用教程(二)-添加库

cmake使用教程(三)-安装、测试、系统自检

cmake使用教程(四)-文件生成器

cmake使用教程(五)-cpack生成安装包

cmake使用教程(六)-蛋疼的语法

cmake使用教程(七)-流程和循环

cmake使用教程(八)-macro和function

这个系列的文章翻译自官方cmake教程:cmake tutorial

示例程序地址:github.com/rangaofei/t…

不会仅仅停留在官方教程。本人作为一个安卓开发者,实在是没有linux c程序开发经验,望大佬们海涵。教程是在macos下完成,大部分linux我也测试过,有特殊说明的我会标注出来。本教程基于cmake-3.10.2,同时认为你已经安装好cmake。

基本语法

一个最基本的CmakeLists.txt文件最少需要包含以下三行:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial tutorial.cxx)

注意:cmake的语法支持大小、小写和大小写混合上边的代码中我们使用的cmake语法是小写的.

cmake_minimum_required
CMAKE_MINIMUM_REQUIRED
cmake_MINUMUM_required

上面三种写法是相同的,注意,只有系统指令是不区分大小写的,但是变量和字符串是区分大小写的。

创建一个tutorial.cxx文件,用来计算一个数字的平方根,内容如下:

// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
  if (argc < 2)
    {
    fprintf(stdout,"Usage: %s number\n",argv[0]);
    return 1;
    }
  double inputValue = atof(argv[1]);
  double outputValue = sqrt(inputValue);
  fprintf(stdout,"The square root of %g is %g\n",
          inputValue, outputValue);
  return 0;
}

这样就完成一个最简单的cmake程序。

构建程序

用cmake来编译这段代码,进入命令行执行内部构建命令(后边会讲外部构建):

cmake .

这是输出一系列的log信息

-- The C compiler identification is AppleClang 9.0.0.9000039
-- The CXX compiler identification is AppleClang 9.0.0.9000039
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/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: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/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: /Users/saka/Desktop/Tutorial/Step1

同时生成了三个文件CMakeCache.txtMakefilecmake_install.cmake和一个文件夹CmakeFiles,然后执行

make 

即可生成可执行程序Tutorial。在ubuntu或者centos上可能会提示找不到math.h文件,这时候我们需要在cmakeLists.txt文件中最后添加

target_link_libraries(Tutorial apue.a)

然后重新编译即可。需要删除刚才生成的额外的文件。

添加版本号

下面讲解如何为程序添加版本号和带有使用版本号的头文件。

set(KEY VALUE)接受两个参数,用来声明变量。在camke语法中使用KEY并不能直接取到VALUE,必须使用${KEY}这种写法来取到VALUE

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
# The version number.
set (Tutorial_VERSION_MAJOR 1)
set (Tutorial_VERSION_MINOR 0)
 
# configure a header file to pass some of the CMake settings
# to the source code
configure_file (
  "${PROJECT_SOURCE_DIR}/TutorialConfig.h.in"
  "${PROJECT_BINARY_DIR}/TutorialConfig.h"
  )
 
# add the binary tree to the search path for include files
# so that we will find TutorialConfig.h
include_directories("${PROJECT_BINARY_DIR}")
 
# add the executable
add_executable(Tutorial tutorial.cxx)

配置文件将会被写入到可执行文件目录下,所以我们的项目必须包含这个文件夹来使用这些配置头文件。我们需要在工程目录下新建一个TutorialConfig.h.in,内容如下:

// the configured options and settings for Tutorial
#define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@
#define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@

上面的代码中的@Tutorial_VERSION_MAJOR@@Tutorial_VERSION_MINOR@将会被替换为CmakeLists.txt中的1和0。 然后修改Tutorial.cxx文件如下,用来在不输入额外参数的情况下输出版本信息:

// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "TutorialConfig.h"
 
int main (int argc, char *argv[])
{
  if (argc < 2)
    {
    fprintf(stdout,"%s Version %d.%d\n",
            argv[0],
            Tutorial_VERSION_MAJOR,
            Tutorial_VERSION_MINOR);
    fprintf(stdout,"Usage: %s number\n",argv[0]);
    return 1;
    }
  double inputValue = atof(argv[1]);
  double outputValue = sqrt(inputValue);
  fprintf(stdout,"The square root of %g is %g\n",
          inputValue, outputValue);
  return 0;
}

然后执行

cmake .
make
./Tutorial

即可看到输出内容: