https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87199
Bug ID: 87199 Summary: Thread local storage dynamic initialization behaviour differs Linux vs macOS Product: gcc Version: 8.2.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: other Assignee: unassigned at gcc dot gnu.org Reporter: me at nvartolomei dot com Target Milestone: --- Hello, Here is a test program that behaves different on Linux vs macOS. main.c: ``` #include "lib.h" int main() { getProfileEvents(); } ``` lib.cpp ``` #include "lib.h" thread_local ThreadStatus thread_local_var = ThreadStatus(); ThreadStatus::ThreadStatus() { std::cout << "cons"; doX(); } ``` lib.h ``` #pragma once #include <memory> #include <iostream> class ThreadStatus { public: int x = 0; ThreadStatus(); static void doX() { std::cout << "test"; } }; extern thread_local ThreadStatus thread_local_var; static int getProfileEvents() { return thread_local_var.x; } ``` Compiling it using the following command: `g++-8 -g -std=c++17 -c -I . main.cpp && g++-8 -g -std=c++17 -c -I . lib.cpp && g++-8 -o main main.o lib.o && ./main` On a Linux machine the output is: "constest" (g++-8 (Ubuntu 8-20180414-1ubuntu2) 8.0.1 20180414 (experimental) [trunk revision 259383]) On a macOS machine the output is empty. (Homebrew GCC 8.2.0, also HEAD) I found curious that moving doX definition from .h file to .cpp will make this work correctly.