yrnkrn / zapcc

zapcc is a caching C++ compiler based on clang, designed to perform faster compilations
Other
1.25k stars 61 forks source link

Undefined reference to static thread_local struct member in shared library #21

Closed peterazmanov closed 6 years ago

peterazmanov commented 6 years ago

Stumbled upon this issue trying to compile some code which uses fbthrift. I managed to reproduce the issue with the following simple test case.

a_lib.h:

struct A
{
  A()
  {
    tm = 0;
  }

  static thread_local int tm;
};

a_lib.cpp:

#include "a_lib.h"

thread_local int A::tm;

main.cpp:

#include "a_lib.h"

int main()
{
  A a;
  return 0;
}

build_zapcc.sh:

#!/bin/bash -e

zapcc++ -c -fPIC a_lib.cpp
zapcc++ -fPIC a_lib.o -shared -o a_lib.so

zapcc++ main.cpp a_lib.so

Output:

/tmp/main-62d36d.o: In function `A::A()':
main.cpp:(.text._ZN1AC2Ev[_ZN1AC2Ev]+0xd): undefined reference to `TLS wrapper function for A::tm'
zapcc: error: linker command failed with exit code 1 (use -v to see invocation)

Test compiles fine with GCC 8.1.1, Clang 6.0 and Clang 7.0 (svn_r338211).

thread_local_test.zip

yrnkrn commented 6 years ago

Thanks, I'll look into this.

yrnkrn commented 6 years ago

One-file example, no caching. 21.zip

yrnkrn commented 6 years ago

Fixed in 57f48831bf15a2d9440167d272196f3626d1e43f