Developing on Unix, Linux, and macOS with GCC, G++, and CLANG

In the 1980's, The Free Software Foundation (FSF) came out with new compilers for use on Unix systems, based upon previous work by Dennis Ritchie and Ken Thompson at Bell Labs. The FSF timing could not have been better, with the quickly rising adoption of the new Internet in the early 1990's.

In 1991, Linus Torvalds, a college student in Finland at the time, released a new operating system called Linux, a Unix-like operating system, and distributed it for free to others by FTP using the new Internet. Initially, Linus wanted to call his new OS FREAX, but a college friend running the university FTP server at their university, University of Helsinki, called it LINUX instead, and that name stuck with the new OS ever since. The gcc compiler was a critical component in the rise of Linux, and the entire Linux diaspora helping Linus Torvalds all used the gcc compiler when developing software for Linux. In later years the g++ compiler was used for C++ development.


Linus Torvalds at LinuxCon Europe 2014

Now, gcc compiler and g++ compiler are available pre-installed in Linux distributions, and are widely used in software development even in the current times.

gcc and g++ also played a critical role in the development of BSD Unix, such as OpenBSD, NetBSD, and FreeBSD, and are included pre-installed in these operating systems also.

gcc and g++ are also pre-installed on Apple macOS operating system, which is based on BSD Unix. While most Mac users do not do work from the command line with their Macs, it certainly can be done. Simply open a terminal from the apps collection, which will then give you a command line.

Also in recent versions of BSD Unix, a new compiler called CLANG is now default. CLANG is actually a compiler front end that can compile C, C++, Objective C, and Objective C++ Software. CLANG was developed primarily by Apple computer, with others contributing also. It was created primarily due to a need to be able to efficiently compile graphics programming with OpenGL. So if you are not doing OpenGL programming, you will be fine with g++ and gcc.

Currently much software is developed using graphical Integrated Development Environments (IDE) like Microsoft Visual Studio and Eclipse. But the original method of developing with gcc and g++ compilers and makefiles from the early 1990's is still widely practiced. Currently, in the USA space program, g++ and makefiles are a critical component being used in new software development. A new paradigm from NASA is to use as much open source software as possible.

If you are new to Unix and Linux, and your primary computer is Microsoft Windows, there are some excellent free options for Unix/Linux accounts. A free NetBSD Unix shell account is available from http://SDF.ORG. Also, http://TILDE.INSTITUTE offers free OpenBSD Unix shell accounts. Any of these will be good for doing the examples in this blog post, or an Apple Mac computer with macOS can be used also, as they all have the gcc and g++ compilers available at the command line pre-installed. From a Windows computer, the excellent free PUTTY application can be used to connect to Unix/Linux hosts with SSH.

Also, Microsoft is currently offering free VM installations, on their Azure Cloud, and you can certainly configure SUSE Linux or Ubuntu Linux on a free Microsoft Azure VM if you desire. During the Bill Gates years this would have been unheard of, but current Microsoft CEO Satya Nadella has been very receptive to open source.

In this blog post, we will first show how to develop a simple C program, using different files to be compiled into .O files, and then combining the .O files into one executable using a Makefile from the command line with the gcc compiler. Dividing C and C++ applications into separate files to be compiled into .O files is one method of managing large software applications, in addition to creating .h files. In this blog we will only show how to compile executables using .O files.

First, with your Unix or Linux or macOS shell account, open a command prompt or terminal, and use the following command to make a directory to store your C files.

$ mkdir source

Then change into your new directory with the following command

$ cd source

Next, we will use the PICO text editor to create your fist C programming language file, if you are using Unix, you will use the pico command at the command line like the following:

$ pico functionone.c

If you have a Linux account, there is an excellent pico clone created by the Free Software Foundation (FSF), called NANO. Enter the following command at the command line in Linux to create your C file.

$ nano functionone.c

You may have heard about VI and VIM text editors available on Unix/Linux, if you are new to these operating systems it is not suggested to use VI or VIM, as they have a steep learning curve. PICO and NANO are much easier to work with.

Now using PICO or NANO, enter the following source code for functionone.c, to save your work in pico or nano, press CTRL-O, to exit nano or pico, press CTRL-X. The following source code is copy and pastable text of functionone.c

/*
=======================================================

FunctionOne() for simple C program

=======================================================
*/

#include <stdio.h>

void FunctionOne() {

	printf("Function One!\n\n");

}

Now, using pico or nano, create the following file, functiontwo.c, and save once complete:

/*
=======================================================

FunctionTwo() for simple C program

=======================================================
*/

#include <stdio.h>

void FunctionTwo() {

	printf("Function Two!\n\n");

}

Now, once again using pico or nano, create the following file for our main routine, main.c. Note that we are using the FunctionOne and FunctionTwo we previously defined in other C files. We will compile all of our files into .O files using gcc, then combine the .O files into a single executable, also using gcc.

/*
=======================================================

main() for simple C program

=======================================================
*/

#include <stdio.h>

void FunctionOne();
void FunctionTwo();

int main() {

	printf("\n\nExample Functions!\n\n");

	// call function one
	FunctionOne();

	// call function two
	FunctionTwo();

	// done
	printf("Done....\n\n");

	return 0;
}

The common and practical way to build C and C++ applications on Unix/Linux has been with Makefiles for many years now, although an older and mature technology, Makefiles are still one of the common ways to build applications even in the current times. Makefiles provide directions on how your C or C++ application will be compiled using the gcc or g++ compilers, commonly called BUILDING the application. Finished executables made with this process are often referred to as BUILDS.

