Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SCons to make a shared library (.so) with a static library (.a)

I'm trying to get SCons to make a shared library. One of the items going into the .so is a .a static lib.

I have a line like:

env_2.SharedLibrary('libstuff.so', \
  Split("""stuff.cxx mylib/libMine.a""")

And upon running it, I get this error:

scons: *** Source file: mylib/libMine.a \
is static and is not compatible with shared target: libstuff.so

However, I know that a shared library can be made from the .a via a command like:

g++ -m32 -shared -o libstuff.so stuff.o mylib/libMine.a

Any ideas on getting this to work or any workarounds would be greatly appreciated.


Related question: How do I get scons to put an additional string -shared on the LINK command line for the Program() call? If I could do this, I think it would meet my needs.

like image 892
xavjuan Avatar asked Feb 11 '10 17:02

xavjuan


People also ask

Is .so a static library?

A . a file is a static library, while a . so file is a shared object (dynamic) library similar to a DLL on Windows.

What is the difference between shared and static library?

Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.

Can you link a static library to a dynamic library?

When you want to “link a static library with dynamic library”, you really want to include the symbols defined in the static library as a part of the dynamic library, so that the run-time linker gets the symbols when it is loading the dynamic library.

Why shared libraries are preferred over static libraries?

The most significant advantage of shared libraries is that there is only one copy of code loaded in memory, no matter how many processes are using the library. For static libraries each process gets its own copy of the code. This can lead to significant memory wastage.


2 Answers

Try to set env['STATIC_AND_SHARED_OBJECTS_ARE_THE_SAME']=1 in your SConstruct.

like image 173
Liang Wul Avatar answered Nov 11 '22 20:11

Liang Wul


This problem is not specific to scons. To build a shared library, you'll need objects that are compiled with position independent code (-fPIC). Your best bet is to make the shared library out of the source files compiled with the right options.

In SCons, you can define a list of targets that's used to build both libMine.a and libShared.so.


Update: for your second question, the SharedLibrary builder might do what you need:

SharedLibrary('foo', ['f1.c', 'f2.c', 'f3.c'])

If not, LINKFLAGS sets the flags passed to a link command.

like image 20
Dave Bacher Avatar answered Nov 11 '22 21:11

Dave Bacher