The Compilation Process: Compile, Link, Go

UnderC is quick at getting programs running, but not very fast at executing them. Once you are happy with your program and need to generate a standalone .EXE file, you need to build the program using a C++ compiler and linker.

A C++ compiler translates .cpp files into object files that contain machine code. These files can't be executed immediately because they contain unresolved references to other functions, some of which are in the other object files of the project but most of which are contained in the libraries. In the case of GCC, the object files end in .o, and the library files end in .a (for “archive”). For BCC32 and Microsoft C++ (CL), the object files end in .obj, and the library files end in .lib.

The linker resolves all the function references in the object files, packing them together into an executable program. Not all the code a program needs is necessarily linked statically; much of the runtime libraries sit in MSVCRT40.DLL, which is a DLL. (DLLs are often called shared libraries.) DLLs are loaded only when the program begins execution. (Windows is mostly a collection of DLLs plus device drivers.) The file that results from the link phase is by default called a.exe in GCC, but it is easy to modify the name by using the -o command-line option, which is followed by the desired name, as in the following example:

C:gcc	est>c++ hello.cpp
C:gcc	est>a
						10 20
Program is C:GCCTESTA.EXE
arg 0 C:GCCTESTA.EXE
arg 1 10
arg 2 2
C:gcc	est>c++ -o hello.exe –s hello.cpp
C:gcc	est>hello
Program is C:GCCTESTHELLO.EXE

The resulting executable from the first command is about 167KB, which seems large for a 10-line program, but GCC defaults to outputting debugging information. So using the –s command-line option (which strips the debug information from the file) brings the file size down to 75KB.

If you really need a small program and can live with some restrictions, you can do two things: You can use C input and output (that is, printf(), and so forth) or you can tell the compiler to use the UnderC pocket libraries. You use the -I command-line option to specify the UnderC for Windows (UCW) include directories. If the UCW directory is c:UCW, the following command builds a 23KB “Hello, World!” C++ program that is noticeably faster to compile and link than using the default C++ libraries. This shows that there is nothing intrinsically big and bloated about C++ programs; it all depends on the libraries.

C:gcc	est>c++ -o hello.exe -s -I"c:ucwinclude" hello.cpp
					

In Quincy 2000, you can achieve the same effect by selecting the Build tab of the Options dialog, and putting c:ucwinclude in the Includes text field.

To build a program that contains a number of files, you can put more than one .cpp file on the command line. The following example first shows you the one-line command to build a program composed of two files, and then breaks this down into two separate compilation steps and one separate link step (note the –c option, which tells GCC to compile but not link):

C:gcc	est>c++ -o test.exe one.cpp two.cpp
C:gcc	est>c++ -c one.cpp
C:gcc	est>c++ -c two.cpp
C:gcc	est>c++ -o test.exe one.o two.o
					

The trouble with building this program on one line is that each file is compiled separately, whether it needs to be recompiled or not. This gets to be a hassle for anything more than a simple project. Using –c, you can compile a C++ file manually, but then you need to recompile anything that might have changed—remember that a file must be recompiled if any header (include) file changes. Creating a project in Quincy 2000 is one way to build a program efficiently; later in this appendix, you will learn how to put together a simple makefile.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.154.64