DMD: can't get extern __gshared to work right (vs. LDC)

DanielG simpletangent at
Thu Feb 7 04:34:57 UTC 2019

macOS 12, DMD 2.084.0 / LDC 1.14.0-beta1

I have a C API that exports some global variables, declared like 

.h file:
extern "C" {
#define PUBLIC_API __attribute__((visibility("default")))
struct __opaqueHandle; typedef __opaqueHandle* opaqueHandle_t;
PUBLIC_API extern const opaqueHandle_t kSomeGlobalValue;

.cpp file:
const opaqueHandle_t kSomeGlobalValue = (opaqueHandle_t) 

.d file:
extern (C):
export extern __gshared opaqueHandle_t kSomeGlobalValue;

I have no issues linking against the resulting .dylib with a 
C/C++ program, nor issues with DMD on Windows with the same code. 
However with DMD on macOS, the value is coming through 
incorrectly. It looks like a valid pointer value (vs. pure 
garbage), but it's still incorrect. LDC2 works fine, no issues.

Is there some extra flag I need to pass to DMD on Mac to get it 
to see this global properly? It links fine, no errors, but the 
value is still wrong. I should also mention that this dylib 
exports functions, too, and they are working fine even with DMD. 
It's just the globals I'm having problems with.

More information about the Digitalmars-d-learn mailing list