Libusb undefined reference to

冷暖自知 提交于 2019-11-27 03:27:10

问题


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


回答1:


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.




回答2:


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




回答3:


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




回答4:


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.



来源:https://stackoverflow.com/questions/7050482/libusb-undefined-reference-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!