在cmake c ++项目中包含库的首选方法是什么? [英] What's the preferred way to include a library in a cmake c++ project?

查看:185
本文介绍了在cmake c ++项目中包含库的首选方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经建立了libfreenect2库,现在我想把它包含在我的c ++项目中。之前,我已经包括一些库的cmake像这样:

I've built the libfreenect2 library, now I want to include it in my c++ project. Before, I've included some libraries with cmake like this:

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

这意味着

但是,这次我需要手动将必要的文件和目录包含到我的项目中。

However, this time I need to "manually" include the necessary files and directories to my project. But I don't have a clue on how the "correct" way to do it.

按照这个教程,但它是令人困惑的如何我要添加库,包括目录,添加子目录(为什么突然添加而不是包括),链接库。 ..术语不一致,还是方法总是真的这么乱?我不明白为什么它只是表达库目录 ONCE 不够,那么cmake应该弄清楚该怎么办呢?对不起,我的无知。

Been following this tutorial, but it's confusing how I have to add library, include directories, add subdirectories (why is it suddenly "add" and not "include"), link libraries... Is the terminology inconsistent, or is the approach always really this messy? I can't see why it wouldn't be enough to just express the library directory ONCE then cmake should figure out what to do with it? Sorry about my ignorance.

无论如何,包括一个自定义的图书馆的首选步骤是什么?

Anyways, what's the preferred steps to include a customly built library?

我的当前尝试,(当我尝试编译我的项目)产生不能找到-lfreenect2

This is my current attempt, which (when I try to compile my project) yields "cannot find -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)


推荐答案

这是我最后做的:


  • 设置cmake前缀以使其找到配置文件命名为freenect2Config.cmake)

  • 查找包,使cmake运行find script


  • 链接库文件

  • Set cmake prefix to enable it to find the config file (named "freenect2Config.cmake")
  • "Find" the "package", which makes cmake run the "find script"
  • Include directories to get headers
  • Link library files

CMakeLists.txt

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})

这篇关于在cmake c ++项目中包含库的首选方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