簡體   English   中英

在cmake c ++項目中包含庫的首選方法是什么?

[英]What's the preferred way to include a library in a cmake c++ project?

我已經構建了libfreenect2庫,現在我想將它包含在我的c ++項目中。 之前,我已經在cmake中包含了一些像這樣的庫:

# Include OpenCV
find_package( OpenCV REQUIRED )
target_link_libraries( ${PROJECT_NAME} ${OpenCV_LIBS} )

這意味着必須將庫正確“安裝”到我的系統才能讓cmake找到它,對嗎?

但是,這次我需要“手動”將必要的文件和目錄包含到我的項目中。 但我不知道如何“正確”地做到這一點。

一直在關注本教程 ,但令人困惑的是如何添加庫,包含目錄,添加子目錄(為什么它突然“添加”而不是“包含”),鏈接庫......術語是否不一致,或者總是方法真的這么亂嗎? 我不明白為什么僅僅表達庫目錄ONCE是不夠的,然后cmake應該弄清楚該怎么辦呢? 抱歉我的無知。

無論如何,包含定制構建庫的首選步驟是什么?

這是我目前的嘗試,(當我嘗試編譯我的項目時)產生“找不到-lfreenect2”

project(kinect-test)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)

add_executable(${PROJECT_NAME} ${SRC_LIST})

# Include directories
include_directories ($ENV{HOME}/freenect2/include)

# Find freenect package
set(freenect2_DIR $ENV{HOME}/freenect2/lib/cmake/freenect2)
find_package(freenect2 REQUIRED)
target_link_libraries (${PROJECT_NAME} freenect2)

您需要指定可以找到給定庫的搜索路徑。 我假設文件libfreenect.so (或libfreenect.a )位於/path/to/freenect - 請根據需要進行編輯,可能使用變量。 target_link_libraries命令上方添加命令:

link_directories(/path/to/freenect)

這就是我最終做的事情:

  • 設置cmake前綴以使其能夠找到配置文件(名為“freenect2Config.cmake”)
  • “查找”“包”,這使得cmake運行“查找腳本”
  • 包含目錄以獲取標頭
  • 鏈接庫文件

的CMakeLists.txt

project(kinect-test)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(. SRC_LIST)

add_executable(${PROJECT_NAME} ${SRC_LIST})

# Set cmake prefix path to enable cmake to find freenect2
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} $ENV{HOME}/freenect2/lib/cmake/freenect2)

# Find freenect, to set necessary variables
find_package(freenect2 REQUIRED)

# Include directories to get freenect headers
include_directories($ENV{HOME}/freenect2/include)

# Link freenect libraries with the project
target_link_libraries(${PROJECT_NAME} ${freenect2_LIBRARIES})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM