Skip to content

force the use of a static library when the dynamic library is available as well #2743

Closed

Description

@bdbaddog

This issue was originally created at: 2011-03-21 09:50:40.
This issue was reported by: axeoth.

axeoth said at 2011-03-21 09:50:40

Dear SCons team,

I ask you to implement the following: when in the same folder there are both static and shared versions of some library (let's say libfoo.a and libfoo.so), to be able to force using the static or the shared one, someting along these lines:

# Link foo statically, bar dynamically 
gcc -Bstatic -lfoo -Bshared -lbar foobar.o -o foobar

You find below a possible solution that involves a bit of hacking:

http://old.nabble.com/Static-library-linking-td26582198.html (see the last message on that page, also copied below)

It should be nice to have that solution integrated into the standard SCons.

The feature is quite asked for, as you may find through the following links:

http://old.nabble.com/How-to-force-the-use-of-a-static-library-when-the-dynamic-library-is-available-as-well-td20476343.html

http://old.nabble.com/Static-library-linking-td26582198.html

http://markmail.org/message/qqkq2bsvfqev7edd#query:+page:1+mid:mukmps5hgdb34m72+state:results

Many Thanks!!!!

=============SOLUTION==========================
Re: Static library linking

by Ringo De Smet Sep 15, 2010; 04:39pm :: Rate this Message: - Use ratings to moderate (?)

Jean-Baptiste,

On 15 September 2010 13:52, Jean-Baptiste Lab
Just been playing with that myself and have currently settled for the non-portable following:

boost_utf_lib = env.File('/usr/lib/libboost_unit_test_framework.a') 
env.Append(LIBS=boost_utf_lib) 

But I'd like to have something less hack-ish.

What I got so far, the SCons way...

Put the lines between the markers in the file site_scons/site_tools/StaticLink.py:

def generate(env, **kwargs): 
    # Only supported when using GCC 
    if env['CC'] == 'gcc': 
        env['STATICLIBS'] = [] 
        env['SHAREDLIBS'] = [] 
        env['_STATICLIBFLAGS'] = '${_stripixes(LIBLINKPREFIX, STATICLIBS, LIBLINKSUFFIX, LIBPREFIXES, LIBSUFFIXES, __env__)}' 
        env['_DYNAMICLIBFLAGS'] = '${_stripixes(LIBLINKPREFIX, SHAREDLIBS, LIBLINKSUFFIX, LIBPREFIXES, LIBSUFFIXES, __env__)}' 
        env['_LIBFLAGS'] = '-Wl,-Bstatic ${_STATICLIBFLAGS} -Wl,-Bdynamic ${_DYNAMICLIBFLAGS}' 
        print env.Dump() 
 
def exists(env): 
    # Only supported when using GCC 
    if env['CC'] == 'gcc': 
        return True 
    return False 

In your SConstruct, just after the construction of your Environment, but before the rest of your build definition:

env.Tool('StaticLink') 
env.Append(STATICLIBS = 'gtest_main') 
env.Append(SHAREDLIBS = 'pthread') 

This did it for me so far. I am now looking into writing specific Configure checks that will append the lib to the correct list if the library can be found, for example:

conf = Configure(env) 
conf.CheckStaticLib('gtest', language='C++') 

Will post the results when I have more...

Ringo

Activity

  1. mwichmann commented on Oct 30, 2023

    @mwichmann
    Collaborator

    The problem of SCons not being able to keep linker instruction flags together with libraries they may relate to remains. However, the GNU linker syntax (-l:filespec) for not modifying filespec is supported since #4429, in addition to the File() methods described here; both are documented in the manpage entry for $LIBS. Closing this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions