CMake使用總結(一)

當咱們在寫CMakeLists.txt文件時,經常會搞不明白link_directories, LINK_LIBRARIES, target_link_libraries這3者的區別,下面就其詳細介紹:
 
  • INCLUDE_DIRECTORIES(添加頭文件目錄)
它至關於g++選項中的-I參數的做用,也至關於環境變量中增長路徑到CPLUS_INCLUDE_PATH變量的做用(這裏特指c++。c和Java中用法相似)。
 
好比:
include_directories("/opt/MATLAB/R2012a/extern/include")
 
 
  • LINK_DIRECTORIES(添加須要連接的庫文件目錄)
語法:
link_directories(directory1 directory2 ...)
它至關於g++命令的-L選項的做用,也至關於環境變量中增長LD_LIBRARY_PATH的路徑的做用。
 
好比:
LINK_DIRECTORIES("/opt/MATLAB/R2012a/bin/glnxa64")
 
  • LINK_LIBRARIES (添加須要連接的庫文件路徑,注意這裏是全路徑)
List of direct link dependencies.
 
好比:
LINK_LIBRARIES("/opt/MATLAB/R2012a/bin/glnxa64/libeng.so")
LINK_LIBRARIES("/opt/MATLAB/R2012a/bin/glnxa64/libmx.so")
 
也能夠寫成:
LINK_LIBRARIES("/opt/MATLAB/R2012a/bin/glnxa64/libeng.so" "/opt/MATLAB/R2012a/bin/glnxa64/libmx.so")
 
 
  • TARGET_LINK_LIBRARIES (設置要連接的庫文件的名稱)
語法:TARGET_LINK_LIBRARIES(targetlibrary1 <debug | optimized> library2 ..)
 
好比(如下寫法(包括備註中的)均可以):
TARGET_LINK_LIBRARIES(myProject hello),鏈接libhello.so庫
TARGET_LINK_LIBRARIES(myProject libhello.a)
TARGET_LINK_LIBRARIES(myProject libhello.so)
 
再如:
TARGET_LINK_LIBRARIES(myProject #這些庫名寫法均可以。
TARGET_LINK_LIBRARIES(myProject TARGET_LINK_LIBRARIES(myProject -leng)
 
 
CMakeLists文件連接方式有以下2種:
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)
include_directories("/opt/MATLAB/R2012a/extern/include")     
LINK_DIRECTORIES("/opt/MATLAB/R2012a/bin/glnxa64")       
add_executable(myProject main.cpp)      
target_link_libraries(myProject eng mx)      
#equals to below  
#target_link_libraries(myProject -leng -lmx)  
#target_link_libraries(myProject libeng.so libmx.so) 
cmake_minimum_required(VERSION 2.8 FATAL_ERROR)  
include_directories("/opt/MATLAB/R2012a/extern/include")   
#directly link to the libraries.  
LINK_LIBRARIES("/opt/MATLAB/R2012a/bin/glnxa64/libeng.so")  
LINK_LIBRARIES("/opt/MATLAB/R2012a/bin/glnxa64/libmx.so") 
#equals to below  
#LINK_LIBRARIES("/opt/MATLAB/R2012a/bin/glnxa64/libeng.so" "/opt/MATLAB/R2012a/bin/glnxa64/libmx.so")  
add_executable(myProject main.cpp
相關文章
相關標籤/搜索