Skip Navigation Links | |
Exit Print View | |
Oracle Solaris Studio 12.3: C++ User's Guide Oracle Solaris Studio 12.3 Information Library |
2.2.3 Using Multiple Source Files
2.3 Compiling With Different Compiler Versions
2.4.2 Separate Compiling and Linking
2.4.3 Consistent Compiling and Linking
2.4.4 Compiling for 64-Bit Memory Model
2.4.5 Compiler Command-Line Diagnostics
2.4.6 Understanding the Compiler Organization
2.5 Preprocessing Directives and Names
2.5.2 Macros With a Variable Number of Arguments
2.6.3 Control of Virtual Memory
2.7 Using the strip Command with C++ Objects
2.8.1 Using Aliases Within the C Shell
2.8.2 Using CCFLAGS to Specify Compile Options
2.8.3.1 Using CCFLAGS Within make
3. Using the C++ Compiler Options
6. Creating and Using Templates
9. Improving Program Performance
10. Building Multithreaded Programs
12. Using the C++ Standard Library
This section gives you a brief overview of how to use the C++ compiler to compile and run C++ programs. See Appendix A, C++ Compiler Options for a full reference to the command-line options.
Note - The command-line examples in this chapter show CC usages. Printed output might be slightly different.
The basic steps for building and running a C++ program involve the following tasks:
Using an editor to create a C++ source file with one of the valid suffixes listed in Table 2-1
Invoking the compiler to produce an executable file
Launching the program into execution by typing the name of the executable file
The following program displays a message on the screen:
example% cat greetings.cc #include <iostream> int main() { std::cout << “Real programmers write C++!” << std::endl; return 0; } example% CC greetings.cc example% ./a.out Real programmers write C++! example%
In this example, CC compiles the source file greetings.cc and, by default, compiles the executable program onto the file, a.out. To launch the program, type the name of the executable file, a.out, at the command prompt.
Traditionally, UNIX compilers name the executable file a.out. It can be awkward to have each compilation write to the same file. Moreover, if such a file already exists, it will be overwritten the next time you run the compiler. Instead, use the -o compiler option to specify the name of the executable output file, as in the following example:
example% CC– o greetings greetings.cc
In this example, the -o option tells the compiler to write the executable code to the file greetings. (Common practice is to give a program consisting of a single source file the name of the source file without the suffix.)
Alternatively, you could rename the default a.out file using the mv command after each compilation. Either way, run the program by typing the name of the executable file:
example% ./greetings Real programmers write C++! example%