cmake使用教程(一)-起步

【cmake系列使用教程】linux

cmake使用教程(一)-起步c++

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

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

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

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

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

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

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

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

示例程序地址: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
复制代码

便可看到输出内容:

相关文章
相关标签/搜索