cmake-03-CompilerOption

本文介绍如何使用CMake进行项目配置与编译,包括设置默认编译选项、指定编译器类型、定义编译标志及标准等关键步骤。

LANGUAGES C CXX

	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	
	# project name and language, load c and cxx compiler
	project(recipe-06 LANGUAGES C CXX)
	
	message(STATUS "Is the C++ compiler loaded? ${CMAKE_CXX_COMPILER_LOADED}")
	if(CMAKE_CXX_COMPILER_LOADED)
	  message(STATUS "The C++ compiler ID is: ${CMAKE_CXX_COMPILER_ID}")
	  message(STATUS "Is the C++ from GNU? ${CMAKE_COMPILER_IS_GNUCXX}")
	  message(STATUS "The C++ compiler version is: ${CMAKE_CXX_COMPILER_VERSION}")
	endif()
	
	message(STATUS "Is the C compiler loaded? ${CMAKE_C_COMPILER_LOADED}")
	if(CMAKE_C_COMPILER_LOADED)
	  message(STATUS "The C compiler ID is: ${CMAKE_C_COMPILER_ID}")
	  message(STATUS "Is the C from GNU? ${CMAKE_COMPILER_IS_GNUCC}")
	  message(STATUS "The C compiler version is: ${CMAKE_C_COMPILER_VERSION}")
	endif()
	
	$ cmake .
	-- The C compiler identification is GNU 9.3.0
	-- The CXX compiler identification is GNU 9.3.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
	-- Is the C++ compiler loaded? 1
	-- The C++ compiler ID is: GNU
	-- Is the C++ from GNU? 1
	-- The C++ compiler version is: 9.3.0
	-- Is the C compiler loaded? 1
	-- The C compiler ID is: GNU
	-- Is the C from GNU? 1
	-- The C compiler version is: 9.3.0
	-- Configuring done
	-- Generating done
	-- Build files have been written to: /home/jianleya/trainning/cmake/cmake

CMAKE_CXX_FLAGS

cmake_minimum_required(VERSION 3.5)

# Set a default C++ compile flag
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DEX2" CACHE STRING "Set C++ Compiler Flags" FORCE)
project (compile_flags)

add_executable(cmake_examples_compile_flags main.cpp)

can also use cmake options
$ cmake … -DCMAKE_CXX_FLAGS="-DEX2"

CMAKE_BUILD_TYPE

	# build type specfies build options
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	project(recipe-07 LANGUAGES C CXX)
	
	# we default to Release build type
	if(NOT CMAKE_BUILD_TYPE)
	  set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
	endif()
	
	message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
	
	message(STATUS "C flags, Debug configuration: ${CMAKE_C_FLAGS_DEBUG}")
	message(STATUS "C flags, Release configuration: ${CMAKE_C_FLAGS_RELEASE}")
	message(STATUS "C flags, Release configuration with Debug info: ${CMAKE_C_FLAGS_RELWITHDEBINFO}")
	message(STATUS "C flags, minimal Release configuration: ${CMAKE_C_FLAGS_MINSIZEREL}")
	
	message(STATUS "C++ flags, Debug configuration: ${CMAKE_CXX_FLAGS_DEBUG}")
	message(STATUS "C++ flags, Release configuration: ${CMAKE_CXX_FLAGS_RELEASE}")
	message(STATUS "C++ flags, Release configuration with Debug info: ${CMAKE_CXX_FLAGS_RELWITHDEBINFO}")
	message(STATUS "C++ flags, minimal Release configuration: ${CMAKE_CXX_FLAGS_MINSIZEREL}")

CMAKE_CXX_FLAGS

similar as
CMAKE_CXX_FLAGS

target_compile_options(flags)

	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	project(recipe-08 LANGUAGES CXX)
	message("C++ compiler flags: ${CMAKE_CXX_FLAGS}")
	
	list(APPEND flags "-fPIC" "-Wall")
	if(NOT WIN32)
	  list(APPEND flags "-Wextra" "-Wpedantic")
	endif()
	
	add_library(geometry
	  STATIC
	    geometry_circle.cpp
	    geometry_circle.hpp
	    geometry_polygon.cpp
	    geometry_polygon.hpp
	    geometry_rhombus.cpp
	    geometry_rhombus.hpp
	    geometry_square.cpp
	    geometry_square.hpp
	  )
	
	target_compile_options(geometry
	  PRIVATE
	    ${flags}
	  )
	
	add_executable(compute-areas compute-areas.cpp)
	
	target_compile_options(compute-areas
	  PRIVATE
	    "-fPIC"
	  )
	
	target_link_libraries(compute-areas geometry)
	
	

