[cmake 系列使用教程]
cmake 使用教程 (一)- 起步 cmake 使用教程 (二)- 添加库
这个系列的文章翻译自官方 cmake 教程: cmake tutorial , 但是又不会仅仅停留在官方教程. 本人作为一个安卓开发者, 实在是没有 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.txt,Makefile,cmake_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
即可看到输出内容:
来源: https://juejin.im/post/5a6f32e86fb9a01ca6031230