官网教程:https://cmake.org/cmake-tutorial/linux
第一个简单的例子ubuntu
源文件:tutorial.cppwindows
1 // A simple program that computes the square root of a number 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <math.h> 5 int main (int argc, char *argv[]) 6 { 7 if (argc < 2) 8 { 9 fprintf(stdout,"Usage: %s number\n",argv[0]); 10 return 1; 11 } 12 double inputValue = atof(argv[1]); 13 double outputValue = sqrt(inputValue); 14 fprintf(stdout,"The square root of %g is %g\n", 15 inputValue, outputValue); 16 return 0; 17 }
同级目录下新建文件 CMakeLists.txt,内容以下:函数
cmake_minimum_required(VERSION 2.6) project (Tutorial) add_executable(Tutorial tutorial.cpp)
而后,若是是在linux下,在当前路径下打开Terminal,执行如下命令:ui
分别是:spa
一、cmake . # 这个命令会根据CMakeLists.txt生成makefile文件。code
二、make # 这个命令会自动查找makefile文件并编译生成可执行文件blog
三、执行可执行文件,从CMakeLists.txt文件中可知生成的可执行文件名为:Tutorial,函数功能是计算平方根,教程
执行命令 ./Tutorial 9 # 计算9的平方根获得3get
wmz@ubuntu:~/Desktop/testcmake$ cmake . -- Configuring done -- Generating done -- Build files have been written to: /home/wmz/Desktop/testcmake wmz@ubuntu:~/Desktop/testcmake$ make [100%] Built target Tutorial wmz@ubuntu:~/Desktop/testcmake$ ./Tutorial 9 The square root of 9 is 3
若是是在window下,执行cmake . 会生成一个vs 工程;执行make 会报错找不到make。缘由是在windows下我安装了cmake,因此能够使用cmake命令,
可是我在windows下没有安装make(MingWin是包含make的),我只安装了visual studio 2015,因此cmake . 会生成一个vs2015的工程。