At the command line, enter the following command using pico for Unix:

$ pico makefile

If you are alternately using Linux, use the nano command instead:

$ nano makefile

Once you have the file open in the text editor, enter the following source code for our Makefile:

all:	functionone.o	functiontwo.o	main.o	main.exe

functionone.o:
	gcc -c functionone.c

functiontwo.o:
	gcc -c functiontwo.c

main.o:
	gcc -c main.c

main.exe:
	gcc main.o functionone.o functiontwo.o -o main.exe

clean:
	rm *.o
	rm *.exe

Note in our makefile we are first compiling the C files into .O files using the -c option, then once all our .O files are complete, we then use the gcc compiler to build the executable using the composite .O files, the -o option designates what the resulting executable will be named. Normally in Unix and Linux, executables have no suffix. But in our example here, were are using the suffix EXE so we know that file is a compiled executable machine code file.

We will build our executable using the MAKE command at the command line, then running the resulting executable using the ./main.exe command, like the following:

If you get an error when trying to run main.exe, such as "permission denied", you may have to run the following command at the command line to make the file executable:

$ chmod +x ./main.exe

To run the makefile a second time, and remove the previous .O files and .EXE files, enter the following command at the command line.

$ make clean

Then run the makefile again to compile our executable C  code.

$ make

The executable has an optional file type of EXE, but actually with gcc and g++ compilers, any file suffix can be used for your executable, or no file suffix at all.

The process is similar using C++ with the g++ compiler, create a new directory called sourcecpp in your home directory, change to it using cd, then use the following files to build a C++ executable in the same manner.

functionone.cpp in copy and pastable text:

// =======================================================
// 
// FunctionOne() for simple C++ program
//
// =======================================================

#include <iostream>
using namespace std;

void FunctionOne() {

	cout << "C++ Function One!!" << endl << endl;

}

functiontwo.cpp in copy and pastable text:

// =======================================================
// 
// FunctionTwo() for simple C++ program
//
// =======================================================

#include <iostream>
using namespace std;

void FunctionTwo() {

	cout << "C++ Function Two!!" << endl << endl;

}

main.cpp in copy and pastable text:

// =======================================================
// 
// main() for simple C++ program
//
// =======================================================

#include <iostream>
using namespace std;

void FunctionOne();
void FunctionTwo();

int main() {

	// header for functions call
	cout << endl << "C++ Functions!!" << endl << endl;

	// function one call
	FunctionOne();

	// function two call
	FunctionTwo();

	// end message
	cout << "Done..." << endl << endl

	return 0;
}

makefile for our simple C++ application in copy and pastable text:

all:	functionone.o	functiontwo.o	main.o	main.exe

functionone.o:
	g++ -c functionone.cpp

functiontwo.o:
	g++ -c functiontwo.cpp

main.o:
	g++ -c main.cpp

main.exe:
	g++ main.o functionone.o functiontwo.o -c main.exe

clean:
	rm *.o
	rm *.exe


If you want to use the CLANG compiler instead if you are using BSD Unix, that is simple to do. Note that normally Linux does not have the CLANG compiler installed, but BSD Unix does have it by default. CLANG works with the same arguments as gcc and g++, and also has some new ones. CLANG is really just a front end for the gcc and g++ compilers. Here is how you would have your makefiles with using the CLANG compiler, and then building the application executable with MAKE at the command line. Our first example will be with the C programming language.

makefile for compiling our C executable using CLANG, in copy and pastable text:

all:	functionone.o	functiontwo.o	main.o	main.exe

functionone.o:
	clang -c functionone.c

functiontwo.o:
	clang -c functiontwo.c

main.o:
	clang -c main.c

main.exe:
	clang main.o functionone.o functiontwo.o -c main.exe

clean:
	rm *.o
	rm *.exe

Then we will build and run the executable as in the previous method:

And we would use CLANG in the same way with our makefile for our C++ code in copy and pastable text:

all:	functionone.o	functiontwo.o	main.o	main.exe

functionone.o:
	clang -c functionone.cpp

functiontwo.o:
	clang -c functiontwo.cpp

main.o:
	clang -c main.cpp

main.exe:
	clang -lstdc++ main.o functionone.o functiontwo.o -c main.exe

clean:
	rm *.o
	rm *.exe

And we will build and execute our C++ code in the same manner as well:



I hope this has been informative on using the gcc and g++ compilers to build applications. For examples of using .h files with C++, please see my earlier blog posts concerning C++.

For those who wish to try out and learn Unix, there is a NetBSD Unix shell account available from SDF at http://sdf.org for $9 every 3 months. Also, a free OpenBSD Unix shell account is available on https://tilde.institute. There is free Linux offerings on the internet as well. A free Ubuntu Linux shell account is available at https://tilde.team. Many Linux versions are available as free downloads on the internet as well.

To see my Curriculum Vitae, go to Michael G Workman CV

To see my projects on Azure DevOps, go to https://dev.azure.com/AbionTechnology/

To see my Posts and Answers on Stack Overflow,
      go to Michael G. Workman on Stack Overflow

 If you have any questions about CC++Microsoft C# .NET,  Microsoft Azure Cloud, Unix, Linux, and/or Mobile Apps, please feel free to contact me by email at:

michael.g.workman@outlook.com

Popular Posts