CMakeList.txt的简单使用

本文详细介绍CMake的基础使用方法,包括CMakeLists.txt的编写、项目配置、多目录工程的处理等,适合初学者快速入门。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CMakeList.txt的简单使用

CMake 简介

CMake 是一个跨平台的自动化建构系统,它使用一个名为 CMakeLists.txt 的文件来描述构建过程,可以产生标准的构建文件,如 Unix 的 Makefile 或Windows Visual C++ 的 projects/workspaces 。文件 CMakeLists.txt 需要手工编写,也可以通过编写脚本进行半自动的生成。CMake 提供了比 autoconfig 更简洁的语法。在 linux 平台下使用 CMake 生成 Makefile 并编译的流程如下:

  • 1)编写 CmakeLists.txt。
  • 2)执行命令“cmake PATH”或者“ccmake PATH”生成 Makefile ( PATH 是 CMakeLists.txt
    所在的目录 )。
  • 3)使用 make 命令进行编译。

第一个工程

源代码分布情况:

./eg_demo/main.cpp

现假设我们的项目中只有一个源文件 main.cpp
源文件main.cpp内容:

#include <iostream>
int main()
{
    std::cout<<"Hello world"<<std::endl;
    return 0;
}

:为了构建该项目,我们需要编写文件 CMakeLists.txt 并将其与 main.cpp 放在 同一个目录下。
CMakeLists.txt内容为:

PROJECT(Hello)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})

CMakeLists.txt 的语法比较简单,由命令、注释和空格组成,其中命令是不区分大小写的,符号"#"后面的内容被认为是注释。命令由命令名称、小括号和参数组成,参数之间使用空格进行间隔。例如对于清单2的 CMakeLists.txt 文件:第一行是一条命令,名称是 PROJECT ,参数是 Hello,该命令表示项目的名称是 Hello 。第二行的命令限定了 CMake 的版本。第三行使用命令 AUX_SOURCE_DIRECTORY 将当前目录中的源文件名称赋值给变量 DIR_SRCS 。 CMake 手册中对命令 AUX_SOURCE_DIRECTORY 的描述如下:aux_source_directory(<dir> <variable>)该命令会把参数 <dir>中所有的源文件名称赋值给参数<variable>。 第四行使用命令 ADD_EXECUTABLE 指示变量 DIR_SRCS 中的源文件需要编译 成一个名称为 main 的可执行文件。
CMake的编译基本就两个步骤:
cmake 指向CMakeLists.txt所在的目录,例如cmake … 表示CMakeLists.txt在当前目录的上一级目录。cmake后会生成很多编译的中间文件以及makefile文件,所以一般建议新建一个新的目录,专门用来编译,例如

mkdir build
cd build
cmake ..
make

make根据生成makefile文件,编译程序。

camke 的运行结果:

[eg_demo]$ mkdir build
[eg_demo]$ cd build/
[build]$ cmake ..
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: /eg_demo/build
[build]$ make
make: Warning: File `Makefile' has modification time 969 s in the future
make[1]: Warning: File `CMakeFiles/Makefile2' has modification time 969 s in the future
make[2]: Warning: File `CMakeFiles/main.dir/flags.make' has modification time 969 s in the future
Scanning dependencies of target main
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Warning: File `CMakeFiles/main.dir/flags.make' has modification time 969 s in the future
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
[100%] Built target main
make[1]: warning:  Clock skew detected.  Your build may be incomplete.
make: warning:  Clock skew detected.  Your build may be incomplete.
[build]$ ./main
Hello world

第二个工程

源代码分布情况:

./eg_demo/main.cpp 、test.cpp 、test.h

主函数main.cpp:

#include <iostream>
#include "test.h"
int main()
{
    std::cout<<"Hello world"<<std::endl;
	print_test();
    return 0;
}

test.cpp内容:

#include "test.h"

void print_test()
{
	std::cout<< "src:test"<<std::endl;
}

test.h中内容:

#include <iostream>
void print_test();

CMakeLists.txt内容:

PROJECT(Hello)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})

注:和第一个项目一致
camke 的运行结果:

