C Programming

How to Compile a C File in macOS

As a Mac user, you may need to compile a C file for some projects. Though installing an IDE is a simple and straightforward approach that allows Mac users to compile a C file easily. But some users are worried about their system storage and installing an IDE, in that case, won’t be an ideal approach as it will take much space on the system.

If you are searching for another way to compile a C file in macOS, follow this article’s guidelines.

How to Compile a C File in macOS

You can compile a C file in macOS from two methods:

Method 1: Compile a C File in macOS Using Clang

Clang compiler is preinstalled in macOS, allowing users to compile a C file on the terminal. Follow the below-given steps to compile a C file using Clang:

Step 1: First, confirm Clang is installed on macOS through the following command:

clang --version

 

Step 2: Then create a C file with .c extension using nano editor from the following command:

nano c_program.c

 

Step 3: Add the following C program inside the file:

#include <stdio.h>
int main() {
   // printf() displays the string in output
   printf("Hello, World!");
   return 0;
}

 

Step 4: Save the file using Ctrl+X and then pressing Y.

Note: Steps 2-4 are optional. If you already create a C file, you can skip these steps.

Step 5: To compile a C program using Clang, use the following syntax:

clang <input-file-name> -o <output-file-name>

 

Note: The <input-file-name> is the source file name, while the <output-file-name> is the file generated after the execution of code.

Step 6: To get the output of the C code, run the output file.

./output_file

 

In this way, you can compile a C file in macOS.

Method 2: Compile a C File in macOS Using gcc Compiler

If you want to compile a C file in macOS using the gcc compiler, you must perform the following steps:

Step 1: First, download and install gcc compiler Xcode on macOS from the App Store.

Step 2: Then install an Xcode command line tool on the Mac terminal using the following command:

xcode-select --install

 

Step 3: After installing the command line tool, you can compile C code using gcc command through the below-given syntax:

gcc -o <output-file-name> <input-file-name>

 

Step 4: To run the C file on macOS and get the output, use the following command.

./output_program

 

In this way, you can use the gcc compiler to compile any C code on macOS easily.

Note: gcc compiler can also be installed using brew package manager:

brew install gcc

 

Conclusion

Compiling a C file in macOS is simple and it can be done either through preinstalled Clang or gcc compiler. Both these methods allow users to run a C file on a macOS terminal. For gcc, the Mac users must install Xcode from the App store. After that, they can run a C file in macOS.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.