Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 204
deltamind106 Avatar asked Dec 02 '16 20:12

deltamind106


1 Answers

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
like image 50
bobDevil Avatar answered Sep 30 '22 14:09

bobDevil