Section 0k

Advanced Operations - Compiling and Linking


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

From Source Code to Executable

Making it Go

In this final section of Chapter 0, you will look at how a program is converted from the source code you write to the executable file the computer can, well, execute. This is a topic you may want to pass by at first and then come back to when the time comes to build your programs. It is not particularly difficult but it will make more sense when you are ready to test your programs.

Simple One File Programs

Your first programs will generally be single source files. That is to say, you will develop your program in a file that might be called myprog.c. This is easy to convert to an executable using the GNU Compiler program called gcc as shown here.

Simple Compile Operation

You will notice that the method is run and there is no apparent result. The cursor just goes to the next line. In many cases, this is okay but in this case, there is a problem. This particular source code file has some warnings in it, so we need to call gcc with the warnings flag set, using -Wall as shown here.

Compile Operation with warnings

Since you always want your programs to be top notch, you need to identify and remove any errors or warnings you find in your code. Using the -Wall command line switch will always show you the issues. When the problems with the program are resolved, calling gcc with -Wall will provide you a very boring (but desirable) next command line, as shown here.

Compile Operation with no problems

That is all there is to compiling and building a program from a single source file, which you will do to start out.

Note that the program name at the end of the command line statement (test.exe) is different from the name of the source file (testprog.c). You can name your source files and programs anything you want as long as they are understandable, although in Windows, you must add an .exe extension on the end to indicate to the operating system that the file is executable. In most cases, you do not have to do that in the Mac or Linux environment.

Again, that is all there is to compiling and building a single-file program. However, not very long after you get started, you will be compiling and building programs with multiple files. That comes next.

Compiling and Building Multiple File Programs

Here is an example of a program that runs using an input/output (aka I/O) utility. There are two files here. The first one is the program source file you will have been working on (testprog.c). The second file holds the utilities you will be using to conduct your input and output operations (Console_IO_Utility.c). The process for compiling and building them is not very different. You just put both files in where you previously just put one file, as shown here.

Compile Operation with multiple C files

Even More Advanced Compiling

One of the things that happens in more advanced programming is that you may have several files that are compiled to create a program. You need to know that there are really two processes that happen when a program is compiled and built. First, each file is compiled, that is, each file is converted into the ones and zeros that the computer can understand; this is the compiling process. However, that is not the end of the building process. All the compiled files must be linked together to create the actual program using a process called, believe it or not, linking.

There are many times where you will only be working on one file and you don't need to keep recompiling the other file. The example above is a good one. The Console_IO_Utility.c file only needs to be compiled once; you won't be modifying it when you are working on your own file. For this situation, you can compile each file separately to an object file which has a .o extension in some systems. Once that is done, you can link the two files together using essentially the same operation you did previously, only you link the object files instead of compiling and linking the source files together. Here is the process of individually compiling the source files to object files.

Compiling but not linking .c files

Notice that the object or .o files are found in the directory after the compiling process is completed. However, the .exe file is not there yet. When it comes time to link the files and create the complete program, the linking process looks like the early multiple-file compiling process only you use the object files instead of the source files. In this case, with only two files, it is not too big a deal. However, when you get into larger projects with many files, this could save a lot of time. Observe the linking process here.

Final linking process

One final note on this is that there can come times in industry or advanced academia where you will be working on a given source file but not have the other files to work. However, you always want to make sure the code you are writing is correct. You can, and should, compile after no more than two or three lines of your coding, and even though you do not have the other files to build the program, you can still compile the one file you are working on by itself. Easy Peasy, and effective too.

All About Compiling, Linking, and Building

First, you should be aware that the above information will get all the jobs done you need to do to learn C programming. However, there is another strategy that can be used, especially for larger projects. The make operation can automate your work when you get further along in your study of programming. You may use this operation when working with this reference but the make operation and associated file may work differently between differing operating systems so you will need to figure out the one that works on your computer. It is not terribly difficult and there is a lot of (mostly good) support on the Internet so if you are motivated, you can check that out.

Finally, there are parts of this topic you do not need right away. However, having this reference available for your future work will be helpful. Be prepared to return to this topic and take advantage.