说明
是什么:高级编译配置工具
多人用不同语言或者编译器开发一个项目,最终要输出一个可执行文件货共享库(dll,so等) 需要所有操作都是通过编译XMakeLists.txt来完成的
学习目的是将来处理大型C/C++/JAVA开发项目
CMake安装
1、绝大多数linux系统已经安装了CMake
安装指令 yum install cmake
装好测试:cmake
CMake一个HelloWorld
1、HelloWorld
#main.cpp #include<iostream> int main() { std::cout<<"hello world"<<std::endl; }
2、写CMakeList
创建文件夹编写helloworld并编译
mkdir cmake01
cd cmake01
vim main.cpp
c++ main.cpp
PROJECT(HELLO)
SET(SRC_LIST main.cpp)
MESSAGE(STATUS "This is BINARY dir" ${HELLO_BINARY_DIR})
MESSAGE(STATUS "This is SOURCE dir" ${HELLO_SOURCE_DIR})
ADD_EXECUTABLE (hello ${SREC_LIST})
用当前CMakeLists编译 main.cpp
ll #查看当前路径下文件
camke . #对当前目录下进行cmake编译
#ll查看时 将会生成一个执行文件 makefile的文件 如下所示
drwxrwxr-x 3 xxxxx xxxxx 4096 Mar 28 04:59 ./
drwxrwxr-x 4 xxxxx xxxxx 4096 Mar 28 03:46 ../
-rw-rw-r-- 1 xxxxx xxxxx 12591 Mar 28 04:59 CMakeCache.txt
drwxrwxr-x 5 xxxxx xxxxx 4096 Mar 28 04:59 CMakeFiles/
-rw-rw-r-- 1 xxxxx xxxxx 1508 Mar 28 04:59 cmake_install.cmake
-rw-rw-r-- 1 xxxxx xxxxx 186 Mar 28 04:59 CMakeLists.txt
-rw-rw-r-- 1 xxxxx xxxxx 97 Mar 28 04:54 main.cpp
-rw-rw-r-- 1 xxxxx xxxxx 4728 Mar 28 04:59 Makefile
接下来使用make进行编译,编译完成时,会在当前目录生成一个hello 可执行文件
xxxxx@ubuntu:~/LinuxC++/230328C++$ make
Scanning dependencies of target hello
[ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o
[100%] Linking CXX executable hello
[100%] Built target hello
xxxxx@ubuntu:~/LinuxC++/230328C++$ ll
total 60
drwxrwxr-x 3 xxxxx xxxxx 4096 Mar 28 05:02 ./
drwxrwxr-x 4 xxxxx xxxxx 4096 Mar 28 03:46 ../
-rw-rw-r-- 1 xxxxx xxxxx 12591 Mar 28 04:59 CMakeCache.txt
drwxrwxr-x 5 xxxxx xxxxx 4096 Mar 28 05:02 CMakeFiles/
-rw-rw-r-- 1 xxxxx xxxxx 1508 Mar 28 04:59 cmake_install.cmake
-rw-rw-r-- 1 xxxxx xxxxx 186 Mar 28 04:59 CMakeLists.txt
-rwxrwxr-x 1 xxxxx xxxxx 8920 Mar 28 05:02 hello*
-rw-rw-r-- 1 xxxxx xxxxx 97 Mar 28 04:54 main.cpp
-rw-rw-r-- 1 xxxxx xxxxx 4728 Mar 28 04:59 Makefile
xxxxx@ubuntu:~/LinuxC++/230328C++$
CMake一个HelloWorld-语法介绍
PROJECT 关键字
可以用来指定工程名字和支持的语言 默认支持所有语言
PROJECT(HELLO) 指定工程名字 并且支持所有语言--建议
PROJECT(HELLO CXX) 支持C++
PROJECT(HELLO C CXX) 支持 C C++
该指令隐含了两个CMAKE变量
<projectname> _BINARY_DIR
<projectname>_SOURCE_DIR
问题:如果改工程名 这两个变量也会跟着改变
解决:定义两个预定义变量 PROJECT_BINARY_DIR 和 PROJECT_SOURCE_DIR 这两个变量和<projectname> BINARY_DIR <projectname>SOURCE_DIR时一致的
SET关键字
显示指定变量 多个变量之间用空格来分割
SET(SRC_LIST main.cpp t1.cpp t2.cpp)
MESSAGE关键字
向终端输出用户自定义信息
主要包含三种信息
·SEND ERROR 产生错误 生成过程被跳过
·STATUS 输出前缀为 -的信息
·FATAL_ERROR 立即终止cmake过程
ADD_EXECUTABLE关键字
生成可执行文件
ADD_EXECUTABLE(hello SRC_LIST) 生成可执行文件的文件名是hello 源文件读取变量 SRC_LIST中的变量内容 也可以直接写 ADD_EXECUTABLE(Hello main.cpp)
工程名HELLO和此处的hello是没有关系的
语法的基本原则
-
变量使用${}方式取值 但是再IF控制语句中直接使用变量名
-
指令(参数1 参数2)参数用圆括号括起来 参数之间可以用空格或者分号进行分割 例如 ADD_EXECUTABLE(main.cpp t1.cpp),ADD_EXECUTABLE(main.cpp;t1.cpp)
-
指令是不区分大小写的 参数和变量名是区分大小写的 但是推荐使用全部大写书写指令
语法注意事项
-
SET(SRC_LIST main.cpp) 可以写成SET(SRC_LIST "main.cpp") 如果源文件名中含有空格 则必须使用双引号
-
ADD_EXECUTABLE(hello main) main的后缀可以不写 会自动去找 .c和.cpp 最好不要这样写 可能有两个文件 main.cpp 和main
内部构建和外部构建
上述例子就是外部构建 产生的临时文件比较多 不方便清理
外部构建 会把生成的临时文件放在build目录下 不会对源文件有任何影响 所以推荐使用外部构建
外部构建举例
新建build文件夹 cmake时使用 cmake .. 指的是用cmake编译上一层目录 生成的临时文件就都在build里面
使用 ./hello执行编译后的文件
avis@ubuntu:~/LinuxC++/230328C++/build$ make Scanning dependencies of target hello [ 50%] Building CXX object CMakeFiles/hello.dir/main.cpp.o [100%] Linking CXX executable hello [100%] Built target hello travis@ubuntu:~/LinuxC++/230328C++/build$ ./hello Hello World travis@ubuntu:~/LinuxC++/230328C++/build$
xxxxx@ubuntu:~/LinuxC++/230328C++$ mkdir build
xxxxx@ubuntu:~/LinuxC++/230328C++$ cd build
xxxxx@ubuntu:~/LinuxC++/230328C++/build$ cmake ..
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- This is BINARY dir/home/xxxxx/LinuxC++/230328C++/build
-- This is SOURCE dir/home/xxxxx/LinuxC++/230328C++
-- Configuring done
-- Generating done
-- Build files have been written to: /home/xxxxx/LinuxC++/230328C++/build
xxxxx@ubuntu:~/LinuxC++/230328C++/build$ ll
total 40
drwxrwxr-x 3 xxxxx xxxxx 4096 Mar 28 05:32 ./
drwxrwxr-x 3 xxxxx xxxxx 4096 Mar 28 05:32 ../
-rw-rw-r-- 1 xxxxx xxxxx 12609 Mar 28 05:32 CMakeCache.txt
drwxrwxr-x 5 xxxxx xxxxx 4096 Mar 28 05:32 CMakeFiles/
-rw-rw-r-- 1 xxxxx xxxxx 1514 Mar 28 05:32 cmake_install.cmake
-rw-rw-r-- 1 xxxxx xxxxx 4752 Mar 28 05:32 Makefile