cmake 添加頭文件目錄,鏈接動態、靜態庫


羅列一下cmake常用的命令。

CMake支持大寫、小寫、混合大小寫的命令。

 

 

一個CMakeLists.txt的例子:

# CMake 最低版本號要求
cmake_minimum_required (VERSION 2.8)

# 項目信息
project (hello_mongoc2)

# 查找目錄下的所有源文件
# 並將名稱保存到 DIR_SRCS 變量
aux_source_directory(. DIR_SRCS)

# Specify the minimum version you require.
find_package (mongoc-1.0 1.7 REQUIRED)

# The "hello_mongoc.c" sample program is shared among four tests.
add_executable (hello_mongoc   ${DIR_SRCS})
target_link_libraries (hello_mongoc PRIVATE mongo::mongoc_shared)


##  添加頭文件目錄INCLUDE_DIRECTORIES
##它相當於g++選項中的-I參數的作用,也相當於環境變量中增加路徑到CPLUS_INCLUDE_PATH變量的作用。
# include_directories(../include)

##添加需要鏈接的庫文件目錄LINK_DIRECTORIES
##它相當於g++命令的-L選項的作用,也相當於環境變量中增加LD_LIBRARY_PATH的路徑的作用。
# link_directories("/home/server/third/lib")



## Specify the minimum version you require.
#find_package (mongoc-1.0 1.7 REQUIRED)
#
## The "hello_mongoc.c" sample program is shared among four tests.
#add_executable (hello_mongoc   ${DIR_SRCS})
#target_link_libraries (hello_mongoc PRIVATE mongo::mongoc_static)

 

 

 

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 (<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://www.cnblogs.com/binbinjx/p/5626916.html
https://www.cnblogs.com/binbinjx/p/5620056.html








免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM