- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Compiling and running C programs in Linux
When you write C programs, you'd use a text editor and save them into files. These files are known as "source code". Just like any other text files, these files can be easily read, shared, compressed, copied and modified.
One thing you can't do with your C source code, however, is execute it. In other words, after you write and save your C program source code, you can't run the program and use it. You will need to convert this text source code into a file that the computer can execute. This is achieved in two steps known as "compilation" and "linking".
C compiler and linker
The C programming language is carefully designed to facilitate the creation of compilers to be easy and straightforward. Thus, today, there are quite a number of C compilers that you can choose from which range in features, stability and price. Among the most popular ones is the free and open-source compiler called "GCC" or GNU Compiler Collection (See gcc.gnu.org).
GCC is really a collection of compiler programs to compile code written in various languages like C, C++ and Java amongst others.
For compiling C programs, we use a program aptly called "gcc". To locate this program in your Linux box, please type the command below on the terminal:
which gcc
In my computer, I see the following results: (yours may vary)
$ which gcc /usr/bin/X11/gcc
To check what version of gcc is installed, just type:
gcc --version
The linker used in Linux is called "ld" (that's the letters "el" and "dee") from a software package called binutils ("binary utilities"). The linker is called normally after the compilation process. It is responsible for combining multiple binary files called "objects" into a single "executable" file.
The executable file is the ultimate output of the compilation and linking processes. This represents your C program as a ready-to-run file. This is also now considered as a "binary" file since the contents of this file is no longer text. It can have characters which aren't even printable on the screen. This is the file you'll use to see your program in action. Once you have this executable file, you can then hide away your source code.
Standard C library
We're almost at the exciting part of actually writing a program, compile and link it and finally run it. However, I'd like to quickly discuss the so-called "standard C library" and then we'll be set.
The C programming language has been standardized by an international organization called "ISO" or International Organization of Standardization (See http://en.wikipedia.org/wiki/ISO). This promotes a consistent implementation of the language and allows interoperability between different systems.
Part of the ISO C standard is a specification of a set of functions that provide very basic but common operations like manipulation of character arrays ("strings") and handling input and output.
It is helpful, therefore, to familiarize yourself with the standard C library when writing your program otherwise you might end up writing a functionality that's already available in the library.
Linux already ships with the standard C library by default and can be found usually in the "/lib" folder. The file you'd look for is "libc.so". You'll notice that it will have a number in the filename and will differ from system to system, this number is just for versioning.
$ ls -l /lib/libc.so.6 /lib/libc.so.6 -> libc-2.9.so
Note in the output above, "libc.so.6" is a symbolic link to a file called "libc-2.9.so" where "2.9" is the specific library version.
In order to use the functions in the standard C library, you'll need to include in your programs the so-called "header" files. These files, usually with a file extension of ".h" (that's dot "h"), contain the function prototypes which basically specifies how to call them from your program. You can open up these header files with your text editor as they're really just simple text files.
Because the standard C library packs numerous functions, several header files are used in order to group related functions. It's important to include the correct header file or files of the functions you want to call from your own program.
Let's take for example a function that prints out text on to the screen called "printf". This function is defined in the header file called "stdio.h". Therefore we need to include "stdio.h" in our program in order to use "printf" like this:
#include <stdio.h> int main(int argc, char **argv) { printf("Hello there!\n"); }
Compile and run
I've mentioned the two steps "compilation" and "linking" above in order to create an executable version of your program. However, the linker "ld" can be very strict and may require specific command-line arguments and sometimes even the order of the arguments can upset it. Luckily, the "gcc" compiler can take care of calling "ld" for us. Thus, I would not attempt to discuss "ld" here, but rather just use "gcc" to make our lives simpler.
So fire up your favorite text editor and type the program below:
#include <stdio.h> int main(int argc, char **argv) { printf("Hello there!\n"); return 0; }
Save the program to a file with a ".c" file extension, I'll use "hello.c". Then we'll compile and link it using gcc like this:
gcc hello.c -o hello
Note that we're running the "gcc" program giving it our "hello.c" file for processing. Then we also have "-o hello" which tells gcc to write its output into a new file called "hello". If the "hello" file already exists, then it will be overwritten.
What gcc will do is read your C program line by line from top to bottom and translate your code into the so-called "machine" code. This machine code is what is written to the output file "hello". What you won't see is that it'll actually create some temporary files during the process of compilation. Then at the final stage, by default, gcc will invoke the linker in order to create the final executable file "hello".
You should now have a file called "hello" in the same directory as "hello.c". To execute your program, on the terminal you'd type:
./hello
The output of this simple program would simply be the words "Hello there!" printed on the screen. Below is the transcript of my terminal session when running our "hello" program.
$ ./hello Hello there!
Congrats!
There you have it! If you were to modify your program, then you'll need to recompile it again in order for your changes to be translated on to the executable version. Try it, make it break and see what happens. The gcc compiler will print out some error or warning messages to you if it discovers some invalid statements or something that might not make sense.
Don't be afraid if you get an error or warning, it's one of the best ways to get experience in C, or any programming language for that matter. By running on to these error conditions, you expose yourself to what the compiler would say or how it behaves. So that when your program grows in complexity, you'd be better able to fix these problems when they arise.
So go on, try to remove those semi-colons and see what gcc would say. Or change printf into "print" or try and remove the "#include" line and see what gcc says. Or if you like, open up the "hello" file in your editor to see the contents; don't be surprised that it will look like just a bunch of useless characters to you. These would really really help you later on.
I will be covering a lot of the interesting topics about C programming, check out my intended coverage by clicking this link: C programming in Linux.
Congratulations, you have just become a C programmer!