GCC on OSX - learnclang/1-helloworld GitHub Wiki

Download XCode from the App Store. It's free, but over 5 gigs in file size. You have to download it to get the Command Line Tools, unfortunately. From there, just open it and go through the prompts, accepting license agreements and then shut it down.

XCode should automatically install XCode Command Line Tools, with no problem.
You can test this by opening Terminal and typing
which gcc
Which for me returns
/usr/bin/gcc
From there, you can write the following program in a text editor and save it to a file called hello.c
#include <stdio.h>

int main(){
    printf("hello, world\n");
    return 0;
}

I advise making a scratch project for practicing, which is just a directory for testing stuff. Normal project structure is as follows:

Project Folder
==sourcefiles folder
====all your .cpp files are in here
==headers folder
====all your .h files are in here
==build folder
====build files will be in here, this is later
==misc folder
====miscellaneous files

Of course, everyone's projects are different, this is just how I create by default and I'm showcasing it as an example.

Compilation:
Inside of Terminal:
cd /Users/username/navigate to folder
When you're within the folder that the .c file is, type
ls
just to make sure. You should see the file named hello.c. Then type
gcc hello.c -o hello

gcc is calling the compiler that was installed with Xcode/Command Line Tools.
hello.c is the file you wrote.
-o is stating that the next argument will be the output filename.
hello is the output file name. When it's done compiling, you should have a Unix Executable file which you can then run inside of Terminal by typing (as long as you remain in the same directory):
./hello

Now, why would anyone want to do this instead of using XCode? I'm not saying you should or should not, I prefer it because I can keep a manual tally of everything that I'm doing, and I personally work better this way. If you want to use XCode, feel free to.

⚠️ **GitHub.com Fallback** ⚠️