Find cpp, c++, clang++ and clang in Xcode 4.3.x and Higher

 

So you have some scripts that use “cpp”, “c++” or “clang” and etc but you cannot find them in your path? Not to worry, read on…

With OS X Lion and sandboxing, you can find your c++ compiler using the “find” command:

find /Applications/Xcode.app/ -name “c++”

This will return something like:

find /Applications/Xcode.app/ -name “c++”
/Applications/Xcode.app//Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
/Applications/Xcode.app//Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/c++

The one in the “bin” folder is the one we are looking for so the folder that contains our toolchain is here:

/Applications/Xcode.app//Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/


Navigate to that folder to do some inspection:

cd /Applications/Xcode.app//Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/
ls -la c++
lrwxr-xr-x 1 root wheel 7 19 Jun 22:41 c++ -> clang++
ls -la clang++
lrwxr-xr-x 1 root wheel 5 19 Jun 22:41 clang++ -> clang
ls -la clang
-rwxr-xr-x 1 root wheel 22985632 19 Jun 22:41 clang

You can see right away that c++ is a symlink to clang++ and clang++ is a symlink to clang. What a mess!

Now let’s find “cpp”:

ls -la cpp
ls: cpp: No such file or directory

Oopsy daisy, it’s not there. That’s one of the issue a lot of developers will be facing. So let’s remedy this by first creating cpp as an alias. We want this alias to be permanent between Terminal sessions so we put it in our bash_profile and we also have to make sure the toolchain bin url highlighted above is in our path:

vim ~/.bash_profile

And then change the content of your bash profile so that your path includes:

/Applications/Xcode.app//Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/

and also create an alias for cpp to point to clang. The contents of my ~/.bash_profile now looks like this:

export PATH=~/bin/:/Applications/Xcode.app//Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/:$PATH
alias cpp=’clang’

Once you are done, press Escape in vim and then type :wq and press enter. That will “write” and then “quit”.

After you are done, close Terminal and open it again and type cpp:

cpp
clang: error: no input files

Lovely, it’s working. Any questions? Leave them down below 🙂

Advertisements

Compiling C files for Mach-O ARM architecture, in OS X Lion

Yesterday I wanted to compile a .c code for ARM architecture using the iOS SDK on my Lion machine. I wanted to create object files and executable files that I can install on my jailbroken iPhone 4. I have explained the process here for you. I hope it will be of help.

We are going to be using the LLVM-GCC compiler. In OS X Lion, this compiler sits here:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2

Also we need to pass the path to our iOS SDK, to the compiler so that it knows where to get the libraries and frameworks from. In OS X Lion with iOS SDK 5.1 installed, the path of the iOS SDK would be here:

/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk

So if your SDK version is different, you need to modify that path. Now let’s write a simple Hello World C code in a file named hello.c

#include <stdio.h>

int main(void){
printf(“Hello, World!\n”);
}

Now let’s go to Terminal and change your current working directory to the directory that contains the above hello.c file. Now let’s tell Terminal where our compiler and SDK folders are:

COMPILER=”/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/usr/bin/llvm-gcc-4.2″
SDKDIR=”/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS5.1.sdk”

Now we are going to attempt to compile the hello.c source file for armv7 CPU architecture:

$COMPILER hello.c -o hello.o -c -arch armv7 -isysroot $SDKDIR

Now if you get an ls -la on your folder, you will also see an object file called hello.o. If you do the following:

file hello.o

You should see an output like this:

hello.o: Mach-O object arm

So now we have the object file. If you want to link this object file into an executable file, then you can do this:

LINKER=$COMPILER
$LINKER hello.o -o hello -arch armv7 -isysroot $SDKDIR

Note: the linker and compiler are both llvm-gcc-4.2 but we pass the -c to it to compile and if we skip -c, we will link, producing the final executable file.

Now you have 3 files

  • hello.c
  • hello.o
  • hello
The first one is your source file, the second one is the compiled file and the third one is the linked (executable) file. So now do this:
file hello
You should be able to see the following output:
hello: Mach-O executable arm

That’s it. If you have any questions, let me know in the comment section below.

Git from command-line after installing Xcode on OS X Lion

Xcode 4.3.x or newer comes with Git but the problem is when you install Xcode on your machine (OS X Lion or newer), Git’s path won’t be added to the user path which means if you run git from your command-line, your system will say:

-bash: git: command not found

Xcode’s installation of Git is at the following location on your machine:

/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/

With the git binary sitting here:

/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/git

To add this binary to your path (which will allow you to run “git” from any directory on your system), go to terminal and type this command:

export PATH=”/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/”:$PATH

There is one caveat to this approach and that is the PATH will only be changed in your current running instance of terminal. As soon as you close terminal and open it again, you will have to enter the above command again to get access to the git app. So what is the proper solution? You will have to add the above “export” command to the .profile file in your home directory. The .profile file gets read every time you open terminal. So open a terminal instance and type the following command:

cd ~/

And then type this command:

ls -la | grep “.profile”

We are trying to find out if we already have a file named .profile in our home directory. If after running the above command you won’t see anything getting printed to the terminal, use the following command to create a new .profile file. If you already have a .profile file, skip this command:

touch .profile

Now open the .profile with this command:

open .profile

Now add the git path to the PATH variable in the .profile file so that your .profile content will look something like this (it really depends on what you already have in this file. I am assuming your .profile file didn’t exist until now and you just created it):

export PATH=”/Applications/Xcode.app/Contents/Developer/usr/libexec/git-core/”:$PATH

Save your changes to the .profile file and close terminal and open it again. Now whichever directory you are in, in terminal, you can use the git command. Good luck.