Home > OS >  Undefined symbols for architecture arm64: `"_puts"`
Undefined symbols for architecture arm64: `"_puts"`

Time:10-04

I am writing a simple "Hello world" program using MacBook Air 2020 (M1) and the C code is as follows:

#include<stdio.h>
int main(void){
        printf("Hello world!\n");
        return 0;
}

clang version is

Apple clang version 14.0.0 (clang-1400.0.29.201)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

However after I compiled it and try to run ld hello.o -o hello it gives the following error:

Undefined symbols for architecture arm64:
  "_puts", referenced from:
      _main in hello.o
ld: symbol(s) not found for architecture arm64

Why is the symbol not defined?

CodePudding user response:

Link using the compiler (clang hello.o -o hello) — it will add the standard C library and the startup code, etc to the ld command line.

Add -v to the options to see what the compiler actually executes.

On an Intel Mac running macOS Big Sur 11.7, part of the information produced was this humungous long line which invokes the loader (ld) in a ridiculously obscure location:

"/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 -no_deduplicate -dynamic -arch x86_64 -platform_version macos 11.0.0 12.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk -o tm59 -L/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/lib -L/usr/local/lib /var/folders/sj/_v4_1hp947d_6qg_m75syr000000gn/T/tm59-7bf6b1.o -lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/13.0.0/lib/darwin/libclang_rt.osx.a
  •  Tags:  
  • cld
  • Related