- 1.设置头文件的路径
- 2.设置配置文件
- 3.设置c/c++版本
- 4.编译指令
- 5.链接三方库
- 6.宏控
- 7. 搜索文件
- 8. 编译结果类型设置
- 9.设置变量
- 10. 打印消息
target_include_directories(Tutorial PUBLIC
"${PROJECT_BINARY_DIR}"
)
# 链接头文件路径
target_include_directories(MathFunctions
INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}
)
2.设置配置文件
configure_file(TutorialConfig.h.in TutorialConfig.h)
TutorialConfig.h.in :
// the configured options and settings for Tutorial #define Tutorial_VERSION_MAJOR @Tutorial_VERSION_MAJOR@ #define Tutorial_VERSION_MINOR @Tutorial_VERSION_MINOR@
访问配置文件:
if (argc < 2) {
// report version
std::cout << argv[0] << " Version " << Tutorial_VERSION_MAJOR << "."
<< Tutorial_VERSION_MINOR << std::endl;
std::cout << "Usage: " << argv[0] << " number" << std::endl;
return 1;
}
3.设置c/c++版本
# specify the C++ standard set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED True)4.编译指令
cd Step1_build cmake --build .5.链接三方库
target_link_libraries(Tutorial PUBLIC MathFunctions) add_library(hello::library ALIAS hello_library)6.宏控
# 设置变量
option(USE_MYMATH "Use tutorial provided math implementation" ON)
# 宏控
if(USE_MYMATH)
add_subdirectory(MathFunctions)
list(APPEND EXTRA_LIBS MathFunctions)
list(APPEND EXTRA_INCLUDES "${PROJECT_SOURCE_DIR}/MathFunctions")
endif()
# 设置数字
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}")
7. 搜索文件
# 查找目录下的所有源文件 # 并将名称保存到 DIR_SRCS 变量 aux_source_directory(. DIR_SRCS) aux_source_directory(8. 编译结果类型设置)
# 指定生成 MathFunctions 链接库
add_library (MathFunctions ${DIR_LIB_SRCS})
9.设置变量
set(SOURCES
src/Hello.cpp
src/main.cpp
)
# Add an executable with the above sources
add_executable(hello_headers ${SOURCES})
10. 打印消息
# Print path to generated files
message ("PROTO_SRCS = ${PROTO_SRCS}")
message ("PROTO_HDRS = ${PROTO_HDRS}")



