How to Build a C++ Executable Program

To build an executable from a C++ source file called demo.cxx, enter the following command:

% cxmcore demo.cxx

The driver recognizes demo.cxx as a valid C++ source file by its.cxx extension and invokes the C++ compiler. The compiler produces an assembly code file, which is then sent to the assembler, which produces an object file. The object file is then sent to the linker and linked with the appropriate libraries selected by the driver.

If no errors occur, an ELF format executable file called a.out is created in the current directory. You can rename the output file with the -o compiler driver option. For example:

% cxmcore demo.cxx -o demo

This command creates an ELF format executable file called demo in the current directory. The filename must immediately follow the -o option. When a single source file is compiled and linked in this way, all intermediate .s and .o files are deleted.

When more than one file is given to the driver, any object files that are created in the process are not deleted. This is convenient when only one of the files must be recompiled. For example, suppose that you compile several files:

% cxmcore demo.cxx file1.cxx file2.cxx

You then discover that demo.cxx must be modified. After editing demo.cxx, you can rebuild the executable with this command:

% cxmcore demo.cxx file1.o file2.o

Since file1.cxx and file2.cxx have not been modified, it is possible to use the object modules file1.o and file2.o created by the previous compilation.

Assembly source files may be input to the compiler driver if the name of the file ends with .s. For example:

% cxmcore demo.s

For assembly source files, the compiler driver first invokes the assembler and then the linker to produce the file a.outin the current directory.

If the -c option is used, the compiler driver stops after creating an object file for each source file from the command line. The following command line produces two relocatable object modules, called demo.o and file.o, in the current directory:

% cxmcore -c demo.cxx file.s

If only one object file is created, you may use the -o option with the -c option to rename the object file. The new name must contain the suffix .o. For example, the following command line creates the relocatable object module newdemo.o in the current directory:

% cxmcore -c demo.cxx -o newdemo.o

The compiler driver links relocatable object modules into an executable file. If all of the names of the input files to the driver end in.o, the compiler driver invokes only the linker. The following command line links the demo.o, file1.o, and file2.o object modules with the necessary startup code and libraries to produce the file a.out:

% cxmcore demo.o file1.o file2.o

Previous

Next



Copyright © 1999, Green Hills Software. All rights reserved.