I have an application (application) and a dynamic library / shared object (dlib), both linked to a static library that declares a global variable (gvar) in the header file with __declspec (selectany)/ __attribute__ ((weak)). By design, applications and dlib should have their own copies of gvar (on MSVC and GCC I get exactly that).
After porting to Mac OSX and compiling with clang, I see that gvar in dlib is associated with gvar in the application. Not sure if this is a clang or design error; if it is by design, is there any way to avoid this and get the same behavior as in GCC / MSVC?
clang version:
bash-3.2$ c++ --version
Apple LLVM version 7.0.0 (clang-700.1.76)
Target: x86_64-apple-darwin15.0.0
Thread model: posix
Minimal design to reproduce the problem:
main.cpp:
#include <stdio.h>
#include <dlfcn.h>
__attribute__ ((weak)) int g_global = 10;
int main ()
{
printf ("main (): g_global: addr = %p; value = %d\n", &g_global, g_global);
typedef void Foo ();
void* so = dlopen ("./my-so.so", RTLD_LAZY | RTLD_LOCAL);
Foo* foo = (Foo*) dlsym (so, "foo");
foo ();
}
shared.cpp:
#include <stdio.h>
__attribute__ ((weak)) int g_global = 20;
extern "C" void foo ()
{
printf ("foo (): g_global: addr = %p; value = %d\n", &g_global, g_global);
}
build.sh:
#!/bin/bash
rm -f my-so.so
rm -f app.
c++ -shared -fPIC shared.cpp -omy-so.so
c++ main.cpp -oapp -ldl
exit:
bash-3.2$ ./app
main (): g_global: addr = 0x10c657030; value = 10
foo (): g_global: addr = 0x10c657030; value = 10
, (()), app dlib gvar.