# CMakeLists.txt # Set the minimum version of CMake that can be used # To find the cmake version run # $ cmake --version cmake_minimum_required(VERSION 3.5)
# Set the project name project (hello_cmake)
# Add an executable add_executable(hello_cmake main.cpp) # add_executable(${PROJECT_NAME} main.cpp)
#################################### # Create a library ####################################
add_library(hello_library STATIC src/Hello.cpp )
target_include_directories(hello_library PUBLIC ${PROJECT_SOURCE_DIR}/include )
################################### # Create an executable ################################### # Add an executable with the above sources add_executable(hello_binary src/main.cpp )
# link the new hello_library target with the hello_binary target target_link_libraries( hello_binary PRIVATE hello_library )
# set make install directory if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "The path to use for make install" FORCE) endif()
# Debug for CMAKE_INSTALL_PREFIX message("CMAKE_INSTALL_PREFIX: "${CMAKE_INSTALL_PREFIX})
# 在未指定install目录时,可以在CMakeLists.txt中设置make install的目录 if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) set(CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" CACHE PATH "The path to use for make install" FORCE) endif()
# 在make install也可以通过DESTDIR=/tmp/stage,安装到${DESTDIR}/${CMAKE_INSTALL_PREFIX} make install DESTDIR=/tmp/stage
# Set a default build type if none was specified # Release, Debug, MinSizeRel, RelWithDebInfo if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) message("Setting build type to 'RelWithDebInfo' as none was specified.") set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE) # Set the possible values of build type for cmake-gui set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug""Release" "MinSizeRel""RelWithDebInfo") endif()
# 设置默认的构建类型 # Release, Debug, MinSizeRel, RelWithDebInfo if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) message("Setting build type to 'RelWithDebInfo' as none was specified.") set(CMAKE_BUILD_TYPE RelWithDebInfo CACHE STRING "Choose the type of build." FORCE) # Set the possible values of build type for cmake-gui set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug""Release" "MinSizeRel""RelWithDebInfo") endif()
compile flags
1 2 3 4
$ tree . ├── CMakeLists.txt ├── main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// main.cpp #include<iostream>
intmain(int argc, char** argv){
std::cout << "Hello Compile Flags!" << std::endl;
// only print if compile flag set #ifdef EX2 std::cout << "Hello Compile Flag EX2!" << std::endl; #endif
# check results and add flag 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()
add_executable(${PROJECT_NAME} main.cpp)
另外还有两种设置c++ standard的方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
####################################################################### # Using the CMAKE_CXX_STANDARD variable introduced in CMake v3.1. ####################################################################### set(CMAKE_CXX_STANDARD 11)
####################################################################### # Using the target_compile_features function introduced in CMake v3.1. ####################################################################### # set the C++ standard to the appropriate standard for using auto target_compile_features(hello_cpp11 PUBLIC cxx_auto_type)
# print the list of known compile features for this version of CMake message("List of compile features: ${CMAKE_CXX_COMPILE_FEATURES}")
# subbinary/CMakeLists.txt # set project name project(subbinary)
# create the executable add_executable(${PROJECT_NAME} main.cpp)
# link the static library from sublibrary1 using it's alias sub::lib1 # link the header only library from sublibrary2 using it's alias sub::lib2 # this will cause the include directories for that target to be added to this project target_link_libraries(${PROJECT_NAME} sub::lib1 sub::lib2 )
code generation
configure files
CMakeLists.txt - Contains the CMake commands you wish to run
main.cpp - The source file with main
path.h.in - File to contain a path to the build directory
ver.h.in - File to contain the version of the project
# set a project version set(cf_example_VERSION_MAJOR 0) set(cf_example_VERSION_MINOR 2) set(cf_example_VERSION_PATCH 1) set(cf_example_VERSION "${cf_example_VERSION_MAJOR}.${cf_example_VERSION_MINOR}.${cf_example_VERSION_PATCH}")
# call configure files on ver.h to set the version # uses the standard ${VARIABLE} syntax in the file configure_file(ver.h.in ${PROJECT_SOURCE_DIR}/ver.h)
# this file can only use the @VARIABLE syntax in the file configure_file(path.h.in ${PROJECT_SOURCE_DIR}/path.h @ONLY)