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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With