This bug report is a little weird as it is contingent on an unmerged PR (https://github.com/dlang/dmd/pull/14509), but I am filing it here before I forget.
C structs will now be in the same module so the .init for structs collides across different object files.
From https://github.com/dlang/dmd/pull/14509#issuecomment-1266355604
// Dummy function definitions to make the example compile.
// freer.i
typedef struct Foo3 *FooRef3;
struct Foo3 {
int x;
};
void free_foo3(FooRef3 foo){}
// maker.i
typedef struct Foo3 *FooRef3;
struct Foo3 {
int x;
};
FooRef3 make_foo3(void){return 0;}
// do_foo.d
import maker;
import freer;
void main(){
FooRef3 f = make_foo3();
free_foo3(f);
}
# Separate compilation makes
# the issue obvious.
$ dmd maker.i -c
$ dmd freer.i -c
$ dmd do_foo.d -c
$ dmd do_foo.o freer.o maker.o
duplicate symbol '__D3__C4Foo36__initZ' in:
freer.o
maker.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error: linker exited with status 1
$ nm freer.o
0000000000000010 B __D3__C4Foo36__initZ
0000000000000000 S _free_foo3
$ nm maker.o
0000000000000010 B __D3__C4Foo36__initZ
0000000000000000 S _make_foo3
$ echo "__D3__C4Foo36__initZ" | ddemangle
__C.Foo3.__init
Walter responded:
WalterBright commented 15 hours ago
The cause of the problem is a workaround for a linker problem:
https://github.com/dlang/dmd/blob/master/compiler/src/dmd/toobj.d#L524
Comment #1 by dave287091 — 2022-10-05T15:27:42Z
The above PR has been merged so the issue is now in master.
Comment #2 by dlang-bot — 2023-02-05T06:29:03Z
@WalterBright updated dlang/dmd pull request #14541 "fix Issue 23387 - ImportC: identical structs defined in two C files l…" fixing this issue:
- fix Issue 23387 - ImportC: identical structs defined in two C files lead to duplicate .init symbol on macOS
https://github.com/dlang/dmd/pull/14541
Comment #3 by dlang-bot — 2023-02-07T11:36:54Z
dlang/dmd pull request #14541 "fix Issue 23387 - ImportC: identical structs defined in two C files l…" was merged into master:
- 7fe0aee2a4ed8775e8f55e02d2ca6696cf30ef43 by Walter Bright:
fix Issue 23387 - ImportC: identical structs defined in two C files lead to duplicate .init symbol on macOS
https://github.com/dlang/dmd/pull/14541