mkdir build
[eg_demo2]$ cd build/
[build]$ cmake ..
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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
-- Configuring done
-- Generating done
-- Build files have been written to: eg_demo2/build
[build]$ make
make: Warning: File `Makefile' has modification time 970 s in the future
make[1]: Warning: File `CMakeFiles/Makefile2' has modification time 970 s in the future
make[2]: Warning: File `CMakeFiles/main.dir/flags.make' has modification time 970 s in the future
Scanning dependencies of target main
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Warning: File `CMakeFiles/main.dir/flags.make' has modification time 970 s in the future
[ 33%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[ 66%] Building CXX object CMakeFiles/main.dir/test.cpp.o
[100%] Linking CXX executable main
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
[100%] Built target main
make[1]: warning:  Clock skew detected.  Your build may be incomplete.
make: warning:  Clock skew detected.  Your build may be incomplete.
[build]$ ./main
Hello world
src:test

第三个工程

源代码分布情况:

./eg_demo/main.cpp
./eg_demo/src/test.cpp 、test.h

程序中的内容和第二个项目一致
注: 多目录工程的CmakeLists.txt编写
主文件(eg_demo)的CMakeLists.txt:

PROJECT(Hello)
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
INCLUDE_DIRECTORIES(./ ./src)
ADD_SUBDIRECTORY(src)
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
ADD_EXECUTABLE(main ${DIR_SRCS})
TARGET_LINK_LIBRARIES(main test)

子文件(src)的CMakeLists.txt:

AUX_SOURCE_DIRECTORY(. DIR_TEST_SRCS)
ADD_LIBRARY(test ${DIR_TEST_SRCS})

camke 的运行结果:

 mkdir build
[eg_demo3]$ cd build
[build]$ cmake ..
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- 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
CMake Warning (dev) at src/CMakeLists.txt:2 (ADD_LIBRARY):
  Policy CMP0037 is not set: Target names should not be reserved and should
  match a validity pattern.  Run "cmake --help-policy CMP0037" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  The target name "test" is reserved or not valid for certain CMake features,
  such as generator expressions, and may result in undefined behavior.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
-- Generating done
-- Build files have been written to: /eg_demo3/build
[build]$ make
make: Warning: File `Makefile' has modification time 937 s in the future
make[1]: Warning: File `CMakeFiles/Makefile2' has modification time 938 s in the future
make[2]: Warning: File `src/CMakeFiles/test.dir/flags.make' has modification time 937 s in the future
Scanning dependencies of target test
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Warning: File `src/CMakeFiles/test.dir/flags.make' has modification time 937 s in the future
[ 25%] Building CXX object src/CMakeFiles/test.dir/test.cpp.o
[ 50%] Linking CXX static library libtest.a
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
[ 50%] Built target test
make[2]: Warning: File `CMakeFiles/main.dir/flags.make' has modification time 934 s in the future
Scanning dependencies of target main
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
make[2]: Warning: File `CMakeFiles/main.dir/flags.make' has modification time 934 s in the future
[ 75%] Building CXX object CMakeFiles/main.dir/main.cpp.o
[100%] Linking CXX executable main
make[2]: warning:  Clock skew detected.  Your build may be incomplete.
[100%] Built target main
make[1]: warning:  Clock skew detected.  Your build may be incomplete.
make: warning:  Clock skew detected.  Your build may be incomplete.
[build]$ ./main
Hello world
src:test

参考:https://www.ibm.com/developerworks/cn/linux/l-cn-cmake/index.html
https://blog.youkuaiyun.com/ktigerhero3/article/details/70313350/
http://www.cnblogs.com/cv-pr/p/6206921.html


注:博众家之所长,集群英之荟萃。
<think>嗯,用户想知道CMakeLists.txt使用的是什么语言。首先,我得回忆一下之前了解过的CMake相关知识。CMake是一个构建工具,用于管理代码的编译过程。根据之前看过的引用内容,引用[1]提到CMakeLists.txtCMake的配置文件,定义项目的编译规则和依赖关系。那么,这个文件里的语法应该属于CMake自己的领域特定语言(DSL)。 我记得CMake的语法有自己的一套命令,比如project、add_executable、target_link_libraries等,这些命令按照特定的结构编写。用户可能对DSL不太熟悉,需要解释清楚为什么它不是通用编程语言,而是专门为构建过程设计的。同时,用户可能也会混淆CMake与其他构建工具,比如Makefile,所以需要指出CMake生成这些文件,但自身使用不同的语法。 另外,用户可能还想知道如何学习CMakeLists.txt的编写,所以应该提供一些基本命令示例,比如设置项目名称、添加可执行文件等。示例部分可以展示一个简单CMakeLists.txt结构,帮助用户直观理解。同时,考虑到用户可能在实际项目中遇到多模块的情况,引用[2]中的例子展示了多模块项目结构,可以适当提及,但不需要深入,除非用户后续提问。 还要注意用户可能的后续问题,比如CMakeMake的关系,或者如何调试CMake脚本,所以生成的相关问题需要覆盖这些方面。确保回答准确,避免错误,比如明确CMake语言不是脚本语言如Python或Bash,而是专用的配置语言。需要强调其声明式特性,以及跨平台的优势,这样用户能明白为何选择CMake。 最后,检查是否符合用户的要求:使用中文,行内公式用$...$,独立公式用$$...$$,虽然这个问题可能不涉及数学公式,但如果有提及版本号或其他可能需要强调的地方,可能需要使用。不过当前问题主要涉及语法,可能不需要数学表达式,但保持结构清晰,分点回答,引用正确来源。确保没有使用任何Markdown格式,语言自然口语化,避免使用专业术语过多,必要时进行解释。</think>CMakeLists.txt 使用的是一种**领域特定语言(Domain-Specific Language, DSL)**,专为 CMake 构建系统设计。这种语言包含一系列命令(如 `project`、`add_executable`、`target_link_libraries` 等)和变量(如 `CMAKE_CXX_STANDARD`),用于描述项目的构建规则、依赖关系和编译配置[^1][^2]。 ### 关键特性: 1. **声明式语法** CMake 语言通过声明式命令定义目标(如可执行文件、库)及其属性,例如: ```cmake project(MyProject VERSION 1.0) # 定义项目名称和版本 add_executable(app main.cpp) # 创建可执行文件目标 target_link_libraries(app PRIVATE module1) # 链接库 ``` 2. **跨平台支持** 通过抽象底层编译工具(如 GCC、Clang、MSVC),CMake 语言编写的配置可生成多种平台的构建脚本(如 Makefile、Visual Studio 项目)[^1]。 3. **模块化设计** 支持通过 `add_subdirectory()` 管理多模块项目,每个子目录可包含独立的 `CMakeLists.txt` 文件[^2]。 ### 示例:简单项目 ```cmake cmake_minimum_required(VERSION 3.10) # 指定 CMake 最低版本 project(HelloWorld) # 定义项目名称 add_executable(hello main.cpp) # 添加可执行文件 ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Peanut_范

您的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值