lundi 27 mai 2019

thread_local global variables give linker error: undefined symbols

I am trying to build a shared object (.so) on AIX, using the IBM xlclang++ compiler (v16.1). The .so uses C++11 concurrency, and I have defined some global variables as thread_local. The linker gives Undefined symbol errors for those variables (and for another symbol).

This code compiles, links, and runs fine on Windows (as a DLL), both when using MS Visual C++ 2019 and clang++ (v9.0.0).

By the way, there are also some thread_local static variables inside a function. (See DoSomething below.) The linker does not complain about those. (It did so before I added the compiler/linker options -qthreaded and -qtls=global-dynamic.)

// --- In GlobData.h: ---
extern thread_local bool GLOB_bTracingEnabled;

// --- In GlobData.cpp: --- (Error: Undefined symbol)
thread_local bool GLOB_bTracingEnabled;

// --- In Calculations.cpp: --- (No problem)
int DoSomething() {
    thread_local static int _staticVar;
    // ...
}

#--- In makefile: ---
INCL1=.
CCC=/opt/IBM/xlC/16.1.0/bin/xlclang++
CCFLAGS1=-q64 -qrtti -qthreaded -qtls=global-dynamic -qmkshrobj -bE:MyDLL.exp -DNDEBUG -I$(INCL1)
CCFLAGS=$(CCFLAGS1) -c
LD=/opt/IBM/xlC/16.1.0/bin/xlclang++
LDFLAGS=$(CCFLAGS1)
LIB_FILE=mydll.so
SOURCEDIR1=.
OBJ_FILES=StdAfx.o GlobData.o Calculations.o
VPATH=$(SOURCEDIR1)
.SUFFIXES: .cpp .o

#Make all:
all :: $(LIB_FILE)

#Compile rule:
.cpp.o:
    $(CCC) $(CCFLAGS) -o$@ $<

#Link rule:
$(OUTDIR)/$(LIB_FILE) : $(OBJ_FILES)
    $(LD) $(LDFLAGS) -o $@ $(OBJ_FILES) 
    strip -t -X64 $@

Link errors:

ld: 0711-317 ERROR: Undefined symbol: .ZTH20GLOB_bTracingEnabled
ld: 0711-317 ERROR: Undefined symbol: .__pt_atexit_np
ld: 0711-345 Use the -bloadmap or -bnoquiet option to obtain more information.

Aucun commentaire:

Enregistrer un commentaire