deltamind106
deltamind106

Reputation: 707

Which OSX library to link against (command line) to use NSLog?

I want to compile and link an application from the command line on MAC OSX using the command-line compiler. I have a source file named "hello.m" as follows:

    #import <Foundation/Foundation.h>
    int main(int argc,char *argv[])
    {
        NSLog(@"hello world\n");
        return 0;
    }

At the command prompt, I type:

    $ clang -o hello hello.m

But the compiler returns:

    Undefined symbols for architecture x86_64:
      "_NSLog", referenced from:
          _main in main-74f615.o
    ld: symbol(s) not found for architecture x86_64

Obviously, I need to link against a library when I call NSLog. Which library do I need to link with?

Upvotes: 9

Views: 2472

Answers (1)

bobDevil
bobDevil

Reputation: 29528

The library you need to link to is 'Foundation'. That's the framework you're importing at the top of the file. If this were in Xcode, it sets up framework linking for you via project settings / automation framework detection. When using clang directly, you need to pass the -framework flag to properly link to them.

clang -framework Foundation -o hello hello.m

Upvotes: 15

Related Questions