How to link compiled object file (hello.o) with ld on Mac OS X?

18,074

For a reference, my complete linker options are

ld -demangle -dynamic -arch x86_64 
-macosx_version_min 10.9.0 
-o hello
-lSystem /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/5.0/lib/darwin/libclang_rt.osx.a
Share:
18,074
CrepuscularV
Author by

CrepuscularV

Updated on June 08, 2022

Comments

  • CrepuscularV
    CrepuscularV almost 2 years

    I got a problem with link objective files on a Mac OS X. Tracing back the problem is, here is my C hello world program

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

    //Compile with gcc (clang LLVM compiler on mac)

    $ gcc -c hello.c
    

    The output file is hello.o link with gcc and run the executable is

    $ gcc hello.o -o hello
    $ ./hello
    

    Now, I have to use the mac linker program ld or Ld to link the the objective files instead of gcc. In this case, what arguments should I pass into the ld program in order to get the program run? A simple pass in the object file name, i.e.

    $ ld hello.o

    resulting in

    ld: warning: -macosx_version_min not specified, assuming 10.6
    Undefined symbols for architecture x86_64:
        "_printf", referenced from:
         _main in hello.o
        "start", referenced from:
        implicit entry/start for main executable
    ld: symbol(s) not found for inferred architecture x86_64
    

    So what other files that i need to include to link or architecture information that I need to specify? Thanks.

  • shock_one
    shock_one almost 9 years
    The manual of my ld says -dynamic is the default. Also it doesn't have the -demangle option. Linking worked without these two for me.
  • Xophmeister
    Xophmeister over 8 years
    I found that -lSystem is enough: you don't need to explicitly include libclang_rt.osx.a and -arch and -macosx_version_min are given defaults (with a warning) if omitted.