CMake学习记录
1、创建build文件夹
创建build文件夹,在此文件下进行编译生成可执行文件,即:
mkdir build
cd build
cmake ..
make
2、对CMakeLists.txt的解析:
2.1 CMakeLists.txt一般放在根目录下,文件结构如下:
-build
-include
-src
CMakeLists.txt
README.md
2.2 对CMakeLists.txt文件内容的详细解析
cmake_minimum_required(VERSION 3.10)
# 1. 设定工程名和版本号
project(ProjectName VERSION 1.0)
set (ProjectName_VERSION_MAJOR 1)
set (ProjectName_VERSION_MINOR 0)
# 2. 检查 C++11
include(CheckCXXCompilerFlag)
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_COMPILER_IS_CLANG OR
CMAKE_CXX_COMPILER_ID MATCHES "Clang")
check_cxx_compiler_flag(-std=c++11 SUPPORTS_CXX11)
if(NOT SUPPORTS_CXX11)
message(FATAL_ERROR "Compiler doesn't support C++11")
endif()
endif()
# 3. 将二叉树添加到包含文件的搜索路径中,这样我们就可以找到所含的头文件
include_directories(include)
# 4. 生成可执行文件
add_executable(ProjectName project.cpp)
# 5. 添加链接库
target_link_libraries(ProjectName other_lib)
|