c++ - 使用 Cmake 链接 libusb 时遇到问题

标签 c++ linux g++ cmake libusb

我一直在尝试将 2 个程序“破解”在一起,其中一个 (openTLD) 使用 cmake。我已经阅读并研究这个问题了一段时间,但似乎无法对其进行排序。

当我在没有 cpp 对象实例化的地方“make”时,它编译得很好,当我有一个我侵入的对象(依赖于 libusb)时,我遇到了链接(我认为)错误。

我的 CMakeLists(添加的位由 ** 或大写字母分隔)

#Set minimum version requered
cmake_minimum_required(VERSION 2.4.6)
#just to avoid the warning
if(COMMAND cmake_policy)
 cmake_policy(SET CMP0003 NEW)
endif(COMMAND cmake_policy)
#set project name
project(TLD)
#Append path to the module path
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR})
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/../cmake/Modules/")
#OpenCV
find_package(OpenCV REQUIRED)

#** ADDED **
find_package(libusb-1.0 REQUIRED)

#set the default path for built executables to the "bin" directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../bin)
#set the default path for built libraries to the "lib" directory
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../lib)
#set the include directories **CHANGED** added libusb
include_directories (${PROJECT_SOURCE_DIR}/../include   ${LIBUSB_1_INCLUDE_DIRS}                ${OpenCV_INCLUDE_DIRS})

#** ADDED **
SET(CMAKE_CXX_FLAGS "-lusb-1.0")

#libraries
add_library(tld_utils tld_utils.cpp)
add_library(LKTracker LKTracker.cpp)
add_library(ferNN FerNNClassifier.cpp)
add_library(tld TLD.cpp)

# **CHANGED** THIS CLASS COMPILES
add_library(servo servo.cpp)

#executables
# ** WHEN I TRY TO ISTANTIATE 'SERVO' IN 'tld.cpp' THAT IS ISTANTIATED THIS CLASS I GET     PROBLEMS
add_executable(run_tld run_tld.cpp)

#link the libraries **CHANGED** added servo and libusb
target_link_libraries(run_tld tld LKTracker ferNN tld_utils servo ${libusb-1.0__LIBS}     ${OpenCV_LIBS})
#set optimization level 
set(CMAKE_BUILD_TYPE Release)

终端的输出是:

