使用 cyusb 对 libusb 的未定义引用
undefined reference to libusb using cyusb
我已经下载了 linux cypress SDK EZ-USB FX3 Software Development Kit 软件。 Cypress/cyusb_linux_1.0.5/src
里面有很多 .cpp
文件,我用 make 编译了它们,它们运行得很好。我想做我自己的 cpp 项目,所以我复制了其中一个文件的代码并尝试使用 CMakeLists.txt
编译它。我有以下结构:
project/
├── include/
│ └── cyusb.h
├── src/
│ └── libcyusb.cpp
├── CMakeLists.txt
└── main.cpp
当我这样做时:
$apt list --installed | grep -i libusb
libusb-1.0-0/bionic,now 2:1.0.21-2 amd64 [installed,automatic]
libusb-1.0-0-dev/bionic,now 2:1.0.21-2 amd64 [installed]
但是,在 this 之后,我在执行 pkg-config --libs libusb
时没有得到任何回应,但我确实找到了一些内容:
$pkg-config --libs libusb-1.0
-lusb-1.0
所以我将它添加到我的 CMakeLists.txt 中,现在它看起来像这样:
cmake_minimum_required(VERSION 3.10)
project(project)
set(CMAKE_CXX_STANDARD 14)
SET(GCC_COVERAGE_LINK_FLAGS "-lusb-1.0")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
file(GLOB SOURCES
include/*.h
src/*.cpp
main.cpp
)
add_executable(project ${SOURCES})
当我点击 make
时,我得到:
[ 33%] Linking CXX executable project
CMakeFiles/project.dir/src/libcyusb.cpp.o: In function `libusb_get_descriptor':
/usr/include/libusb-1.0/libusb.h:1777: undefined reference to `libusb_control_transfer'
CMakeFiles/project.dir/src/libcyusb.cpp.o: In function `libusb_get_string_descriptor':
/usr/include/libusb-1.0/libusb.h:1799: undefined reference to `libusb_control_transfer'
CMakeFiles/project.dir/src/libcyusb.cpp.o: In function `device_is_of_interest(libusb_device*)':
project/src/libcyusb.cpp:157: undefined reference to `libusb_get_device_descriptor'
CMakeFiles/project.dir/src/libcyusb.cpp.o: In function `renumerate()':
project/src/libcyusb.cpp:208: undefined reference to `libusb_get_device_list'
project/src/libcyusb.cpp:219: undefined reference to `libusb_open'
[...] ETC [...] (you got the idea...)
您需要添加带有标准CMake机制的库。
target_link_libraries(project usb-1.0)
变量 GCC_COVERAGE_LINK_FLAGS
未在正常构建中使用。
我已经下载了 linux cypress SDK EZ-USB FX3 Software Development Kit 软件。 Cypress/cyusb_linux_1.0.5/src
里面有很多 .cpp
文件,我用 make 编译了它们,它们运行得很好。我想做我自己的 cpp 项目,所以我复制了其中一个文件的代码并尝试使用 CMakeLists.txt
编译它。我有以下结构:
project/
├── include/
│ └── cyusb.h
├── src/
│ └── libcyusb.cpp
├── CMakeLists.txt
└── main.cpp
当我这样做时:
$apt list --installed | grep -i libusb
libusb-1.0-0/bionic,now 2:1.0.21-2 amd64 [installed,automatic]
libusb-1.0-0-dev/bionic,now 2:1.0.21-2 amd64 [installed]
但是,在 this 之后,我在执行 pkg-config --libs libusb
时没有得到任何回应,但我确实找到了一些内容:
$pkg-config --libs libusb-1.0
-lusb-1.0
所以我将它添加到我的 CMakeLists.txt 中,现在它看起来像这样:
cmake_minimum_required(VERSION 3.10)
project(project)
set(CMAKE_CXX_STANDARD 14)
SET(GCC_COVERAGE_LINK_FLAGS "-lusb-1.0")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS}")
file(GLOB SOURCES
include/*.h
src/*.cpp
main.cpp
)
add_executable(project ${SOURCES})
当我点击 make
时,我得到:
[ 33%] Linking CXX executable project
CMakeFiles/project.dir/src/libcyusb.cpp.o: In function `libusb_get_descriptor':
/usr/include/libusb-1.0/libusb.h:1777: undefined reference to `libusb_control_transfer'
CMakeFiles/project.dir/src/libcyusb.cpp.o: In function `libusb_get_string_descriptor':
/usr/include/libusb-1.0/libusb.h:1799: undefined reference to `libusb_control_transfer'
CMakeFiles/project.dir/src/libcyusb.cpp.o: In function `device_is_of_interest(libusb_device*)':
project/src/libcyusb.cpp:157: undefined reference to `libusb_get_device_descriptor'
CMakeFiles/project.dir/src/libcyusb.cpp.o: In function `renumerate()':
project/src/libcyusb.cpp:208: undefined reference to `libusb_get_device_list'
project/src/libcyusb.cpp:219: undefined reference to `libusb_open'
[...] ETC [...] (you got the idea...)
您需要添加带有标准CMake机制的库。
target_link_libraries(project usb-1.0)
变量 GCC_COVERAGE_LINK_FLAGS
未在正常构建中使用。