Search code examples
macosd

I'm getting a confusing link error building a trival D program on my Mac


I'm new to D programming. I'm using macOS Sonoma on an M1 processor. The clang version on my system is 15.0.0. I installed the latest version of DMD (2.106). I created this program:

import std.stdio;

int main()
{
    int a = 0;
    a++;

    // Display the result.
    writefln("a = %d", a);

    return 0;
}

I tried compiling this with the command dmd app.d where "app.d" is the name of the file containing the code above. This is the message I get:

ld: multiple errors: symbol count from symbol table and dynamic symbol table differ in '/Users/peter/BOK/D/source/app.o' in '/Users/peter/BOK/D/source/app.o'; address=0x0 points to section(2) with no content in '/Library/D/dmd/lib/libphobos2.a[3228](config_a94_4c3.o)'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

When I add the -v option to the linker command line I get a large number of messages of this form:

ld: warning: ignoring file '/Library/D/dmd/lib/libphobos2.a[12543](biguintcore_218b_517.o)': found architecture 'x86_64', required architecture 'arm64'.

This makes the problem appears to be related to the fact that I'm on an M1. However, when I explicitly add the -arch x86_64 option to the linker command line, I get this:

peter@hurricane source % cc -arch x86_64 -v app.o -o app -m64 -Xlinker -no_compact_unwind -L/Library/D/dmd/lib -lphobos2 -lpthread -lm
Apple clang version 15.0.0 (clang-1500.0.40.1)
Target: x86_64-apple-darwin23.1.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
 "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld" -demangle -lto_library /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/libLTO.dylib -dynamic -arch x86_64 -platform_version macos 14.0.0 14.0 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o app -L/Library/D/dmd/lib -L/usr/local/lib app.o -no_compact_unwind -lphobos2 -lpthread -lm -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/lib/darwin/libclang_rt.osx.a
ld: multiple errors: symbol count from symbol table and dynamic symbol table differ in '/Users/peter/BOK/D/hello/source/app.o' in '/Users/peter/BOK/D/hello/source/app.o'; address=0x0 points to section(2) with no content in '/Library/D/dmd/lib/libphobos2.a[3228](config_a94_4c3.o)'

So I am again stuck. I can confirm, using the file command that the app.o file is for the x86_64 architecture.

Thanks, Peter


Solution

  • This is a known issue with dmd while using the latest xcode:

    https://issues.dlang.org/show_bug.cgi?id=24137

    LDC has fixed this problem somewhat (and LDC supports M1 natively, DMD is x86 only), but for DMD you may want to try setting the environment variable MACOSX_DEPLOYMENT_TARGET=11

    related: https://github.com/ldc-developers/ldc/issues/3864

    Another suggestion is to use -ld_classic linker flag: https://forum.dlang.org/post/[email protected]