如何链接第三方库(LibUSB)在CMake [英] How to Link a third Party Library (LibUSB) in CMake
问题描述
我试图在一个项目中使用LibUSB。但是,每当我尝试使用基本的libUSB函数时,我得到以下错误:
I am attempting to use LibUSB in a project. However whenever I attempt to use basic libUSB functions I get the following error:
...src/main/main.cpp.o: In function `main':
...src/main/main.cpp:10: undefined reference to `libusb_init'
...src/main/main.cpp:11: undefined reference to `libusb_set_debug'
collect2: error: ld returned 1 exit status
包LibusB- devel安装(我在fedora 22),我的IDE KDevelop查找和识别头,一旦你添加了import语句,它提供LibUSB代码完成。我在我的IDE或CMake(我的构建系统)没有任何自定义包含行,所以我想知道我需要使CMake找到LibUSB头。
The package LibUSB-devel is installed (I'm on fedora 22) and my IDE KDevelop finds and recognises the headers, to the point it offers LibUSB code completions once you have added the import statement. I don't have any custom include lines in either my IDE or CMake (my build system) so I would like to know what I need to to to make CMake find the LibUSB headers.
这是 main.cpp
的内容,以防万一我乱七八糟:
This is the contents of main.cpp
, just in case I messed something up:
#include <iostream>
#include <libusb-1.0/libusb.h>
int main(int argc, char **argv) {
libusb_init(NULL);
libusb_set_debug(NULL, LIBUSB_LOG_LEVEL_WARNING);
/*snip*/
std::cout << "Hello, world! PTPID=" << std::endl;
return 0;
}
以下是 CMakeLists.txt
:
../
The following are the CMakeLists.txt
:
../
cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_BUILD_TYPE Debug)
project(aProjectThatHasHadIt'sNameObcured)
add_subdirectory(src)
... / src / cmakelists.txt只是添加子目录
.../src/cmakelists.txt just adds subdirectories
... / src / main /
.../src/main/
add_executable(main main.cpp)
推荐答案
从您的项目 CMakeLists.txt
文件它不会变得明显我如何你试图链接libusb。我的方法如下:
From your projects CMakeLists.txt
file it doesn't become apparent to me how you've tried to link libusb. The way I would do is the following:
target_link_libraries(project_name< other_dependencies> usb-1.0)
(只是为了说明我在添加可执行文件的CMakeLIsts.txt文件)
(Just to clarify I mean the CMakeLIsts.txt file in which you add your executable)
您尝试从< libusb-1.0 /...& gt;
导入,因此您需要链接usb-1.0(lib是 always 从链接器命令中省略!)
You're trying to import from <libusb-1.0/...>
thus you need to link usb-1.0 (the lib is always omitted from linker commands!)
我在Fedora 23上,也使用KDevelop,我没有指定路径。特别是因为在我的系统上所有在前面的答案中使用的环境变量,无论如何。
I'm on Fedora 23, also using KDevelop and I didn't have to specify a path. Especially because on my system all the environment variables used in the previous answer are NULL anyways.
并确认库的安装在未来,你可以做:
找到libusb | grep .so
And to confirm where and how a library is installed in the future you can just do:
locate libusb | grep .so
希望这有些帮助。
这篇关于如何链接第三方库(LibUSB)在CMake的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!