Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding functions in static library

I'm building .lib (static library) for a large project and I'd like to hide some function to end user of the library, but I need those functions to be callable from every .c file inside the library (thus not static).

This is exactly opposite to __declspec(dllexport) and I've already found a solution for gcc.

I'd like to use static, but static function should be available only inside "current .c file", so this is not the way.

In another words: I need a way how can I tell Microsoft Visual C++ 2010 not to export some functions to final .lib (while keeping them available for all other .c files inside the project).

Note: I've never used __declspec (neither __attribute__( visibility)) in my whole solution, this is probably just some default setting associated with building static library (which I cannot trace).

like image 748
Vyktor Avatar asked Oct 23 '12 11:10

Vyktor


2 Answers

This just is not possible. What you are asking for has nothing to do with __declspec(dllexport), that's an attribute that determines what identifiers are visible outside of a DLL. Which certainly will meet your requirement.

But you are asking for a static library. Which is a very simple file format, it is just a bag of .obj files. Put together into an archive by lib.exe after compiling. Nothing happens at all to resolve dependencies between separately compiled .c files in the .lib. That doesn't happen until the .lib is linked.

At which point there is no difference at all between the identifiers with external linkage that the client code used and yours. Making any attempt to hide yours, if that would be possible, would just create link failure when the linker can't figure out how to satisfy an external dependency of one of your .c files with another.

The only way to get anywhere close is to have all of your code in a single translation unit with the functions marked static so they have no external linkage. That's mighty ugly but could be done by a single file that #includes all the other .c files. Surely you'll dismiss that option, so do pursue a DLL solution to get this.

like image 188
Hans Passant Avatar answered Sep 22 '22 16:09

Hans Passant


I think to hide internal symbols from static library and only export some of symbols to external users is possible, at least on Linux platform it's possible.

I used to work on OSX and Linux having similar requirement, my approach is to try to create a pre-linked library with 'ld' command with the following options:

-r -x -exported_symbols_list ../exported_symbols.txt -o $@ $^ in a makefiles.


Options used for xcrun ld are as below

-x

    Delete all local symbols.

-r

    Generate relocatable output--i.e., generate an output file that can in turn serve as input to ld. This is often called partial linking

-exported_symbols_list

    option uses a text file exported_symbols.txt which contains all the interface APIs for the static library that you want to ship out.

I supposed there might be equivalent approach on Windows platform.

like image 25
Eureka.gh Avatar answered Sep 25 '22 16:09

Eureka.gh