官网教程:https://cmake.org/cmake-tutorial/
第一个简单的例子
源文件:tutorial.cpp
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,执行以下命令:
分别是:
1、cmake . # 这个命令会根据CMakeLists.txt生成makefile文件。
2、make # 这个命令会自动查找makefile文件并编译生成可执行文件
3、执行可执行文件,从CMakeLists.txt文件中可知生成的可执行文件名为:Tutorial,函数功能是计算平方根,
执行命令 ./Tutorial 9 # 计算9的平方根得到3
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的工程。
转载于:https://www.cnblogs.com/juluwangshier/p/11609100.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)