#include
using namespace std;
int main()
{
cout<<"hello world"<<endl;
#ifdef OUTPUT_HAHA
cout<<"i use add_definitions OUTPUT_HAHA"<<endl;
#endif OUTPUT_HAHA
return 0;
}
cmake_minimum_required(VERSION 3.8.0)
set(TARGET "hello")
set(TARGET_FOLDER_PATH "hello_folder")
set(TARGET_OUTPUT_NAME "hello")
PROJECT(NEWHELLO)
option(HAHA_OUTPUT_FLAG "HAHA_OUTPUT_FLAG" ON)
if(HAHA_OUTPUT_FLAG)
add_definitions(-DOUTPUT_HAHA)
endif()
#ZERO_CHECK监视CMakeLists.txt,如果CMakeLists.txt发生变化,则告诉编译器重新构建整个工程环境。
#CMakeLists.txt中加入set(CMAKE_SUPPRESS_REGENERATION true),这样不会产生ZERO_CHECK工程,可以手工跑这个目标。
set(CMAKE_SUPPRESS_REGENERATION 1)
#使用相对路径
set(CMAKE_USE_RELATIVE_PATHS ON)
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin") #可执行文件路径
set(LIBRARY_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/lib") #静态库文件路径
set(TARGET_BIN_OUTPUE_PATH "${PROJECT_SOURCE_DIR}/bin") #可执行文件路径
set(TARGET_LIB_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/lib") #静态库文件路径
#默认关闭,打开后才可以使用属性FOLDER,
#否则set_target_properties(${TARGET} PROPERTIES FOLDER "${TARGET_FOLDER_PATH}")会无效
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
#头文件和源文件目录
file(GLOB HEADERS "./*.h")
source_group("include" FILES ${HEADERS})
aux_source_directory(. SRC_LIST)
source_group("source" FILES ${SRC_LIST})
set(ALL_LIST ${HEADERS} ${SRC_LIST} "CmakeLists.txt")
add_executable(${TARGET_OUTPUT_NAME} ${ALL_LIST})
#版本
set_target_properties(${TARGET} PROPERTIES VERSION 1.1.0.0)
#可执行文件输出目录
set_target_properties(${TARGET} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TARGET_BIN_OUTPUE_PATH}")
#lib输出目录
set_target_properties(${TARGET} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${TARGET_LIB_OUTPUT_PATH}")
#二进制文件名字
set_target_properties(${TARGET} PROPERTIES OUTPUT_NAME "${TARGET_OUTPUT_NAME}")
#项目所在文件夹的目录
set_target_properties(${TARGET} PROPERTIES FOLDER "${TARGET_FOLDER_PATH}")
#安装
install(TARGETS "${TARGET}" RUNTIME DESTINATION "${PROJECT_SOURCE_DIR}/hello")
cmake ..
devenv.com NEWHELLO.sln /Build "Debug|x64"
.\hello.exe
set_target_properties(target1 target2 …
PROPERTIES prop1 value1
prop2 value2 …)
VERSION 指定构建的版本号,SOVERSION 指定构建的 API 版本号。
set_target_properties(${TARGET} PROPERTIES VERSION 1.1.0.0)
#可执行文件输出目录
set_target_properties(${TARGET} PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${TARGET_BIN_OUTPUE_PATH}")
#静态库文件输出目录
set_target_properties(${TARGET} PROPERTIES ARCHIVE_OUTPUT_DIRECTORY "${TARGET_LIB_OUTPUT_PATH}")
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set_target_properties(${TARGET} PROPERTIES FOLDER "${TARGET_FOLDER_PATH}")
install(TARGETS <target>... [...])
install(IMPORTED_RUNTIME_ARTIFACTS <target>... [...])
install({FILES | PROGRAMS} <file>... [...])
install(DIRECTORY <dir>... [...])
install(SCRIPT <file> [...])
install(CODE <code> [...])
install(EXPORT <export-name> [...])
install(RUNTIME_DEPENDENCY_SET <set-name> [...])
参数中的TARGET可以是很多种目标文件,最常见的是通过ADD_EXECUTABLE或者ADD_LIBRARY定义的目标文件,即可执行二进制、动态库、静态库:
|-
目标文件 | 内容 | 安装目录变量 | 默认安装文件夹 |
---|---|---|---|
ARCHIVE | 静态库 | ${CMAKE_INSTALL_LIBDIR} | lib 、a |
LIBRARY | 动态库 | ${CMAKE_INSTALL_LIBDIR} | dll、so |
RUNTIME | 可执行二进制文件 | ${CMAKE_INSTALL_BINDIR} | bin |
PUBLIC_HEADER | 与库关联的PUBLIC头文件 | ${CMAKE_INSTALL_INCLUDEDIR} | include |
PRIVATE_HEADER | 与库关联的PRIVATE头文件 | ${CMAKE_INSTALL_INCLUDEDIR} | include |
install(TARGETS "${TARGET}" RUNTIME DESTINATION "${PROJECT_SOURCE_DIR}/hello")
install(CODE "execute_process(
COMMAND /home/start.sh
)")
install(CODE "execute_process(
COMMAND chomd 755 /home/start.sh WORKING_DIRECTORY /home/
)")
FILE(WRITE filename "message to write"... )
FILE(APPEND filename "message to write"... )
FILE(READ filename variable)
FILE(GLOB variable [RELATIVE path] [globbing
expressions]...)
FILE(GLOB_RECURSE variable [RELATIVE path]
[globbing expressions]...)
FILE(REMOVE [directory]...)
FILE(REMOVE_RECURSE [directory]...)
FILE(MAKE_DIRECTORY [directory]...)
FILE(RELATIVE_PATH variable directory file)
FILE(TO_CMAKE_PATH path result)
FILE(TO_NATIVE_PATH path result)
例子:
file(GLOB HEADERS "./*.h")
aux_source_directory(. SRC_LIST)
source_group("source" FILES ${SRC_LIST})
用来载入 CMakeLists.txt 文件,也用于载入预定义的 cmake 模块.(本例子中没有用到)
include (file1 [OPTIONAL])
include (module [OPTIONAL])
OPTIONAL 参数的作用是文件不存在也不会产生错误。 你可以指定载入一个文件,如果定义的是一个模块,那么将在 CMAKE_MODULE_PATH 中搜 索这个模块并载入。
option(<variable> "" [value])
option(HAHA_OUTPUT_FLAG "HAHA_OUTPUT_FLAG" ON)
if(HAHA_OUTPUT_FLAG)
add_definitions(-DOUTPUT_HAHA)
endif()
add_definitions(-DOUTPUT_HAHA)
#ifdef OUTPUT_HAHA
cout<<"i use add_definitions OUTPUT_HAHA"<<endl;
#endif OUTPUT_HAHA
前面几篇文章,几乎都讲到了构建和编译等,今天在这里再介绍几个关于构建、编译、安装的小知识点。
cmake -B win_build
#cmake -B win_build -DCMAKE_INSTALL_PREFIX =~/install -DCMAKE_BUILD_TYPE=DEBUG
#cmake -B win_build -G "Visual Studio 16 2019"
cmake --build win_build
#等价于下面的命令
#devenv.com ./win_build/NEWHELLO.sln /Build "Debug|x64"
cmake --build win_build --target install
#等价于下面的命令
#devenv.com ./win_build/NEWHELLO.sln /build “Debug|x64” /project “INSTALL”