Thu Vlion
Thu Vlion

Reputation: 71

How to solve the 'undefined reference to libusb' error in ros?

I am trying to transfer a camera(non-uvc) stream program to ros platform.

I've already got the camera driver running

and Makefile is like this:


g++  main.cpp -o test_gui -g  -I /usr/local/include -L /usr/local/lib -D_LIN -D_DEBUG   -L../lib/x64 -I../include -lASICamera  -lpthread  -lusb   -DGLIBC_20 -m64 -lrt -I/opt

Now I want to do this in a ros node, so I write a node in which the CMakelists is like this:


include_directories(
  ${catkin_INCLUDE_DIRS}
)
include_directories(/usr/local/include)
include_directories(/root/catkin_ws/src/asi_converter_real/include/asi_converter_real)
link_directories(/usr/local/lib)
link_libraries(pthread)
link_libraries(usb)
link_libraries(libASICamera.a)
link_libraries(libASICamera.so)
add_executable(asi_converter_real src/asi_converter_real.cpp)
target_link_libraries(asi_converter_real ${catkin_LIBRARIES})

And the Makefile line generated into /catkin_ws/build/***/ is like this:


/usr/bin/c++       CMakeFiles/asi_converter_real.dir/src/asi_converter_real.cpp.o  -o /root/catkin_ws/devel/lib/asi_converter_real/asi_converter_real -rdynamic -L/usr/local/lib -lpthread -lusb -Wl,-Bstatic -lASICamera -Wl,-Bdynamic -lASICamera

But it seems that the system cannot find the dynamic libraries of libusb, coz' it reports


undefined reference to `libusb_set_configuration'
undefined reference to `libusb_claim_interface'
/usr/local/lib/libASICamera.a(ASI174MM.o): In function `WorkingFunc(void*)':
undefined reference to `libusb_bulk_transfer'
undefined reference to `libusb_bulk_transfer'

[1]Is there anyone who knows how to solve this problem ?

[2]How to find the .a and .so of libusb in my computer ? (I am sure I have them, coz' I can pkg-config --cflags/--libs them)

[3]How could I explicitly link a dynamic library in ros CMakelists ? or just link_libraries(usb) is enough for both static and dynamic libriries ?

Upvotes: 0

Views: 3494

Answers (2)

Leiaz
Leiaz

Reputation: 2917

find_package requires that CMake have a corresponding Find<package>.cmake

Cmake have a pkg-config module.

You can use it to write your own FindLibUSB, as explained on the CMake wiki.

Or you can use it directly in your CMakeLists.txt :

find_package(PkgConfig REQUIRED)
pkg_search_module(LIBUSB1 REQUIRED libusb-1.0)
include_directories(SYSTEM ${LIBUSB1_INCLUDE_DIRS})

You can see in the module documentation all the variables that are set : LIBUSB1_LIBRARIES for the libraries, etc ...

Upvotes: 2

luator
luator

Reputation: 5017

Finding libraries in cmake is typically done using find_package.

In your case it might look like this (taken from this question):

find_package(libusb-1.0 REQUIRED)
include_directories (${LIBUSB_1_INCLUDE_DIRS})
...
target_link_libraries(asi_converter_real ${catkin_LIBRARIES} ${LIBUSB_1_LIBRARIES})

You may have to adjust the version number of course (maybe it can simply be removed).

Upvotes: 1

Related Questions