lewis@lewis-desktop:~/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/build$ cmake ../src/
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /usr/bin/gcc
-- Check for working C compiler: /usr/bin/gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Found libusb-1.0:
--  - Includes: /usr/include/libusb-1.0
--  - Libraries: /usr/lib/x86_64-linux-gnu/libusb-1.0.so
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/build
lewis@lewis-desktop:~/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/build$ make
Scanning dependencies of target LKTracker
[ 16%] Building CXX object CMakeFiles/LKTracker.dir/LKTracker.o
Linking CXX static library /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libLKTracker.a
[ 16%] Built target LKTracker
Scanning dependencies of target ferNN
[ 33%] Building CXX object CMakeFiles/ferNN.dir/FerNNClassifier.o
Linking CXX static library /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libferNN.a
[ 33%] Built target ferNN
Scanning dependencies of target tld_utils
[ 50%] Building CXX object CMakeFiles/tld_utils.dir/tld_utils.o
Linking CXX static library /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libtld_utils.a
[ 50%] Built target tld_utils
Scanning dependencies of target servo
[ 66%] Building CXX object CMakeFiles/servo.dir/servo.o
Linking CXX static library /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libservo.a
[ 66%] Built target servo
Scanning dependencies of target tld
[ 83%] Building CXX object CMakeFiles/tld.dir/TLD.o
Linking CXX static library /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libtld.a
[ 83%] Built target tld
Scanning dependencies of target run_tld
[100%] Building CXX object CMakeFiles/run_tld.dir/run_tld.o
Linking CXX executable /home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/bin/run_tld
/home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libservo.a(servo.o): In function `servo::servo()':
servo.cpp:(.text+0xa): undefined reference to `libusb_init'
servo.cpp:(.text+0x1b): undefined reference to `libusb_get_device_list'
/home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libservo.a(servo.o): In function `servo::deviceMatchesVendorProduct(libusb_device*, unsigned short, unsigned short)':
servo.cpp:(.text+0x49): undefined reference to `libusb_get_device_descriptor'
/home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/lib/libservo.a(servo.o): In function `servo::setTarget(int)':
servo.cpp:(.text+0xc1): undefined reference to `libusb_get_device_descriptor'
servo.cpp:(.text+0xe3): undefined reference to `libusb_get_device_descriptor'
servo.cpp:(.text+0x105): undefined reference to `libusb_get_device_descriptor'
servo.cpp:(.text+0x143): undefined reference to `libusb_get_device_descriptor'
servo.cpp:(.text+0x170): undefined reference to `libusb_open'
servo.cpp:(.text+0x19c): undefined reference to `libusb_control_transfer'
servo.cpp:(.text+0x1a6): undefined reference to `libusb_close'
servo.cpp:(.text+0x1c2): undefined reference to `libusb_free_device_list'
servo.cpp:(.text+0x1ce): undefined reference to `libusb_exit'
collect2: ld returned 1 exit status
make[2]: *** [/home/lewis/Desktop/mcleanlewis-OPENTLD_blackbox-ce389c3/bin/run_tld] Error 1
make[1]: *** [CMakeFiles/run_tld.dir/all] Error 2
make: *** [all] Error 2

我的servo.cpp文件,以防它有问题(我不是cpp程序员,它已经被一起黑客攻击了)

#include <iostream>
#include <libusb-1.0/libusb.h>
#include "protocol.h"
#include "servo.h"


using namespace std;

 int count=0;
const unsigned short vendorId = 0x1ffb;
    unsigned short productIDArray[]={0x0089, 0x008a, 0x008b, 0x008c};
    libusb_context *ctx=0;
    libusb_device **device_list=0;

servo::servo(){
    libusb_init(&ctx);
    count=libusb_get_device_list(ctx, &device_list);
}

bool servo::deviceMatchesVendorProduct(libusb_device *device, unsigned short idVendor, unsigned short idProduct)
{
    libusb_device_descriptor desc;
    libusb_get_device_descriptor(device, &desc);
    return idVendor == desc.idVendor && idProduct == desc.idProduct;
}

void servo::setTarget(int position)
{
    for(int i=0;i<count;i++)
    {
        libusb_device *device=device_list[i];
        {
            for(int Id=0;Id<4;Id++)
            {
                if(deviceMatchesVendorProduct(device, vendorId, productIDArray[Id]))
                {
                    libusb_device_handle *device_handle;
                    libusb_open(device, &device_handle);
                    libusb_control_transfer(device_handle, 0x40, REQUEST_SET_TARGET, position*4, 0, 0, 0, (ushort)5000);
                    libusb_close(device_handle);
                    break;
                }
            }
        }
    }
    libusb_free_device_list(device_list, 0);
    libusb_exit(ctx);
}

感谢您的帮助或指点。

最佳答案

这确实是一个链接器错误。看来您的 libusb-1.0 库变量可能有误。尝试将您的 target_link_libraries 命令更改为:

target_link_libraries(run_tld tld LKTracker ferNN tld_utils servo ${LIBUSB_1_LIBRARIES} ${OpenCV_LIBS})

您可能还应该删除 SET(CMAKE_CXX_FLAGS "-lusb-1.0"),因为链接将由 target_link_libraries 命令处理。

关于c++ - 使用 Cmake 链接 libusb 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10020231/

相关文章:

c++ - 共享内存中的映射

linux - 使用 Sed 将文本附加到现有的第一行

c++ - 使用 std::shared_ptr 时的内存新删除类型不匹配

c++ - g++ 不会在模板代码中发出 -Wsign-compare

c++ - 使用 FileStorage API 加载的 cv::Mat 的可用内存

python - 如何设置 python 异常消息以使用特殊语言?

c++ - 使用命名空间和文件夹来组织代码是否太过分了?

c++ - 如何在STL中使用xtree调用一个简单的红黑树?

linux - 使用 Linux 排序来获得标准输出结果

linux - 我已经在Windows中使用Strawberry Perl在Windows中运行的脚本无法在Unix机器中使用。