0

I have a special situación using a function from two differents static library, and is this:

I have a module "Fun" that have a function "Fun()" and static variables for work. And I have a static library "LibA.a" that have a function "LibA()" that use the module "Fun". And I make the module "Fun" in static library "Fun.a".

In other words:

Fun.a include Func.c

LibA.a include LibA.c and Fun.c

When in the main call the function LibA that use the Fun.c this execute from Fun.a and not from LibA.

The problem is that the static variable into Fun.c is use from main and LibA.

Is possible make that LibA use the Fun that include into the library LibA.a and not the Fun from Fun.a library? For have two differents instances of Func().

I make this example:

Fun.h

extern int Fun( void );

Fun.c

#include <stdio.h>
#include "Fun.h"

static int value = 0;

int Fun( void )
{
#ifdef FUN
    printf( "Call from Fun.a\n" );
#endif
#ifdef LIBA
    printf( "Call from LibA.a\n" );
#endif

    return value++;
}

LibA.h

extern void LibA( void );

LibA.c

#include <stdio.h>
#include "LibA.h"
#include "Fun.h"

void LibA( void )
{
    printf( "LibA->Fun:%d\n", Fun( ) );
}

main.c

#include <stdio.h>
#include "LibA.h"
#include "Fun.h"

int main()
{
    printf("Lib Example\n");

    LibA( );
    LibA( );
    LibA( );
    printf( "Main->Fun:%d\n", Fun( ) );
    printf( "Main->Fun:%d\n", Fun( ) );
    printf( "Main->Fun:%d\n", Fun( ) );
    LibA( );

    return 0;
}

The output from this example is:

Lib Example
Call from Fun.a
LibA->Fun:0
Call from Fun.a
LibA->Fun:1
Call from Fun.a
LibA->Fun:2
Call from Fun.a
Main->Fun:3
Call from Fun.a
Main->Fun:4
Call from Fun.a
Main->Fun:5
Call from Fun.a
LibA->Fun:6

And I want to the output are this:

Lib Example
Call from LibA.a
LibA->Fun:0
Call from LibA.a
LibA->Fun:1
Call from LibA.a
LibA->Fun:2
Call from Fun.a
Main->Fun:0
Call from Fun.a
Main->Fun:1
Call from Fun.a
Main->Fun:2
Call from LibA.a
LibA->Fun:3

for make the Fun static library I use this:

gcc-6 -c -x c Fun.c -g2 -gdwarf-2 -o Fun.o" -Wall -O0 -DFUN -std=c11
ar -rsc  libFun.a Fun.o

for make the LibA static library I use this:

gcc-6 -c -x c Fun.c -g2 -gdwarf-2 -o Fun.o -O0 -DLIBA -std=c11
gcc-6 -c -x c LibA.c -g2 -gdwarf-2 -o LibA.o -O0 -DLIBA -std=c11
ar -rsc  libLibA.a Fun.o LibA.o

for make the main example I use this:

gcc-6 -c -x c main.c -g2 -gdwarf-2 -o main.o -O0 -std=c11
gcc-6 -o main.out -Wl,--no-undefined -Wl,-L./ -Wl,-z,relro -Wl,-z,now -Wl,-z,noexecstack main.o -lFun -lLibA
  • 1
    If you have multiple definitions of the function with the same name in the static libs, the linker will use the first function definition it could find in static libs. Do not abuse it. – Serge Sep 08 '17 at 02:11
  • Dear Serge, I know how work the linker, the question is if exist any parameter in the linker or other method for solve this. – Carlos Fernandez Sep 08 '17 at 13:42
  • no, no such parameter. All binding in the linker are done by the names in the symbol tables of the linked files. If you have duplicate global names. It will confuse the linker. So, if instead of the library you would link '.o' files, the linker will complain aloud with a linking error. In case of the static library, it will not look for the symbol definition if it finds one. You can play some tricks with the dynamic libraries though. – Serge Sep 08 '17 at 13:45

1 Answers1

0

I found the answer to my problem and is the following:

Declare the Fun.h usign hidden attribute

extern __attribute__( ( visibility( "hidden" ) ) ) int Fun( void );

And the compile usign ld and objcopy

gcc -fvisibility=hidden -c Fun.c LibA.c
ld -r Fun.o LibA.o -o LibAx.o
objcopy --localize-hidden LibAx.o
ar rcs LibA.a LibAx.o

When I compile the main.c including Fun.c and the LibA.a the output is:

Lib Example
Call from LibA.a
LibA->Fun:0
Call from LibA.a
LibA->Fun:1
Call from LibA.a
LibA->Fun:2
Call from App
Main->Fun:0
Call from App
Main->Fun:1
Call from App
Main->Fun:2
Call from LibA.a
LibA->Fun:3

I found the solution in this link https://stackoverflow.com/a/30028576/5011611

thanks https://stackoverflow.com/users/103855/ypsu