问题
I've been trying to 'hack' 2 programs together and one of them (openTLD) uses cmake. I've been reading up and working on this issue for a bit now and can't seem to sort it.
When I 'make' where there is no instantiation of the cpp object it compiles fine, when I have an object (that relies on libusb) that I hacked in I'm getting linking (I think) errors.
My CMakeLists (added bits delimeted by ** or CAPS)
#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)
The output from terminal is :
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
My servo.cpp file incase there is an issue with it (I'm not a cpp programmer and it's been hacked together)
#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);
}
Thanks for any assistance or pointers.
回答1:
This is indeed a linker error. It looks like you might have the wrong variable for the libusb-1.0 libraries. Try changing your target_link_libraries
command to:
target_link_libraries(run_tld tld LKTracker ferNN tld_utils servo ${LIBUSB_1_LIBRARIES} ${OpenCV_LIBS})
You probably also should remove SET(CMAKE_CXX_FLAGS "-lusb-1.0")
, since linking will be handled by the target_link_libraries
command.
来源:https://stackoverflow.com/questions/10020231/trouble-linking-libusb-using-cmake