Bug 19640 – Linker error when compiling delegate with betterC
Status
RESOLVED
Resolution
WONTFIX
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2019-02-02T23:17:48Z
Last change time
2019-12-17T11:01:31Z
Keywords
betterC
Assigned to
No Owner
Creator
andy.pj.hanson
Comments
Comment #0 by andy.pj.hanson — 2019-02-02T23:17:48Z
**a.d**:
```
int call(int delegate() d) {
return d();
}
int foo(int i) {
return call(() => i);
}
void main() {
foo(0);
}
```
**dub.json**:
```
{
"name": "test",
"buildOptions": ["betterC"],
}
```
Output of `dub build`:
```
Performing "debug" build using /usr/bin/dmd for x86_64.
test ~master: building configuration "application"...
Linking...
/usr/bin/ld: error: /usr/lib/x86_64-linux-gnu/libphobos2.a(dmain2_6f5_47b.o): size of section .dtors.d_dso_dtor[.data.d_dso_rec] is not multiple of address size
/usr/bin/ld: final link failed: Error reading (null): No error
collect2: error: ld returned 1 exit status
Error: linker exited with status 1
/usr/bin/dmd failed with exit code 1.
```
Comment #1 by razvan.nitu1305 — 2019-12-10T13:32:40Z
There are multiple issues with the code you've written. First of all you cannot have a main function with betterC because dmd creates a d_run_main function which is the entrypoint of d programs. Typically, you just create the object file with betterC and then you link it with some C code. This is a minor aspect, regarding the test code.
The main issue with the code is that the delegate (() => i) requires a closure that is allocated with the GC. Since you are compiling with -betterC, the GC is not available.
Comment #2 by radu.racariu — 2019-12-16T17:41:23Z
As Razvan pointed out, that delegate will create a closure and allocate on the GC heap.
This is fixable by changing `call` to look like
```
int call(scope int delegate() d)
```
Note the scope keyword.
Also, main should look like
```
extern(C) void main()
```
Please use the learn forums (https://forum.dlang.org/group/learn) before adding a bug, this kind of information is easily available there.