CMake

本文详细介绍了CMake的编译器和链接器标志设置,包括CMAKE_CXX_FLAGS的使用,如何检查编译器对C++11等标准的支持。同时,讲解了CMake的变量配置,如添加预编译定义,设置头文件和库文件路径。此外,还展示了如何查看及使用CMake的各种相关变量。

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

CMAKE

official tutorials

对于源码编译的库

cmake -D [parameters] …

make -j

make install 是把头文件和库文件写入CMAKE_INSTALL_PREFIX下。

MESSAGE(STATUS " path ${…}" ) 可以在编译时检查路径是否出错

include(CheckCXXCompilerFlag)
CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
if(COMPILER_SUPPORTS_CXX11)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 
elseif(COMPILER_SUPPORTS_CXX0X)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
else()
    message(STATUS "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
endif()

判断编译器支持

Compiler and Linker

FLAGS

cmake provides a set of variables that specify compiler and linker flags. The variable invovled has the following form:

  • CMAKE_<LANG>_FLAGS

  • CMAKE_<LANG>_FLAGS_<CONFIG>

<LANG> specifies the languange, like C, CXX, Fortran, …

<CONFIG> is the uppercase of the build types.

The first variable will be applied to all build types. The second will to applied to the build type specified by <CONFIG>.

The same goes for linker FLAGS

  • CMAKE_<TARGETTYPE>_LINKER_FLAGS

  • CMAKE_<TARGETTYPE>_LINKER_FLAGS_<CONFIG>

<TARGETTYPE> :

  1. EXE created by the add_executable(…)
  2. SHARED created by add_library(name SHARED …)
  3. STATIC created by add_library(name STATIC …)
  4. MODULE created by add_library(name MODULE …)

Append flags!

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")
# or use
string(APPEND CMAKE_CXX_FLAGS " -Wall -Werror")

Definations

添加预编译时的编译

# 给当前 CMakeLists 中所有 Target 添加定义
# 可以使用 ${VAR} 赋值
add_compile_definitions(VAR1 VAR2=1 ...)

路径配置

# 头文件路径
include_directories(...)
# 库文件路径
link_directories(...)

CMake 变量

输出查看所有变量

get_cmake_property(_variableNames VARIABLES)
list (SORT _variableNames)
foreach (_variableName ${_variableNames})
    message(STATUS "${_variableName}=${${_variableName}}")
endforeach()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值