1.设置工程名
add_executable ( ${ PROJECT_NAME}
main. c
testFunc. c
testFunc1. c
testFunc2. c
testFunc3. c
)
2.当有多个源文件时,使用自动检索
aux_source_directory ( dir var)
add_executable ( ${ PROJECT_NAME}
${ var} )
3.变量设置,相等于程序里的=号
set ( SOURCES
. / main. c
. / testFunc1. c
. / testFunc. c)
4.使用include加入头文件搜索路径
project ( demo)
include_directories ( test_func test_func1)
aux_source_directory ( test_func SRC_LIST)
aux_source_directory ( test_func1 SRC_LIST1)
add_executable ( ${ PROJECT_NAME} main. c ${ SRC_LIST} ${ SRC_LIST1} )
5.使用target_include_directories
cmake_minimum_required ( VERSION 2.8 )
project ( demo)
set ( SOURCES
src/ testFunc. c
src/ testFunc1. c
src/ main. c
)
add_executable ( ${ PROJECT_NAME} ${ SOURCES} )
target_include_directories ( ${ PROJECT_NAME}
PRIVATE
${ PROJECT_SOURCE_DIR} / include
)
6.添加子文件夹的命令(能够适应生成多个exe的情况)
cmake_minimum_required (VERSION 2.8)
project (demo)
add_subdirectory (src)
//src/CMakeLists.txt 内容如下,这里是转至src/CMakeLists.txt继续运行
include_directories (${PROJECT_SOURCE_DIR}/include)
aux_source_directory (. SOURCES)
add_executable (${PROJECT_NAME} ${SOURCES})
//这里设置了可执行文件的输出路径信息:
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
7.消息输出
SET ( USER_KEY "Hello World" )
MESSAGE ( STATUS "this var key = ${USER_KEY}." )
8.编译一个静态库,供给可执行程序使用
cmake_minimum_required ( VERSION 3.5 )
project ( hello_library)
add_library ( hello_library STATIC
src/ hello. cpp
)
target_include_directories ( hello_library
PUBLIC
${ PROJECT_SOURCE_DIR} / include
)
# 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
)
9.生成动态库
cmake_minimum_required ( VERSION 3.5 )
project ( hello_library)
############################################################
# Create a library
############################################################
add_library ( hello_library SHARED
src/ Hello. cpp
)
add_library ( hello:: library ALIAS hello_library)
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
)
10.修改生成库的名字,并修改生成路径
aux_source_directory ( . SRC_LIST)
add_library ( testFunc_shared SHARED ${ SRC_LIST} )
add_library ( testFunc_static STATIC ${ SRC_LIST} )
set_target_properties ( testFunc_shared PROPERTIES OUTPUT_NAME "testFunc" )
set_target_properties ( testFunc_static PROPERTIES OUTPUT_NAME "testFunc" )
set ( LIBRARY_OUTPUT_PATH ${ PROJECT_SOURCE_DIR} / lib)
11.生成各类文件的安装
cmake_minimum_required ( VERSION 3.5 )
project ( cmake_examples_install)
############################################################
# Create a library
############################################################
# Generate the shared library from the library sources
add_library ( cmake_examples_inst SHARED
src/ hello. cpp
)
target_include_directories ( cmake_examples_inst
PUBLIC
${ PROJECT_SOURCE_DIR} / include
)
############################################################
# Create an executable
############################################################
# Add an executable with the above sources
add_executable ( cmake_examples_inst_bin
src/ main. cpp
)
# link the new hello_library target with the hello_binary target
target_link_libraries ( cmake_examples_inst_bin
PRIVATE
cmake_examples_inst
)
############################################################
# Install
############################################################
install ( TARGETS cmake_examples_inst_bin
DESTINATION bin)
install ( TARGETS cmake_examples_inst
LIBRARY DESTINATION lib)
install ( DIRECTORY ${ PROJECT_SOURCE_DIR} / include/
DESTINATION include)
install ( FILES cmake- examples. conf
DESTINATION etc)
INSTALL ( TARGETS myrun mylib mystaticlib
RUNTIME DESTINATION bin
LIBRARY DESTINATION lib
ARCHIVE DESTINATION libstatic)
set ( CMAKE_INSTALL_PREFIX "${CMAKE_BINARY_DIR}/install" )
12.添加第三方库
cmake_minimum_required ( VERSION 3.5 )
# Set the project name
project ( third_party_include)
find_package ( Boost 1.46 . 1 REQUIRED COMPONENTS filesystem system)
# check if boost was found
if ( Boost_FOUND)
message ( "boost found" )
include_directories ( ${ Boost_INCLUDE_DIRS} )
else ( )
message ( FATAL_ERROR "Cannot find Boost" )
endif ( )
add_executable ( ${ PROJECT_NAME} main. cpp)
# link against the boost libraries
target_link_libraries ( ${ PROJECT_NAME}
PRIVATE
Boost:: filesystem
)
# Include the boost headers
target_include_directories ( third_party_include
PRIVATE ${ Boost_INCLUDE_DIRS}
)
# link against the boost libraries
target_link_libraries ( third_party_include
PRIVATE
${ Boost_SYSTEM_LIBRARY}
${ Boost_FILESYSTEM_LIBRARY}
)
find_package ( Qt5 5.1 . 0 COMPONENTS Widgets Xml Sql)
SET ( LAPACK_DIR / usr/ local/ lib/ )
SET ( LAPACK_INCLUDE_DIRS / usr/ local/ include)
SET ( LAPACK_LIBRARIES / usr/ local/ lib)
13.操作系统类型检查
# -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
# O/ S Detection
# these are defined in common. h too
SET ( LINUX False)
SET ( FREEBSD False)
SET ( MACOS False)
# Detect the operating system
IF ( ${ CMAKE_SYSTEM_NAME} MATCHES "Darwin" )
SET ( TARGET_OS_NAME "macos" )
SET ( TARGET_OS 3 )
SET ( MACOS True)
ELSEIF ( ${ CMAKE_SYSTEM_NAME} MATCHES "FreeBSD" )
SET ( TARGET_OS_NAME "freebsd" )
SET ( TARGET_OS 2 )
SET ( FREEBSD True)
ELSE ( )
SET ( TARGET_OS_NAME "linux" )
SET ( TARGET_OS 1 )
SET ( LINUX True)
ENDIF ( )
# show the operating system on the console
message ( STATUS "operating system: ${TARGET_OS_NAME} (TARGET_OS=${TARGET_OS})" )
14.设置c++标准
cmake_minimum_required ( VERSION 3.1 )
project ( hello_cpp11)
set ( CMAKE_CXX_STANDARD 11 )
add_executable ( ${ PROJECT_NAME} main. cpp)