12

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 :)

phihag
  • 278,196
  • 72
  • 453
  • 469
Reshi
  • 799
  • 4
  • 15
  • 32
  • Please see the next link, it worked for me http://stackoverflow.com/questions/10059146/gcc-libm-not-working –  Aug 18 '12 at 23:32

4 Answers4

24

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

user3240675
  • 241
  • 2
  • 2
  • Usually, such a thing happens when libusb.so link on your system (e.g. /usr/lib/x86_64-linux-gnu/libusb.so) points to some older version of libusb that does not contain the required symbols, e.g. libusb-0.1.so.4. You can check where a link points using the ```readlink ``command. In this case, it may be a matter of tweaking the source to look for a specific version. – Greg Kramida Jun 04 '15 at 15:11
18

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.

Xtroce
  • 1,749
  • 3
  • 19
  • 43
  • 1
    yes thank you very much :) I followed your commands and now it works. Furthermore I have better overview about how to link the .so libraries to gcc compilers cos I programmed til now in Java and i didnt exactly now how to do this. My flag for the library was -lusb-1.0 there was the only diference – Reshi Aug 13 '11 at 13:42
  • Compiled Heimdall, got error, edit heimdall/CMakeFiles/heimdall.dir/link.txt add -lusb-1.0 after following this instructions. – Pieter Dec 11 '17 at 05:08
1

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

Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
  • I tried it but i didnt know how to get the -lusb flag :) but Xtroce told me that so now it works:) thanks for help:) – Reshi Aug 13 '11 at 20:27
1

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.

emboss
  • 38,880
  • 7
  • 101
  • 108
  • It's the fact that the OP is not linking against the library, not the fact that the headers can't be found. This is an `ld` undefined reference error, not a `gcc` missing headers error. – Delan Azabani Aug 13 '11 at 12:58
  • Sure. But I was referring to the principle in general for Eclipse. – emboss Aug 13 '11 at 13:03
  • gcc's C preprocessor, which is used by Eclipse, looks for headers, at the very least, in `/usr/include` by default, so headers are almost never a problem. – Delan Azabani Aug 13 '11 at 13:07