hyperlinklinkercc

cc static linking: How to import all symbols of just one .a library?


I have 2 static libraries,

libalgha.a with 2 functions: func1() and func2()

and

libbeta.a with 2 functions: func3() and func4()

I have 1 executable (mytest) linked with these 2 libs. The executable calls only func1() and func3(). So as I understand the linker will put only the symbol of these 2 functions into the executable.

The executable is loading with dlopen() in runtime a shared library (libgamma.so) and this shared library uses the function func2() from libalgha.a. When running mytest I get the error:

symbol func2: referenced symbol not found

I can't include libalgha.a inside libgamma.so because of a Solaris 32/64 bits issue.

So how can I link mytest with all symbols of libalgha.a ?


Solution

  • Include a reference to func2 in your program source like:

    extern void func2 ();
    void (*pfunc2)() = func2;
    

    Alternatively, link the whole library into one object, using ld -r, and link that object to the executable, instead of the library. You may or may not need to extract objects, e.g., if the following does not work:

    ld -r -o libbeta.o libbeta.a
    

    then do

    mkdir x
    cd x
    ar x ../libbeta.a
    ld -r -o ../libbeta.o *.o
    cd ..
    rm -rf x