CMake新增標頭檔案,庫檔案,鏈結庫檔案

2021-07-24 01:44:24 字數 3801 閱讀 3773

羅列一下cmake常用的命令。

cmake支援大寫、小寫、混合大小寫的命令。

1. 新增頭檔案目錄include_directories

語法:

include_directories([after|before] [system] dir1 [dir2 ...])
它相當於g++選項中的-i引數的作用,也相當於環境變數中增加路徑到cplus_include_path變數的作用。

include_directories

(../../../thirdparty/comm/include)

2. 新增需要鏈結的庫檔案目錄link_directories

語法:

link_directories(directory1 directory2 ...)
它相當於g++命令的-l選項的作用,也相當於環境變數中增加ld_library_path的路徑的作用。

link_directories("/home/server/third/lib")
3. 查詢庫所在目錄find_library

語法:

a short-hand signature is:

find_library (

name1 [path1 path2 ...])

the general signature is:

find_library (

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 library2 ...)

# 直接是全路徑

link_libraries(「/home/server/third/lib/libcommon.a」)

# 下面的例子,只有庫名,cmake會自動去所包含的目錄搜尋

link_libraries(iconv)

# 傳入變數

link_libraries($)

# 也可以鏈結多個

link_libraries("/opt/matlab/r2012a/bin/glnxa64/libeng.so" "/opt/matlab/r2012a/bin/glnxa64/libmx.so")

可以鏈結乙個,也可以多個,中間使用空格分隔.

5. 設定要鏈結的庫檔案的名稱target_link_libraries

語法:

target_link_libraries([item1 [item2 [...]]]

[[debug|optimized|general] ] ...)

# 以下寫法都可以: 

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([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($ $)

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.

cppglobal.

cppmd5.

cpp)

link_libraries(lib_demo)

add_executable(demo

main.

cpp)

# link library

instatic mode

target_link_libraries(demo libuuid.a)

另外,使用cmake生成makefile之後,make edit_cache可以編輯編譯選項。

不熟悉的命令可以去查詢文件,貼個cmake3.0官方幫助文件位址

cmake 新增頭檔案目錄,鏈結動態 靜態庫

羅列一下cmake常用的命令。cmake支援大寫 小寫 混合大小寫的命令。1.新增頭檔案目錄include directories 語法 include directories after before system dir1 dir2 它相當於g 選項中的 i引數的作用,也相當於環境變數中增加路徑...

cmake 新增頭檔案目錄,鏈結動態 靜態庫

羅列一下cmake常用的命令。cmake支援大寫 小寫 混合大小寫的命令。1.新增頭檔案目錄include directories 語法 include directories after before system dir1 dir2 它相當於g 選項中的 i引數的作用,也相當於環境變數中增加路徑...

cmake 新增頭檔案目錄,鏈結動態 靜態庫

原文 cmake 新增頭檔案目錄,鏈結動態 靜態庫 table of contents 1.新增頭檔案目錄include directories 2.新增需要鏈結的庫檔案目錄link directories 3.查詢庫所在目錄find library 4.新增需要鏈結的庫檔案路徑link libra...