www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 23387] New: ImportC: identical structs defined in two C files


          Issue ID: 23387
           Summary: ImportC: identical structs defined in two C files lead
                    to duplicate .init symbol on macOS
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Mac OS X
            Status: NEW
          Keywords: ImportC, link-failure
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: dave287091 gmail.com

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

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); } $ 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 --
Oct 04 2022