Thread: Symbol Visibility in Static Libraries

  1. #1
    Registered /usr
    Join Date
    Aug 2001
    Newport, South Wales, UK

    Question Symbol Visibility in Static Libraries


    Normally I don't make static libraries, only DLLs or EXEs. But after having to deal with a piece of code that I have copied and pasted into virtually all of my programs lose its versioning (so I don't know which copy is up to date), I have decided on compiling it into a static library and linking against that.

    The thing is, when I make the lib (MSVC 2005), its symbol table seems to be chock full of functions, when the only thing that I want visible to the linker at the end is one function and a reference to an external variable.

    Reading around, one thing I can do is declare each function that I want to be hidden from the linker as static. All this seems to do is change the storage class for the functions from "External" to "Static". This prevents linkers from using the reference, assuming that they are well behaved. But the implementation leak is still there.

    Other things that I have read include using attribute((visibility("hidden"))), which is GNU C-specific, and __declspec(dllexport) on only the elements that I want to export, which seems to have no effect (more likely for DLL use only).

    I suspect that I would need a tool to do this. But surely there must be a way of telling the compiler that "this stuff is not to be externally referenced, compile it as one big chunk"?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Long Beach, CA
    I'm not a Windows programmer, but I would guess the "leak" you see is actually debugging symbols, so that you can debug functions in the static library from your application. It needs to know what function foo() looks like for debugging purposes, even if it's not exported to the linker. You probably need to build your library without debugging symbols and/or strip them from the static library when it's done building, before you link it into your final executable. How exactly to do that depends on your compiler/build environment.

  3. #3
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Portland, OR
    This isn't a Windows problem... A static library on any platform is going to include all global symbols which haven't been declared "static." The compiler has no way of guessing which of these you intended to be private. Just how it is.
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. static libraries
    By coletek in forum Linux Programming
    Replies: 4
    Last Post: 11-05-2010, 04:15 PM
  2. Using static libraries
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 02-29-2008, 11:40 PM
  3. Making static libraries
    By fh791 in forum C++ Programming
    Replies: 1
    Last Post: 09-07-2004, 02:48 AM
  4. Static Libraries
    By blue8173 in forum C++ Programming
    Replies: 2
    Last Post: 06-12-2004, 09:54 PM
  5. unresolved symbol -_- (static int)
    By Lynux-Penguin in forum C++ Programming
    Replies: 3
    Last Post: 10-11-2003, 06:23 PM