# set minimum cmake version
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
# project name and language
project(recipe-03 LANGUAGES CXX)
# define executable and its source file
add_executable(hello-world hello-world.cpp)
target_compile_definitions(hello-world PUBLIC "COMPILER_NAME=\"${CMAKE_CXX_COMPILER_ID}\"")
# let the preprocessor know about the compiler vendor
if(CMAKE_CXX_COMPILER_ID MATCHES Intel)
target_compile_definitions(hello-world PUBLIC "IS_INTEL_CXX_COMPILER")
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES GNU)
target_compile_definitions(hello-world PUBLIC "IS_GNU_CXX_COMPILER")
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES PGI)
target_compile_definitions(hello-world PUBLIC "IS_PGI_CXX_COMPILER")
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES XL)
target_compile_definitions(hello-world PUBLIC "IS_XL_CXX_COMPILER")
endif()
# etc ...
cmake 处理与编译器相关的源代码
最新推荐文章于 2025-02-19 23:32:30 发布
该代码示例展示了如何在CMake中根据不同的编译器(如Intel, GNU, PGI, XL等)设置预处理器宏。通过`cmake_minimum_required`和`project`定义项目的基本信息,然后使用`add_executable`创建可执行文件,并利用`target_compile_definitions`为不同编译器添加特定的宏定义。
971

被折叠的 条评论
为什么被折叠?



