Undefined Reference calling D from C using static linking
data pulverizer via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Thu Mar 23 05:06:14 PDT 2017
On Thursday, 23 March 2017 at 11:32:25 UTC, Nicholas Wilson wrote:
> On Thursday, 23 March 2017 at 10:49:37 UTC, data pulverizer
> wrote:
>> On Thursday, 23 March 2017 at 10:16:22 UTC, Nicholas Wilson
>> wrote:
>>>
>>> It has to do with module references to druntime stuff. You
>>> can either try adding a
>>>
>>> pragma(LDC_no_module_info); //I think it is spelled correctly.
>>>
>>> or you can use ldc to link and it will link druntime
>>>
>>> gcc ccode.c -c
>>> ldc2 dcode.d code.o
>>>
>>> I don't know how well that will work.
>>
>> Many thanks, I tried:
>>
>> ```
>> pragma(LDC_no_moduleinfo) //
>> https://wiki.dlang.org/LDC-specific_language_changes#LDC_no_moduleinfo
>> ```
>>
>> which worked, your method of doing the final compilation using
>> ldc2 (or dmd) also works :-)
>>
>> Is there a dmd equivalent for `pragma(LDC_no_module_info);`?
>> Attempting the final compilation `gcc -o output ccode.o
>> dcode.o` after the second stage compilation `dmd -c dcode.d`
>> gives an error:
>>
>> ```
>> dcode.o: In function `_D5dcode7__arrayZ':
>> dcode.d:(.text._D5dcode7__arrayZ+0x23): undefined reference to
>> `_d_arraybounds'
>> dcode.o: In function `_D5dcode8__assertFiZv':
>> dcode.d:(.text._D5dcode8__assertFiZv+0x23): undefined
>> reference to `_d_assert'
>> dcode.o: In function `_D5dcode15__unittest_failFiZv':
>> dcode.d:(.text._D5dcode15__unittest_failFiZv+0x23): undefined
>> reference to `_d_unittest'
>> dcode.o:(.text.d_dso_init[.data.d_dso_rec]+0x22): undefined
>> reference to `_d_dso_registry'
>> collect2: error: ld returned 1 exit status
>> ```
>>
>> ```
>> dmd --version
>> DMD64 D Compiler v2.073.2
>> Copyright (c) 1999-2016 by Digital Mars written by Walter
>> Bright
>> ```
>
> Those functions are the bounds checking function, the non
> unittest assert function, the unittest function an module
> initialisation function respectively. dmd -boundscheck=off
> -release should get rid of the first two, you didn't compile
> with -unittest so I'm not sure why the thord one is there at
> all. For _d_dso_registry all i can suggest is see what -betterC
> gets you.
I just compiled `dmd -c dcode.d -betterC -boundscheck=off`
(-betterC probably makes -boundscheck=off irrelevant but I threw
it in as a prayer) I am still getting:
```
dcode.o:(.text.d_dso_init[.data.d_dso_rec]+0x22): undefined
reference to `_d_dso_registry'
collect2: error: ld returned 1 exit status
```
More information about the Digitalmars-d-learn
mailing list