Skip to content

Instantly share code, notes, and snippets.

@cslarsen
Created March 20, 2012 19:23
Show Gist options
  • Star 20 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cslarsen/2140152 to your computer and use it in GitHub Desktop.
Save cslarsen/2140152 to your computer and use it in GitHub Desktop.
How to compile Objective-C on the command line on Mac OS X
/*
* To compile objective-c on the command line:
*
* gcc -framework Foundation objc-gcc.m
*
* You may have to link with -lobjc or other libs,
* as required.
*/
#import <Foundation/Foundation.h>
int main(int argc, char** argv)
{
NSLog(@"Testing");
}
@paulmiard
Copy link

Didn't work for me. The following did though

clang -framework Foundation main.m -o hello

@c4arl0s
Copy link

c4arl0s commented Jul 17, 2018

it works for me too. Thanks!

captura de pantalla 2018-07-16 a la s 20 26 20

@c4arl0s
Copy link

c4arl0s commented Jul 17, 2018

captura de pantalla 2018-07-16 a la s 20 30 13

@mendozj5
Copy link

Thanks guys. That second one worked for me too.

@ytyubox
Copy link

ytyubox commented Jul 15, 2020

I was trying to compile with #import "MYHEADER.h", I can successfully compile with -I(header search path).

clang -std=c99 -pedantic -Wall -Wextra -Wno-import -lobjc -framework Foundation \
 -I MYHEADERSDIR \
 ALLMYMFILES \
 -o main

@pcimring-taboola
Copy link

-framework Foundation worked for me. Thanks!

@ebraminio
Copy link

ebraminio commented Feb 14, 2021

For some reason I couldn't use -framework Foundation using brew installed clang

$ /usr/local/opt/llvm/bin/clang -Wall -framework Foundation a.m
a.m:1:9: fatal error: 'Foundation/Foundation.h' file not found
#import <Foundation/Foundation.h>
        ^~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.

but using Apple built, /usr/bin/clang -Wall -framework Foundation a.m so feel free to check your list of installed clang maybe using where clang

@krishpranav
Copy link

@ebraminio make sure you have Xcode tools downloaded

@krishpranav
Copy link

$ xcode-select install

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment