Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a shared library from existing object files

I have a project in my IDE. I need to make a shared library of it to use in extensions. I don't want to make a copy of this project with shared-library settings. Is there any way to build a shared library using the object files (.o) from my already existing project? As I understand, I can write a makefile for this.

like image 263
Max Frai Avatar asked Apr 06 '10 09:04

Max Frai


People also ask

How do I create a Dynamic Library?

The way to create a Dynamic Library in Linux is with the gcc command using the -c to generate the object files (.o) from the source files (. c) and the -fPIC to make the code position independent. Thus, the following command makes a bunch of .o files from each .

Which of the following options is necessary to create a shared library?

The -shared or -dynamiclib option is required to create a shared library.

What are shared object libraries?

A shared library or shared object is a file that is intended to be shared by multiple programs. Symbols used by a program are loaded from shared libraries into memory at load time or runtime.


2 Answers

I assume you're on some sort of Unix and are probably using the GNU toolchain. In that case, to create a proper shared library, you'd need to compile your code using the position-independent code flags (-fpic or -fPIC) before you can create a shared library. Unless your .o files are already compiled with those flags, chances are you won't end up with a working shared lib.

If they already are compiled for position independent code, the usual g++ -shared ... should do the trick.

like image 106
Timo Geusch Avatar answered Sep 26 '22 09:09

Timo Geusch


g++ -shared -fPIC -o myshared.so *.o

like image 25
pajton Avatar answered Sep 23 '22 09:09

pajton