cmake备忘

获取文件夹下的所有文件

  1. aux_source_directory(<dir>, <variable>) collects the names of all source files in the specified directory and store the list in the variable provided.

1
2
3
4
AUX_SOURCE_DIRECTORY(libraries/api API_SRC)
AUX_SOURCE_DIRECTORY(libraries/net NET_SRC)
AUX_SOURCE_DIRECTORY(libraries/rpc RPC_SRC)

  1. file(GLOB <variable> [<globbing-expressions>...]) generate a list of files that match and store it in variable.

1
2
3
4
5
6
7
8
9
file( GLOB LEVEL_DB_SOURCES "${LEVEL_DB_DIR}/db/*.cc"
"${LEVEL_DB_DIR}/helpers/memenv/memenv.cc"
"${LEVEL_DB_DIR}/table/*.cc"
"${LEVEL_DB_DIR}/util/*.cc" )
foreach( filename ${LEVEL_DB_SOURCES} )
if( ${filename} MATCHES ".*_test.cc" OR ${filename} MATCHES ".*_bench.cc" OR ${filename} MATCHES ".*_main.cc" )
list( REMOVE_ITEM LEVEL_DB_SOURCES ${filename} )
endif()
endforeach()

执行(内嵌)目录下的CMakeList.txt

include(path\CMakeList.txt)

获取路径信息/文件名信息

get_filenamecomponent(<VAR> <FileName> <COMP> [CACHE])

1
2
get_filenamecomponent(dir "${CMAKE_CURRENT_LIST_FILE}" PATH)
set(SRCS ${dir}/special.c)

信息的输出

  1. message([<mode>] "messages to display" ...)

添加include以及lib

  • impact on directory scope
  1. include_directories(path...)
  2. link_libraries(path...)
  • only impact on target
  1. target_include_directories()
  2. target_link_libraries()

匹配编译器

CMAKE_[language]_COMPILER_ID

1
2
3
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
commands
endif()
-------------本文结束感谢您的阅读-------------