2012-04-04 450 views
2

我一直在試圖將兩個程序一起「破解」,其中一個程序(openTLD)使用cmake。我一直在閱讀和處理這個問題有點現在,似乎無法排序。當我有一個對象(依賴於libusb)的時候,我沒有創建cpp對象的實例化,所以它編譯得很好,我入侵了我正在鏈接(我認爲)的錯誤。使用Cmake連接libusb時遇到的問題

我CMakeLists(添加的比特由delimeted **或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) 

從終端的輸出是:

[email protected]:~/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 
[email protected]:~/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); 
} 

感謝您的任何幫助或指針。

回答

3

這實際上是一個鏈接器錯誤。它看起來像你可能有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命令來處理。

+0

非常感謝,昨晚大部分時間我都在這裏。 – LewisMc 2012-04-05 09:20:31