2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
CMake is a cross-platform build system generation tool that can generate build files (such as Makefile or Visual Studio project files) for compiling and linking applications.
Windows
Linux
sudo apt-get update
sudo apt-get install cmake
We need to provide source files and provide a project configuration description to CMake. This description is done using CMake, and the full documentation can be found at https://cmake.org/cmake/help/latest/.
Create a simple C++ project containing the following files:
We want to compile the main.cpp source code into a single executable file:
#include<iostream>
int main(){
std::cout<<"Hello world"<<std::endl;
return 0;
}
# 设置CMake所需的最低版本。如果使用的CMake版本低于该版本,则会发出致命错误
cmake_minimum_required(VERSION 3.10)
# 声明了项目的名称(Test)和支持的编程语言(CXX代表C++)
project(Test CXX)
# 指示CMake创建一个新目标:可执行文件main。这个可执行文件是通过编译和链接源文件main生成的。CMake将为编译器使用默认设置,并自动选择生成工具
add_executable(main main.cpp)
Open a command prompt, navigate to the project directory, and run:
mkdir build
cd build
cmake ..
Or use, with the same effect:
# 该命令是跨平台的,使用了-H和-B为CLI选项。-H表示当前目录中搜索根CMakeLists.txt文件。-Bbuild告诉CMake在一个名为build的目录中生成所有的文件
cmake -H. -Bbuild
The project configuration is already inbuild
We can now compile the executable:
cmake --build .
CMake is a build system generator that can configure a project for different toolsets on different platforms using a single CMakeLists.txt. You can describe in CMakeLists.txt what the build system must do to configure and compile your code. Based on these instructions, CMake will generate the appropriate instructions for the selected build system (Unix Makefile, Ninja, Visual Studio, etc.).
NMake Makefiles
orVisual Studio
.MinGW Makefiles
.cmake -Bbuild -G "NMake Makefiles"
The directory structure is as follows:
MyProject/
├── include/
│ └── say.hpp
├── src/
│ ├── main.cpp
│ └── say.cpp
├── lib/
└── bin/
# main.cpp
#include"say.hpp"
int main() {
say();
return 0;
}
# say.cpp
#include<iostream>
#include"say.hpp"
void say(){
std::cout<<"Hello world!"<<std::endl;
}
# say.hpp
#pragma once
void say();
We need to write CMakeLists.txt
file to compile these files and link the library.
# 指定 CMake 的最低版本要求
cmake_minimum_required(VERSION 3.10)
# 定义项目名称和版本
project(test VERSION 1.0.0)
# 设置 C++ 标准
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
# 添加 include 目录到编译器的头文件搜索路径
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
# 设置库和可执行文件输出路径
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/bin)
# 创建静态库目标
add_library(message
STATIC # 若动态库则参数为SHARED
${CMAKE_CURRENT_SOURCE_DIR}/src/say.cpp
)
# 添加可执行文件
add_executable(test
${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp
)
# 链接静态/动态库到可执行文件
target_link_libraries(test say)
Open a command prompt or terminal and navigate to the project root directory:
cd Test
create build
directory and navigate into it:
mkdir build
cd build
Run the CMake configuration command:
cmake ..
Run the build command:
cmake --build .