C++ User's Guide

Simplifying Commands

You can simplify complicated compiler commands by defining special shell aliases, using the CCFLAGS environment variable, or by using make.

Using Aliases Within the C Shell

The following example defines an alias for a command with frequently used options.


demo% alias CCfx "CC -fast -xnolibmil"

The next example uses the alias CCfx.


demo% CCfx any.C

The command CCfx is now the same as:


demo% CC -fast -xnolibmil any.C

Using CCFLAGS to Specify Compile Options

You can specify options by setting the CCFLAGS variable.

The CCFLAGS variable can be used explicitly in the command line. The following example shows how to set CCFLAGS (C Shell):


 demo% setenv CCFLAGS '-silent -fast -Xlist'

The next example uses CCFLAGS explicitly.


 demo% CC $CCFLAGS any.cc

When you use make, if the CCFLAGS variable is set as in the preceding example and the makefile's compilation rules are implicit, then invoking make will result in a compilation equivalent to:

CC -silent -fast -Xlist files...

Using make

make is a very powerful program development tool that you can easily use with all Sun compilers. See the make(1) man page for additional information.

Using CCFLAGS Within make

When you are using the implicit compilation rules of the makefile (that is, there is no C++ compile line), CCFLAGS is used automatically by the make program.

Adding a Suffix to Your Makefile

You can incorporate different file suffixes into C++ by adding them to your makefile. The following example adds .C as a valid suffix for C++ files.Add the SUFFIXES macro to your makefile:

.SUFFIXES: .cpp .cpp~

(This line can be located anywhere in the makefile.)

Add the following lines to your makefile. Indented lines must start with a tab.


.cpp:
    $(LINK.cc) -o $@ $< $(LDLIBS)
.cpp~:
    $(GET) $(GFLAGS) -p $< > $*.cpp
    $(LINK.cc) -o $@ $*.cpp $(LDLIBS)
.cpp.o:
    $(COMPILE.cc) $(OUTPUT_OPTION) $<
.cpp~.o:
    $(GET) $(GFLAGS) -p $< > $*.cpp
    $(COMPILE.cc) $(OUTPUT_OPTION) $<
.cpp.a:
    $(COMPILE.cc) -o $% $<
    $(COMPILE.cc) -xar $@ $%
    $(RM) $%
.cpp~.a:
    $(GET) $(GFLAGS) -p $< > $*.cpp
    $(COMPILE.cc) -o $% $<
    $(COMPILE.cc) -xar $@ $%
    $(RM) $%

Using make With iostreams

The standard iostream file names do not have ".h" suffixes. Instead, they are named istream, fstream, and so forth. In addition, the template source files are named istream.cc, fstream.cc, and so forth. If you include a standard iostream header, such as <istream>, in your program and your makefile has .KEEP_STATE, you will encounter problems. For example, if you include <istream>, make thinks that istream is an executable and uses the default rules to build istream from istream.cc resulting in very misleading error messages. (Both istream and istream.cc are installed under SC5.0/include/CC). To prevent make from using the default rules, use the -r option.