On Friday, 31 August 2018 at 06:20:09 UTC, James Blachly wrote:
Hi all,
I am linking to a C library which defines a symbol,
const char seq_nt16_str[] = "=ACMGRSVTWYHKDBN";
In the C sources, this is an array of 16 bytes (17 I guess,
because it is written as a string).
In the C headers, it is listed as extern const char
seq_nt16_str[];
When linking to this library from another C program, I am able
to treat seq_nt16_str as any other array, and being defined as
[] fundamentally it is a pointer.
When linking to this library from D, I have declared it as:
extern __gshared const(char)* seq_nt16_str;
***But this segfaults when I treat it like an array (e.g. by
accessing members by index).***
I believe this should be extern extern(C)? I'm surprised that
this segfaults rather than having a link error.
A bare `extern` means "this symbol is defined somewhere else".
`extern(C)` means "this symbol should have C linkage".
When I try it with just `extern`, I see a link error:
scratch.o: In function `_Dmain':
scratch.d:(.text._Dmain[_Dmain]+0x7): undefined reference to
`_D7scratch5cdataPa'
collect2: error: ld returned 1 exit status
Error: linker exited with status 1