Weak-linking with static libraries

Posted by Jaakko L. on Stack Overflow See other posts from Stack Overflow or by Jaakko L.
Published on 2010-04-13T07:53:07Z Indexed on 2010/04/13 9:23 UTC
Read the original article Hit count: 635

Filed under:
|
|
|

I have declared an external function with a GCC weak attribute in a .c file:

extern int weakFunction( ) __attribute__ ((weak));

Compiled object file has weakFunction defined as a weak symbol. Output of nm:

1791:         w weakFunction

I am calling the weak defined function as follows:

if (weakFunction != NULL)
{
    weakFunction();
}

When I link the program by defining the object files as parameters to GCC (gcc main.o weakf.o -o main.exe) weak symbols work fine. If I leave the weakf.o out of linking, the function address is NULL in main.c and the function won't be called.

Problem is, when weakf.o is inside a static library, for some reason the linker doesn't find the function and the function address always ends up being NULL. Static library is created with ar: ar rcs weaklibrary weakf.o

Anyone had similar problems?

© Stack Overflow or respective owner

Related posts about gcc

Related posts about weak-linking