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