Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libusb undefined reference to

I'm trying to set up libusb API on my OS. I downloaded libusb api on libusb.org. I followed the standard installation procedure:

cd into directory
./configure
make
make check //without errors
make install

Then I launched Eclipse C/C++ and copied some code from the tutorial found on the internet. But when trying to build it I got following output:

main.cpp:(.text+0x19): undefined reference to `libusb_init'
main.cpp:(.text+0x76): undefined reference to `libusb_set_debug'
main.cpp:(.text+0x8a): undefined reference to `libusb_get_device_list'
main.cpp:(.text+0x136): undefined reference to `libusb_free_device_list'
main.cpp:(.text+0x142): undefined reference to `libusb_exit'
/tmp/ccOWJGwe.o: In function `printdev(libusb_device*)':
main.cpp:(.text+0x162): undefined reference to `libusb_get_device_descriptor'
main.cpp:(.text+0x28a): undefined reference to `libusb_get_config_descriptor'
main.cpp:(.text+0x4d4): undefined reference to `libusb_free_config_descriptor'
collect2: ld returned 1 exit status

I have libusb.so in /lib and also I have usb.h in /usr/local/include and the link for the .so and libusb.a in /usr/local/lib.

Also the #include inside the code is correct.

I know that problem is in linker but I, kind of, cannot make it work :)

I'm using Fedora 15 operating system and gcc 4.6.0 20110603 (Red Hat 4.6.0-10) version compiler.

So what could I do to resolve these undefined references? Thanks very much for help :)

like image 374
Reshi Avatar asked Aug 13 '11 12:08

Reshi


4 Answers

you have to set the library linker flag for compilation in the linker, you can get a full list in the console by executing

pkg-config --list-all

These are the libraries which you have installed on your system and you have to link against the ones you want to use. so in your example it is libusb so you do

pkg-config --libs libusb

there should be the output

-lusb

or

-lusb-1.0

This gives you the flag you have to pass to the linker. e.g.

g++ myfile.cpp -lusb[-1.0]

Then you edit the configuration of the project and search for the linkerflags, there should be a textfield for that somewhere in the buildoptions. i'm not quite shure where to find it but googling for it suggested:

Project -> Properties -> C/C++
Build -> Miscellaneous -> flags

After you found it, just add the linker flag in the textfield and you should be fine.

EDIT

since my answer is the accepted one, I also added the other flag that seems to work for a lot of people.

like image 60
Xtroce Avatar answered Oct 16 '22 10:10

Xtroce


I did face the same problem. But I was able to solve it by adding '-lusb-1.0' to the linker.

e.g : g++ myfile.cpp -lusb-1.0

like image 28
user3240675 Avatar answered Oct 16 '22 09:10

user3240675


What is your linker command line? You need to have -lusb in the linking command; just having the header included won't work.

like image 29
Delan Azabani Avatar answered Oct 16 '22 09:10

Delan Azabani


I don't use Eclipse C/C++ but I am pretty sure the reason is the same that I faced some while ago when setting up a C project in Netbeans.

It's not enough to have the #include in your code and the library at the right location - you also have to tell Eclipse where to look for them and how to use them. This turorial shows you how to set it up in Eclipse.

like image 24
emboss Avatar answered Oct 16 '22 10:10

emboss