LINUX

How to Install a C Compiler on Linux

0

Whether you're coding in C or building a Linux program from the source, you'll have to install a C compiler. The two major ones on Linux are the venerable GCC and the newer Clang.

Here's how you can install them both on your machine.

Installing GCC on Linux

GCC, or the GNU Compiler Collection, has been around since the 1980s, predating Linux itself. Not only does it compile C programs, but also handles C++, Objective-C, Objective-C++, Fortran, ADA, and Go. A lot of open-source projects still rely on it, including the Linux kernel.

To install GCC along with the required C libraries on Debian and Ubuntu, install the build-essential package:

sudo apt install build-essential

On Fedora and other RPM-based distros:

sudo dnf install gcc

And on Arch Linux:

sudo pacman -S gcc

On any other distribution, you can usually search for "GCC" and you'll find a package for your system. This goes for Clang as well.

To compile a simple C program, such as the famous "Hello, World!", just go to the directory where you saved it and run the following command:

gcc hello.c

…where hello.c is the name of your program.

GCC output in Linux

If the program is correct, GCC will output the compiled file as a.out in the current directory. To run it, type:

./a.out

Related: How to Print "Hello, World!" in the 20 Most Popular Programming Languages

Installing Clang on Linux

The newer kid on the block is the Clang compiler, developed as a front to the LLVM compiler by Apple, ARM, Sony, AMD, and others. Apple uses it as the compiler for its Xcode development environment for macOS.

Clang aims for compatibility with GCC, while increasing performance. It's popular because it's licensed under the Apache 2.0 License, which doesn't require developers to release their source code if they make modifications.

You can install Clang using your package manager. On Debian and Ubuntu, just install the clang package:

sudo apt install clang

On Fedora/CentOS:

sudo dnf install clang

To install Clang on Arch-based distributions:

sudo pacman -S clang

Compiling works the same as with GCC:

clang hello.c
Clang output in Linux

Now You Can Compile C Programs in Linux

Whether you're just learning C or are an experienced C programmer, you can easily install two major C compilers for Linux—GCC and Clang.

If you want to explore C programming, here are some more tips on the language that will give you a headstart.

admin

How to Create and Scan QR Codes on Chromebook

Previous article

Note Taking On Chromebooks Made Easier With Cursive

Next article

You may also like

Comments

Leave a reply

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

More in LINUX

Login/Sign up