羅列一下cmake經常使用的命令。html
CMake支持大寫、小寫、混合大小寫的命令。ui
1. 添加頭文件目錄INCLUDE_DIRECTORIESspa
語法:debug
include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])
它至關於g++選項中的-I參數的做用,也至關於環境變量中增長路徑到CPLUS_INCLUDE_PATH變量的做用。code
include_directories(../../../thirdparty/comm/include)
2. 添加須要連接的庫文件目錄LINK_DIRECTORIESserver
語法:htm
link_directories(directory1 directory2 ...)
它至關於g++命令的-L選項的做用,也至關於環境變量中增長LD_LIBRARY_PATH的路徑的做用。blog
link_directories("/home/server/third/lib")
3. 查找庫所在目錄FIND_LIBRARYmd5
語法:文檔
A short-hand signature is: find_library (<VAR> name1 [path1 path2 ...]) The general signature is: find_library ( <VAR> name | NAMES name1 [name2 ...] [NAMES_PER_DIR] [HINTS path1 [path2 ... ENV var]] [PATHS path1 [path2 ... ENV var]] [PATH_SUFFIXES suffix1 [suffix2 ...]] [DOC "cache documentation string"] [NO_DEFAULT_PATH] [NO_CMAKE_ENVIRONMENT_PATH] [NO_CMAKE_PATH] [NO_SYSTEM_ENVIRONMENT_PATH] [NO_CMAKE_SYSTEM_PATH] [CMAKE_FIND_ROOT_PATH_BOTH | ONLY_CMAKE_FIND_ROOT_PATH | NO_CMAKE_FIND_ROOT_PATH] )
例子以下:
FIND_LIBRARY(RUNTIME_LIB rt /usr/lib /usr/local/lib NO_DEFAULT_PATH)
cmake會在目錄中查找,若是全部目錄中都沒有,值RUNTIME_LIB就會被賦爲NO_DEFAULT_PATH
4. 添加須要連接的庫文件路徑LINK_LIBRARIES
語法:
link_libraries(library1 <debug | optimized> library2 ...)
# 直接是全路徑
link_libraries(「/home/server/third/lib/libcommon.a」)
# 下面的例子,只有庫名,cmake會自動去所包含的目錄搜索
link_libraries(iconv)
# 傳入變量
link_libraries(${RUNTIME_LIB})
# 也能夠連接多個
link_libraries("/opt/MATLAB/R2012a/bin/glnxa64/libeng.so" "/opt/MATLAB/R2012a/bin/glnxa64/libmx.so")
能夠連接一個,也能夠多個,中間使用空格分隔.
5. 設置要連接的庫文件的名稱TARGET_LINK_LIBRARIES
語法:
target_link_libraries(<target> [item1 [item2 [...]]]
[[debug|optimized|general] <item>] ...)
# 如下寫法均可以:
target_link_libraries(myProject comm) # 鏈接libhello.so庫,默認優先連接動態庫
target_link_libraries(myProject libcomm.a) # 顯示指定連接靜態庫
target_link_libraries(myProject libcomm.so) # 顯示指定連接動態庫
# 再如:
target_link_libraries(myProject libcomm.so) #這些庫名寫法均可以。
target_link_libraries(myProject comm)
target_link_libraries(myProject -lcomm)
6. 爲工程生成目標文件
語法:
add_executable(<name> [WIN32] [MACOSX_BUNDLE]
[EXCLUDE_FROM_ALL]
source1 [source2 ...])
簡單的例子以下:
add_executable(demo main.cpp )
6. 最後貼一個完整的例子
cmake_minimum_required (VERSION 2.6) INCLUDE_DIRECTORIES(../../thirdparty/comm) FIND_LIBRARY(COMM_LIB comm ../../thirdparty/comm/lib NO_DEFAULT_PATH) FIND_LIBRARY(RUNTIME_LIB rt /usr/lib /usr/local/lib NO_DEFAULT_PATH) link_libraries(${COMM_LIB} ${RUNTIME_LIB}) ADD_DEFINITIONS( -O3 -g -W -Wall -Wunused-variable -Wunused-parameter -Wunused-function -Wunused -Wno-deprecated -Woverloaded-virtual -Wwrite-strings -D__WUR= -D_REENTRANT -D_FILE_OFFSET_BITS=64 -DTIXML_USE_STL ) add_library(lib_demo cmd.cpp global.cpp md5.cpp ) link_libraries(lib_demo) add_executable(demo main.cpp ) # link library in static mode target_link_libraries(demo libuuid.a)
另外,使用cmake生成makefile以後,make edit_cache能夠編輯編譯選項。
不熟悉的命令能夠去查找文檔,貼個cmake3.0官方幫助文檔地址
https://cmake.org/cmake/help/v3.0/index.html==============================================最近在搭建本身的博客,歡迎來逛。https://sharebook.wang