set_target_properties (c++14)

	# set as c++14  etc
	
	cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
	project(recipe-09 LANGUAGES CXX)

	set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
	
	add_library(animals
	  SHARED
	    Animal.cpp
	    Animal.hpp
	    Cat.cpp
	    Cat.hpp
	    Dog.cpp
	    Dog.hpp
	    Factory.hpp
	  )
	
	set_target_properties(animals
	  PROPERTIES
	    CXX_STANDARD 14
	    CXX_EXTENSIONS OFF
	    CXX_STANDARD_REQUIRED ON
	    POSITION_INDEPENDENT_CODE 1
	  )
	
	add_executable(animal-farm animal-farm.cpp)
	
	set_target_properties(animal-farm
	  PROPERTIES
	    CXX_STANDARD 14
	    CXX_EXTENSIONS OFF
	    CXX_STANDARD_REQUIRED ON
	  )
	
	target_link_libraries(animal-farm animals)
	

target_compile_definitions (-D)

cmake_minimum_required(VERSION 3.5)

# Set the project name
project (compile_flags)

# Add an executable
add_executable(cmake_examples_compile_flags main.cpp)
#equals to "gcc/g++ -D EX3" when build target cmake_examples_compile_flags
target_compile_definitions(cmake_examples_compile_flags
    PRIVATE EX3
)
<think>我们正在讨论CMake的两个重要命令:`cmake --build` 配合 `--target` 参数,以及 `cmake --install`。以下是对这两个命令的详细解释: ### 1. `cmake --build` 命令与 `--target` 参数 `cmake --build` 命令用于构建已经在配置步骤中生成构建系统的项目。它实际上是对底层构建工具(如Make、Ninja、Visual Studio等)的封装,使得用户可以使用统一的命令进行构建。 #### 基本用法: ```bash cmake --build <build-directory> [options] ``` 其中 `<build-directory>` 是CMake生成的构建目录(通常包含 `CMakeCache.txt` 文件)。 #### 常用选项: - `--target`:指定要构建的目标(target)。目标可以是可执行文件、库文件或自定义目标(如 `install`、`test` 等)。如果不指定,则构建默认目标(通常是 `all`)。 - `--config`:指定构建配置(如 `Debug`、`Release` 等),主要在多配置生成器(如Visual Studio、Xcode)中使用。 - `-j` 或 `--parallel`:指定并行编译的作业数。 #### 示例: - 构建默认目标(整个项目): ```bash cmake --build ./build ``` - 构建特定目标(如 `myapp`): ```bash cmake --build ./build --target myapp ``` - 构建安装目标(该目标必须已在CMakeLists.txt中定义): ```bash cmake --build ./build --target install ``` 这等同于在Makefile生成器中运行 `make install`。 #### 注意: - `--target` 参数指定的目标必须在项目的CMakeLists.txt中通过 `add_executable`、`add_library` 或 `add_custom_target` 定义。 ### 2. `cmake --install` 命令 `cmake --install` 命令用于安装项目到指定目录。它直接调用安装步骤,而不需要先构建整个项目(但前提是安装所需的目标已经构建)。该命令在CMake 3.15及以上版本中提供。 #### 基本用法: ```bash cmake --install <build-directory> [options] ``` #### 常用选项: - `--prefix`:指定安装目录(覆盖CMake配置时设置的 `CMAKE_INSTALL_PREFIX`)。 - `--config`:指定安装的配置(如 `Debug`、`Release` 等)。对于单配置生成器(如Make、Ninja)通常不需要,但多配置生成器必须指定。 #### 示例: - 安装项目到默认目录(即配置时指定的 `CMAKE_INSTALL_PREFIX`): ```bash cmake --install ./build ``` - 安装项目到自定义目录: ```bash cmake --install ./build --prefix ./install ``` - 安装特定配置(如Release): ```bash cmake --install ./build --config Release ``` #### 注意: - 项目的CMakeLists.txt中必须定义了 `install()` 规则,否则此命令无效。 - 安装过程会将构建好的目标文件、头文件、配置文件等复制到指定目录。 ### 对比:`cmake --build --target install` 与 `cmake --install` | 命令 | 功能 | 适用场景 | 优势 | |------|------|----------|------| | `cmake --build <dir> --target install` | 调用构建系统的安装目标 | 所有CMake版本 | 兼容性高 | | `cmake --install <dir>` | 直接执行安装操作 | CMake 3.15+ | 更简洁,支持 `--prefix` 动态覆盖安装路径 | ### 示例场景 假设有一个项目结构如下: ``` project/ build/ # 构建目录 src/ # 源代码 CMakeLists.txt ``` #### 步骤1:配置项目 ```bash cd project cmake -B build -DCMAKE_INSTALL_PREFIX=./install ``` #### 步骤2:构建并安装 - 方法A(传统方式): ```bash cmake --build build cmake --build build --target install ``` - 方法B(直接安装): ```bash cmake --build build # 先构建 cmake --install build # 再安装 ``` 或一步安装(要求目标已构建): ```bash cmake --install build ``` > 注意:`cmake --install` 会自动构建所需目标(如果尚未构建)吗?**不会**。它只执行安装步骤,因此确保目标已构建。 ### 总结 - 使用 `cmake --build --target <target>` 构建特定目标。 - 使用 `cmake --install`(CMake 3.15+)简化安装过程,尤其适合脚本自动化。 - 在旧版本CMake中,使用 `cmake --build --target install` 进行安装。 §§相关问题§§ 1. CMake 的安装规则(`install()` 命令)如何定义? 2. 如何指定安装路径(`CMAKE_INSTALL_PREFIX`)? 3. CMake 多配置生成器(如Visual Studio)如何处理安装? 4. 如何创建支持 `find_package` 的CMake包? </think> ### CMake `--build` 与 `--install` 命令详解 #### 一、`cmake --build` 命令(含 `--target` 参数) 用于在**已配置好的构建目录**中执行构建操作,是跨平台的构建命令封装。 **基本语法**: ```bash cmake --build <构建目录> [选项] ``` **常用选项**: | 选项 | 作用 | 示例 | |------|------|------| | `--target` | 指定构建目标 | `cmake --build build --target myapp` | | `-j N` | 并行编译(N=线程数) | `cmake --build build -j 8` | | `--config` | 指定构建配置(Debug/Release) | `cmake --build build --config Release` | **典型场景**: 1. **构建特定目标**(需在 CMakeLists.txt 中定义): ```bash # 构建名为 "test" 的单元测试目标 cmake --build ./build --target test ``` [^1] 2. **多线程构建**: ```bash # 使用 8 线程构建整个项目 cmake --build ./build -j 8 ``` 3. **清理构建产物**: ```bash # 执行 clean 目标(需 CMakeLists.txt 定义) cmake --build ./build --target clean ``` #### 二、`cmake --install` 命令 用于将构建好的文件安装到系统目录(需 CMakeLists.txt 定义安装规则)。 **基本语法**: ```bash cmake --install <构建目录> [选项] ``` **核心选项**: | 选项 | 作用 | 示例 | |------|------|------| | `--prefix` | 覆盖安装路径 | `cmake --install build --prefix ./install` | | `--config` | 指定安装配置 | `cmake --install build --config Release` | **工作流程**: ```mermaid graph LR A[CMakeLists.txt 定义 install() 规则] --> B[配置项目] B --> C[构建项目] C --> D[执行安装] ``` **示例**: 1. **安装到默认路径**(需 sudo 权限): ```bash cmake --install ./build # 默认路径通常为 /usr/local ``` 2. **自定义安装路径**: ```bash # 安装到当前目录下的 install 文件夹 cmake --install ./build --prefix ./install ``` 3. **指定构建配置安装**: ```bash cmake --install ./build --config Debug --prefix ./debug_install ``` #### 三、关键区别对比表 | 特性 | `cmake --build` | `cmake --install` | |------|-----------------|-------------------| | **目的** | 编译源代码生成二进制 | 复制文件到安装目录 | | **依赖** | 需要先执行 `cmake` 配置 | 需要先完成构建 | | **必需规则** | `add_executable()`/`add_library()` | `install()` 命令 | | **跨平台** | 封装 make/ninja/msbuild | 直接处理安装逻辑 | | **版本要求** | CMake 2.8+ | CMake 3.15+ | #### 四、典型问题解决 **权限问题**(Linux/macOS): ```bash # 临时获取权限 sudo cmake --install ./build # 或安装到用户目录 cmake --install ./build --prefix $HOME/.local ``` **缺失安装规则**: 需在 CMakeLists.txt 添加: ```cmake install(TARGETS myapp RUNTIME DESTINATION bin # 可执行文件 LIBRARY DESTINATION lib # 动态库 ARCHIVE DESTINATION lib # 静态库 ) install(DIRECTORY include/ DESTINATION include) # 头文件 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值