1、单个源文件
例如,假设现在我们的项目中只有一个源文件 add.c ,该程序的用途是计算两数之和。
# Ly @ centos in ~/Project [10:55:23]
$ ls
add.c
步骤1:编写CMakeLists.txt
首先编写 CMakeLists.txt 文件,并保存在与 add.c 源文件同个目录下。文件内容如下:
# CMake 最低版本号要求
cmake_minimum_required (VERSION 2.8)
# 项目信息
project (AddFun)
# 指定生成目标
add_executable(add.exe add.c)
对于上面的 CMakeLists.txt 文件,依次出现了几个命令:
1、cmake_minimum_required:
指定运行此配置文件所需的 CMake 的最低版本;
2、project:
参数值是 AddFun,该命令表示项目的名称是 AddFun ;
3、add_executable:
将名为 add.c 的源文件编译成一个名称为 add.exe 的可执行文件。
此时,当前工作目录下有两个文件:add.c CMakeLists.txt
步骤2:执行 cmake .
之后,在当前目录执行 cmake .
# Ly @ centos in ~/Project [10:55:23]
$ cmake .
-- The C compiler identification is GNU x.x.x
-- The CXX compiler identification is GNU x.x.x
-- Check for working C compiler: /user/bin/cc
......//中间部分信息省略
-- Build files have been written to: /home/xxx/xxx/Project
此时 cmake . 完成,得到 Makefile
# Ly @ centos in ~/Project [10:55:23]
$ ls
add.c CMakeFiles CMakeLists.txt
CMakeCache.txt cmake_install.cmake Makefile
步骤3:使用 make 命令编译得到 AddFun 可执行文件
# Ly @ centos in ~/Project [10:55:23]
$ make
Scanning dependencies of target add.exe
[100%] Building C object CMakeFiles/add.exe.dir/add.c.o
Linking C executable add.exe
[100%

最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



