CMake 入门教程 (一)
本文为 CMake 官方教程翻译文档。
英文原文地址:http://www.cmake.org/cmake/help/cmake_tutorial.html
对应源码下载地址:
http://public.kitware.com/cgi-bin/viewcvs.cgi/Tests/Tutorial.tar.gz?root=CMake&view=tar
第一步:一个基础入门例子
大多数的源码都会编译为可执行文件,对于简单的工程只需要两行的 CMakeLists.txt 文件
就行了,我们从这里开始我们的 CMake 之旅。
CMakeLists.txt 文件如下:
cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial tutorial.cxx)
注意到这个例子里面全部命令使用小写,CMake 支持大写、小写或者混合大小写命令。
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",argv[0]);
return 1;
}
double inputValue = atof(argv[1]);
double outputValue = sqrt(inputValue);
fprintf(stdout,"The square root of %g is %g",
inputValue, outputValue);
return 0;
}
添加一个版本号和配置头文件
我们要添加的第一个功能就是为我们的可运行程序和项目提供一个版本号。你可以在源码中
专门做这件事,CMakeLists 文件提供更灵活的方式来实现。添加了版本号的 CMakeLists
文件如下:
评论0