C Compiler for Mac
Overview
A compiler is a program that converts high-level language like C, C++, Java, etc., to machine code understandable by a computer. C is a compiled language which means code needs to be compiled first to run it. Various compilers are available on Mac to compile C code. Mac C compilers compile C code into an executable form. This executable form can be run directly to run the C code.
Clang and GCC(GNU Compiler Collection) are the common compilers used to compile C code. Other than these two compilers, the LLVM compiler and CDT plugin for Eclipse can also be used to compile C code on Mac.
Introduction
Ever thought of how a computer works? A computer only understands binary - 0 and 1. How can we write code that is understood by a computer? It is impossible to write a code in 0s and 1s. A compiler is a program that converts source code(written in a high-level language) into machine code for the computer.
Mac C compilers convert a C code to an executable that can be run directly. Can you guess the size of the above C code and the executable it generates?
C code only took 71 bytes to be stored. Whereas the size of the executable on Mac is 49 KB !!! Can you guess why there is a difference in size between C code and executable? The compiler takes care of loading stdio.h code and all the required code that is needed to run into an executable. That is the reason for the size difference.
C code can be written on any platform like Mac, Windows, etc. Online C compilers compile C code and create an executable according to the platform. The executable created for one platform can only be executed on that platform.
Getting started on compiling
For compiling C code on Mac, we would need an IDE(Integrated Development Environment) which can be used to write code, and a compiler to compile the written code. IDE can be anything like Sublime text, Xcode, etc. In the following sections, we will discuss different C compilers that can be used on MAC.
Installing Xcode
- Xcode is an IDE(Integrated Development Environment) developed by Apple.
- Xcode can be installed via App Store on Mac.
- Once Xcode is installed, open a terminal window and enter Xcode-select --install, which installs command line developer tools.
- Command-line tools install various compilers like Clang and GCC, which will be explained in the next sections.
Using Clang inbuilt compiler
Clang is a compiler created by Apple written over the LLVM compiler. It can compile C, C++, Objective C/C++, OpenCL, CUDA, and RenderScript. Command-line developer tools install clang.
Once command-line tools are installed, clang --version can be used to check if clang is installed.
- clang <file_name> -o <output_filename> can be used to compile a C code.
- Let us say the code is written in the test.c file, and the output executable should be test.
- The code to compile would become clang test.c -o test.
- If no output file name is used, a.out is used as an output file.
- For example, clang test.c creates a.out as executable.
Using GCC compiler in the Terminal window
GCC(GNU Compiler Collection) is a compiler for various programming languages like C, C++, and Objective-C developed by GNU.
Following are some of the differences between GCC and Clang:
- GCC is another compiler like Clang developed to compile different programming languages. Whereas, Clang is developed to provide better performance than GCC.
- GCC is a group of compilers that supports C, C++, Objective-C, Fortran, Ada, Go, and D. Whereas Clang is a compiler toolchain that supports C, C++, and Objective-C natively.
Command-line developer tools install gcc. gcc --version can be used to check the version of GCC.
- gcc <file_name> -o <output_filename> can be used to compile a C code.
- Let us say code is written in the test.c file, and the output executable should test.
- The code to compile would become gcc test.c -o test.
- If no output file name is used, a.out is used as an output file.
- For example, gcc test.c creates a.out as an executable.
Other C compilers for Mac
Apart from Clang and GCC(GNU Compiler Collection), there are a couple more ways to compile C code.
LLVM
LLVM is a group of toolchains and compilers. A toolchain is a programming tool that can be used to perform a task in software development. An example of a toolchain is Gradle which is used in building applications. It was developed to give more user-friendly messages in errors. LLVM can also be used to compile C code. LLVM can not be installed using an installer. It can only be built from source code. This link explains how to build LLVM.
CDT plugin for Eclipse IDE
Just like Xcode, Eclipse is also a popular IDE(Integrated Development Environment). Eclipse is mostly used for Java projects. But CDT(C/C++ Development Tooling) plugin of Eclipse can be used to compile C programs in Eclipse.
Following is a screenshot of Eclipse IDE.
Run the Executable
An executable can be created out of C code by using any compiler discussed above. The executable runs the code we wrote. Following is the output running the executable created after compiling the following C code.
Output
Conclusion
- Mac C compilers convert high language(C, C++, Java, etc.) code to machine code understandable by a computer.
- We would need an IDE (e.g. Xcode) to write C code and a compiler to compile the written code.
- C code can be compiled by different compilers like clang, gcc, llvm, and CDT plugin of Eclipse on Mac.
- Mac C compilers compile C code and create an executable file (.exe) that can be run directly.