Section 0d

Installing C in the Linux Environment


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Installing GNU for Linux

The following instructions explain step-by-step how to install GNU/GCC (C and C++) compiler tools. While, there are a few ways to do this, everything in this reference will be installed via the terminal.

First, open a terminal window. This can be done by locating the terminal in the applications window or by the keyboard shortcut Ctr+Alt+T.

Once that is done, type the following command into the terminal and press enter:

sudo apt update

There may be a prompt to type in a system password for the above command. If so, type it in and hit enter. Note: The password will not show on the screen. This is for security reasons. The terminal is grabbing the password and will execute once enter has been hit.

Picture of update command

The update may take a few minutes to complete. Once it does, type the following command and press enter:

sudo apt upgrade

Picture of upgrade command

Wait until that has completed and then type the following command and hit enter:

sudo apt install build-essential

There may be another prompt for a password here.

Picture of install command

After that has completed the installation can be verified by typing the following command and hitting enter:

whereis gcc make

Picture of where command

This will show the file path of where gcc is located on the machine.

Next, type the following command and hit enter:

gcc --version

Picture of version command

This will show that the version downloaded is, in fact, up to date.

Finally, type the following command and hit enter:

make -v

Picture of make command

Next up is installing the dev man pages. Man is short for manual. These are the manual pages for using GNU/Linux for development. Type the following command and hit enter:

sudo apt-get install manpages-dev man-db manpages-posix-dev

Picture of manPage command

Now, a small test program can be run to test the new compiler. Type the following command and hit enter:

vi test.c

(test.cpp for a C++ program). This will open a new window where editing and writing of a program can take place. Note: the program can be named anything. This reference uses the name test.

Picture of test command

In order to start typing, first type the letter i and then begin writing the program.

Picture of program command

Notice the pound include at the top of the program. This is a requirement of all C and C++ programs. stdio.h is used for C while iostream is used for C++. These are your library functions. They are like import in JAVA.

Also notice that this program requires main and a return value. Since main is an int an int is returned. printf is the print statement.

Once the small test program is completed, hit the esc key and will no longer be editing the program. Next, to write the program to the terminal type:

:w

then, to return to the terminal window, type:

:q

Once there, the program can be compiled by typing:

gcc test.c -o test

(g++ and test.cpp for a C++ file).

Picture of compile command

If nothing happens, than the program compiled without errors. If that is the case, to run the program type:

./test

Picture of run command

If everything was done correctly the output should show in the terminal window. Congratulations! You just ran your first C/C++ program via the terminal window.

Next, isntall X11 development compilers by typing the command:

sudo apt install libx11-dev

Picture of x11 command

Creating or editing programs can also be done via a GUI window. This can be quicker and less tedious than just editing via the terminal. This works by the terminal calling a text editor or IDE to open the program in. This reference was done via Linux default text editor.

Type the command:

gedit test.c &

Note: If & is not utilized, the GUI window will still open, but the terminal will no longer be active. & is required in order for the GUI window and the terminal to work congruently.

Picture of install command

It may take a few moments to open and the window will look something like the following.

Picture of GUI command