Terminal Alias Setup for XCode Embedded Compiler

Warning: The following is mostly unnecessary tinkering

To get a handle on Objective C using Mac OS, I wanted a simple way to compile from the terminal. A quick search made me realize that the LLVM compiler is not accessible via the terminal. You can download it via XCode -> Preferences -> Downloads. But when you go there you will see the following note:

“Before installing, note that from within Terminal you can use the XCRUN tool to launch compilers and other tools embedded within the Xcode application.”

So to compile via XCRUN I found you have to run this command:

$ xcrun -sdk /Applications/Xcode.app/Contents/Developer//Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk gcc hello.c

To shorten that you can setup an alias in your .bash_profile.

Open the terminal by pressing Command + Space and type Terminal in Spotlight then press enter once it appears.

Create or edit your profile by typing the following:

vim ~/.bash_profile

Once vim is open press ‘i’ for insert mode and add the following line:

alias c='xcrun clang --sysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk/'

Change to vim command mode by pressing ESC. Save and exit by typing ‘:wq’

To test the alias, using Xcode make a new Objective-C class.
Delete all the code Xcode gives you by default and stick the following C code to keep it simple.

#include
int main(){
printf("Hello World\n");
return 0;
}

In Terminal and change to the location where your file is then type this command:

c program1.m -o program1  

Your program should compile with no errors. To run it, simply type:

./program1  

Then hit return.

Leave a Reply

Your email address will not be published. Required fields are marked *