http://gcc.gnu.org/bugzilla/show_bug.cgi?id=59364

            Bug ID: 59364
           Summary: C++11 extern thread_local breaks linking: undefined
                    reference to TLS init function
           Product: gcc
           Version: 4.8.2
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: conradsand.arma at gmail dot com

Short description:

Declaring a variable as extern thread_local in a C++11 program leads to
"undefined reference to `TLS init function for ..." during linking.

Long description:

Consider three files: rng.hpp, a.cpp and b.cpp, listed below.
Compile using:
g++ a.cpp -c -o a.o -std=c++11
g++ b.cpp -c -o b.o -std=c++11
g++ a.o b.o -o prog -std=c++11

b.o: In function `TLS wrapper function for rng_instance':
b.cpp:(.text._ZTW12rng_instance[_ZTW12rng_instance]+0x5): undefined reference
to `TLS init function for rng_instance'

Things only work if thread_local is removed.

gcc version 4.8.2 20131017 (Red Hat 4.8.2-1) on Fedora 19 (x86-64).

---
rng.hpp:

#include <random>

class rng {
  public:
  std::mt19937_64 engine;
  std::uniform_real_distribution<double> distr;
  double get_val() { return distr(engine); }
  };

---
a.cpp:

#include "rng.hpp"
thread_local rng rng_instance;

---
b.cpp:

#include <iostream>
#include "rng.hpp"

extern thread_local rng rng_instance;

int main(int argc, char** argv)
  {
  std::cout << "val: " << rng_instance.get_val() << std::endl;
  return 0;
  }
---

Reply via email to