I have some static library that I can't change or rebuild. The library uses global variables. Something like this:
//lib A
#include <iostream>
static int i = 0;
void printA(){
std::cout << i++ << std::endl;
}
I want to create two shared libraries that have their own "copy" of static library and its global state:
//lib B
#include "liba.h"
void printB(){
printA();
}
⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
//lib C
#include "liba.h"
void printC(){
printA();
}
... and use them simultaneously:
#include "libb.h"
#include "libc.h"
int main(){
printB();
printB();
printC();
printC();
}
I expect following output:
0
1
0
1
.. but actually get:
0
1
2
3
Seems like libB and libC share common counter variable. If had access to libA source code, I would rebuild it with -fvisibility=hidden. But unfortunately I have only binary.
Is there any way to achieve expected behavior without libA rebuilding?
If had access toI have only binary.- so you have accesslibA.aor not? You can unpacklibA.ato.ofiles, then rename all symbols using ex.objcopy --redefine-sym printA=printAcopy, then rebuildlibAcopy.athen fromprintC()callprintAcopy-printC() { printAcopy(); }.libA.a, but I'd rather not change it. Because it may be updated once.