[LINUX] A story about a build error in a shared library that references libusb

What happened

Creating a program that uses libusb on ubuntu

$ gcc -c testUsb.c
$ gcc testUsb.o testUsb -lusb

Normally, I think you can build like this, but If I try to make this a shared library (.so) instead of an executable file, I get a build error.

$ gcc -c testUsb.c
$ gcc -shared -fPIC testUsb.o -o libTestUsb.so -lusb
/usr/bin/ld: testUsb.o: relocation R_X86_64_PC32 against symbol `devInfo' can not be used when making a shared object。 -Recompile with fPIC.
/usr/bin/ld:Last link failed: bad value
collect2: error: ld returned 1 exit status

What is this? saw it for the first time.

Try to find out

The content of the error is "libusb was not built with -fPIC, so it cannot be used in this context. If you want to use it, please rebuild with -fPIC".

No, I wouldn't have to rebuild libusb for this.

e? Is this the solution?

I tried various things, but all of them failed (in the direction of not rebuilding libusb), so I don't think it is necessary to write in detail here, but the final solution was to build as follows.

$ gcc -shared -fPIC testUsb.c -o libTestUsb.so -lusb

Compile and link directly from .c files without creating .o files. Now that you have a .so file that works fine without any errors, what's the difference with generating an .o file? ..

I don't understand what I'm doing normally ... Now I have to find out why this works.

Recommended Posts

A story about a build error in a shared library that references libusb
About psd-tools, a library that can process psd files in Python
A story about a 503 error on Heroku open
dlopen () ltrace a function call in a shared library
A story that an error occurred when PyInstaller was used in a program that uses googleapiclient
A story about reflecting Discord activity in Slack Status
A story about a Linux beginner passing LPIC101 in a week
A story about PHP that was okay in the development environment but buggy in the production environment LEVEL 1-3 + 1
Published a library that hides character data in Python images
A story about how to specify a relative path in python.
A story about competing with a friend in Othello AI Preparation
A story that stumbled when using pip in a proxy environment
A story about installing matplotlib using pip with an error
A story about trying to implement a private variable in Python.
A story about a tragedy happening by exchanging commands in chat
[Google Photo & Slack Photo Bot] A story about making a bot that acquires photos in Google Photos and sends them to Slack.
Use networkx, a library that handles graphs in python (Part 2: Tutorial)
The story that a hash error came out when using Pipenv
A story about creating a program that will increase the number of Instagram followers from 0 to 700 in a week
A refreshing story about Python's Slice
A note about get_scorer in sklearn
A sloppy story about Python's Slice
A story about using Python's reduce
A note about mock (Python mock library)
Introducing a library that was not included in pip on Python / Windows
[Memorandum] A story about trying OpenCV tutorial (face recognition) in a Windows environment
Try using APSW, a Python library that SQLite can get serious about
A note about the functions of the Linux standard library that handles time
A story about writing a program that automatically summarizes your own asset transitions
A story that heroku that can be done in 5 minutes actually took 3 days
Code reading of faker, a library that generates test data in Python