概述
本文通过一个实例入门CMake。
正文
首先创建一个目录test,目录下包含5个文件如下:
test$ tree
.
├── a.cpp
├── b.cpp
├── c.cpp
├── head.h
└── main.cpp
其中a.cpp内容为:
#include <stdio.h>
#include "head.h"
void runa()
{
printf("program a!!!\r\n");
}
b.cpp内容为:
#include <stdio.h>
#include "head.h"
void runb()
{
printf("program b!!!\r\n");
}
c.cpp内容为:
#include <stdio.h>
#include "head.h"
void runc()
{
printf("program c!!!\r\n");
}
head.h内容为:
#ifndef __HEAD_H
#define __HEAD_H
void runa();
void runb();
void runc();
#endif
main.cpp内容为:
#include "head.h"
int main()
{
runa();
runb();
runc();
return 0;
}
用gcc编译指令为:
test$ gcc -o app main.cpp a.cpp b.cpp c.cpp
可以看到test目录下生成了一个app文件,运行app可以看到其打印内容如下:
test$ ./app
program a!!!
program b!!!
program c!!!
接下来用CMake的方式来生成app文件,
首先在test文件夹下新建一个文件命令为CMakeLists.txt
,CMakeLists.txt是CMake构建系统使用的核心文件。
内容如下:
add_executable(app main.cpp a.cpp b.cpp c.cpp)
add_executable
定义工程生成一个可执行程序,使用方式为:
add_executable(可执行程序名 源文件名称)
源文件可以是一个也可以是多个,多个可以用空格或者;隔开,如:
#方法1
add_executable(app main.cpp a.cpp b.cpp c.cpp)
#方法2
add_executable(app main.cpp;a.cpp;b.cpp;c.cpp)
CMakeLists.txt内容保存后,接下来执行CMake
指令为 cmake CMakeLists.txt文件所在路径
执行结果如下:
test$ cmake .
CMake Warning (dev) in CMakeLists.txt:
No project() command is present. The top-level CMakeLists.txt file must
contain a literal, direct call to the project() command. Add a line of
code such as
project(ProjectName)
near the top of the file, but after cmake_minimum_required().
CMake is pretending there is a "project(Project)" command on the first
line.
This warning is for project developers. Use -Wno-dev to suppress it.
CMake Warning (dev) in CMakeLists.txt:
cmake_minimum_required() should be called prior to this top-level project()
call. Please see the cmake-commands(7) manual for usage documentation of
both commands.
This warning is for project developers. Use -Wno-dev to suppress it.
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/cc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done (1.0s)
-- Generating done (0.0s)
-- Build files have been written to: /home/mrw/桌面/learnCMake/test
可以看到生成了一推文件,其中包括Makefile
,然后运行make
test$ make
[ 20%] Building CXX object CMakeFiles/app.dir/main.cpp.o
[ 40%] Building CXX object CMakeFiles/app.dir/a.cpp.o
[ 60%] Building CXX object CMakeFiles/app.dir/b.cpp.o
[ 80%] Building CXX object CMakeFiles/app.dir/c.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
就生成了最后的可执行文件app
。
此时test文件夹中包含内容如下:
tree -L 1
.
├── a.cpp
├── app
├── b.cpp
├── c.cpp
├── CMakeCache.txt
├── CMakeFiles
├── cmake_install.cmake
├── CMakeLists.txt
├── head.h
├── main.cpp
└── Makefile
可以看到CMake生成了一些文件,使得项目目录看起来比较混乱,如果在test文件夹下生成一个build文件夹,然后在build文件夹中执行cmake,如下:
test$ mkdir build
test$ cd build
test/build$ cmake ..
此时test文件夹中内容如下:
test$ tree -L 2
.
├── a.cpp
├── b.cpp
├── build
│ ├── CMakeCache.txt
│ ├── CMakeFiles
│ ├── cmake_install.cmake
│ └── Makefile
├── c.cpp
├── CMakeLists.txt
├── head.h
└── main.cpp
可以看到cmake生成的内容都放在了build文件夹中。
总结
1、cmake最重要的是文件CMakeLists.txt
;
2、cmake执行指令为 cmake CMakeLists.txt文件所在路径
;
3、CMakeLists.txt中add_executable(可执行程序名 源文件名称)
指令用于生成可执行文件;
4、cmake执行完之后会在执行目录下生成一堆文件,其中包括Makefile
;
5、在Makefile
所在路径下执行make
,即可生成可执行文件。