Using the C++ Compiler |
2 |
![]() |
% CC myprog.cc -o myprogThe resulting executable file is called myprog because this command line uses the -o name argument. Without that argument, the executable file has the default name, a.out.
The possible file name extensions for the source file are: .c, .C, .cc, .cpp, or .cxx.
CC [options] list_of_files [-lx] |
When you have a second module like the string class module, both the implementation part of the second module and the main program module must include the header file for the second module. For example, testr.cc and str.cc include the header file, str.h, with a line like:
#include "str.h"If there is no object file for the second module, you can compile the second module and link it with the program with a command line like:
% CC testr.cc str.cc -o testrAlternately, you can create an object file for the second module with this command line:
% CC -c str.ccThis line does not invoke the linker and results in an object file called str.o.
When there is an object file for the unit, you can compile the program and link it with the unit, as follows:
% CC str.o testr.cc -o testr
Here is an example of how to add suffixes.
.C:
$(LINK.cc) -o $@ $< $(LDLIBS)
.C.o:
$(COMPILE.cc) $(OUTPUT_OPTION) $<
.C.a:
$(COMPILE.cc) -o $% $<
$(AR) $(ARFLAGS) $@ $%
$(RM) $%
Note - Since .c is supported as a C-language suffix, it is the one suffix that cannot be added to the SUFFIXES macro to support C++. Write explicit rules in your own makefile to handle C++ files with a .c suffix.
.C: $(LINK.cc) -o $@ $< $(LDLIBS) .C.o: $(COMPILE.cc) $(OUTPUT_OPTION) $< .C.a: $(COMPILE.cc) -o $% $< $(AR) $(ARFLAGS) $@ $% $(RM) $% |
#pragma preprocessor-tokenspecify implementation-defined actions.
The pragmas discussed in this section are recognized in the compilation system. The compiler ignores unrecognized pragmas.
Note - #pragmas in template definition files are ignored.
#pragma align 64 (aninteger, astring, astruct) int aninteger; static char astring[256]; struct S {int a; char *b;} astruct; |
Within a source file, the functions specified in #pragma init are executed after the static constructors in that file. You must declare the identifiers before using them in the #pragma.
Use #pragma fini (identifier [, identifier]...) to mark identifier as a finalization function. Such functions are expected to be of type void, to accept no arguments, and to be called either when a program terminates under program control or when the containing shared object is removed from memory. As with initialization functions, finalization functions are executed in the order processed by the link editor.
In a source file, the functions specified in #pragma fini are executed after the static destructors in that file. You must declare the identifiers before using them in the #pragma.
Because such routines render standard flowgraph analysis invalid, routines that call them cannot be safely optimized; hence, they are compiled with the optimizer disabled.
#pragma weak function_namedefines function_name to be a weak symbol. No error messages are generated if the linker cannot find a definition for function_name. The line:
#pragma weak function_name1 = function_name2defines function_name1 to be a weak symbol, an alias for the symbol function_name2. You must declare function_name1 and function_name2 before using them in the pragma. For example:
extern void bar(int) extern void _bar(int) #pragma weak _bar=bar |
In addition, these descriptions are summarized in:
Note - In the descriptions are many references to the linker, ld. For details about ld, see the ld(1) man page.
This option is the old style of basic block profiling for tcov. See -xprofile=tcov for information on the new style of profiling, the tcov(1) man page, and Profiling Tools for more details.
If set at compile time, the TCOVDIR environment variable specifies the directory where the .d files are located. If this variable is not set, then the .d files remain in the same directory as the .f files.
The -xprofile=tcov and the -a options are compatible in a single executable. That is, you can link a program that contains some files that have been compiled with -xprofile=tcov, and others that have been compiled with -a. You cannot compile a single file with both options.
For more information on this option on SPARC, x86 and PowerPC platforms, see the ld(1) man page and the Solaris documentation.
-lC -lC_mtstubs -lm -lw -lcx -lc
For more details on -l, refer to "-llib" on page 31.
To link some of these libraries statically, use -nolib, as described in "-nolib" on page 34. To link libC statically, you can also use the
-Bstatic
Directs the link editor to look only for files named liblib.a. The .a suffix indicates that the file is static, that is, nonshared. Use this option if you want nonshared library bindings for linking. On Solaris, this option and its arguments are passed to the linker, ld. -c
Directs the CC driver to suppress linking with ld and produces a .o file for each source file. If you specify only one source file on the command-line, then you can explicitly name the object file with the -o option. For example:
-cg[89|92]
(SPARC) Specifies the code generator for floating-point hardware in SPARC systems released in 1989 or 1992. Use the fpversion command to determine which floating-point hardware you have. If you compile one procedure of a program with this option, it does not mean that you must compile all the procedures of that program in the same way. -cg89
(SPARC)-cg89 expands to:
-xarch=v7 -xchip=old -xcache=64/32/1. -cg92
(SPARC) -cg92 expands to:
-xarch=v8 -xchip=super -xcache=16/32/4:1024/32/1. +d
Prevents the compiler from expanding inline functions. This option is turned on when you specify -g, the debugging option.
-Dname[=def]
Defines a macro symbol name to the preprocessor. Doing so is equivalent to including a #define directive at the beginning of the source. If you do not use the argument =def, name is defined as 1. You can use multiple -D options.
-fast
Selects a combination of compilation options for optimum execution speed. This option provides near maximum performance for most applications by choosing the following compilation options:
Option |
SPARC |
x86 |
PowerPC |
-dalign |
X |
|
|
-fns |
X |
X |
X |
-fsimple |
X |
|
|
-ftrap=%none |
X |
X |
X |
-libmil |
X |
X |
X |
-nofstore |
|
X |
|
-O4 |
X |
X |
X |
-xlibmopt |
X |
X |
|
-xtarget=native |
X |
X |
X |
The code generation option, optimization level, and use of inline template files can be overridden by subsequent flag switches. For example, although the optimization level set by -fast is -O4, if you specify -fast -O3, the optimization level becomes -O3. The optimization level that you specify will override a previously set optimization level.
The -fast option includes -fns -ftrap=%none; that is, this option turns off all trapping. In previous SPARC releases, the -fast macro option included
Note - The criteria for the -fast option vary with the C, C++, Fortran 77, and Pascal compilers. Please see the appropriate documentation for the specifics.
-fnonstd; now it does not. -features=a
Enables/disables various C++ language features. a must be one or more of: [no%]anachronisms, [no%]castop, [no%]rtti
Value |
Meaning |
[no%]anachronisms |
[Do not] Allow anachronistic constructs |
[no%]castop |
[Do not] Allow new-style casts (dynamic or otherwise) |
[no%]rtti |
[Do not] Allow RTTI (dynamic_cast <> and typeid) |
-flags
Displays a brief description of each compiler option. Also displayed are phone numbers to call for additional information on Sun products and technical support, and instructions on how to send comments on Sun products. -fnonstd
Causes nonstandard initialization of floating-point arithmetic hardware. In addition, the -fnonstd option causes hardware traps to be enabled for floating-point overflow, division by zero, and invalid operations exceptions. These results are converted into SIGFPE signals; if the program has no SIGFPE handler, it terminates with a memory dump. See the Numerical Computation Guide for more information. -fns
Turns on the nonstandard floating-point mode. The default is the standard floating-point mode. -fprecision=p
(Intel) Sets floating-point rounding precision mode.
Value |
Meaning |
single |
Rounds to an IEEE single-precision value |
double |
Rounds to an IEEE double-precision value |
extended |
Rounds to the maximum precision available |
-fround=r
Sets the IEEE 754 rounding mode.
If you compile one routine with -fround=r, compile all routines of the program with the same -fround=r option; otherwise, you can get unexpected results.
If n is present, it must be 0, 1 or 2. The defaults are:
To turn off this option, use the -nofstore option.
t is a comma-separated list that consists of one or more of the following: %all, %none, common, [no%]invalid, [no%]overflow, [no%]underflow, [no%]division, [no%]inexact.
The default is -ftrap=%none.
This option sets the IEEE 754 trapping modes that are established at program initialization. Processing is left to right. The common exceptions, by definition, are invalid, division (by zero), and overflow.
For example: -ftrap=%all,no%inexact means to set all traps except inexact.
The meanings are the same as for the ieee_flags subroutine, except that:
Use this option when building shared libraries of templates. Any generated templates are then automatically included in the library.
The following text options are passed to ld:
-g0
Instructs the compiler to prepare the file or program for debugging, but not to disable inlining. This option is the same as -g, except that +d is not enabled.
-H
Prints, one per line, the path name of each #include file included during the current compilation on the standard error output (stderr). -help
Same as -flags, as described in "-flags" on page 23.
-hname
Names a shared dynamic library and provides a way to have versions of a shared dynamic library.% CC -G -o libx.so.1 -h libx.so.1 a.o b.o c.o
-i
Tells the linker to ignore any LD_LIBRARY_PATH setting. -Ipathname
Adds pathname to the list of directories that are searched for #include files with relative file names--those that do not begin with a slash. The preprocessor searches for #include files in this order:
/opt/SUNWspro/SC4.2/include/CC
/opt/SUNWspro/SC4.2/include/cc
Note - If -ptipath is not used, the compiler looks for template files in -Ipath. Use -Ipath instead of -ptipath.
If a function specified in the list is not declared as extern "C", the function name should be mangled. You can use the nm command on the executable file to find the mangled function names. For functions declared as extern "C", the names are not mangled by the compiler.
Note - This option does not affect C++ inline functions and is not related to the +d option.
If you compile with the flag -O3, using this option can increase optimization by restricting inlining to only those routines in the rlst list.
If you compile with the flag -O4, the compiler tries to inline all user-written subroutines and functions.
A routine is not inlined if any of the following conditions apply--no warning is issued.
a must be one of: static, extern, global. These linkages are mutually exclusive. The following table shows the values of a.
-keeptmp
Retains the temporary files that are created during compilation. Along with the -v option, this option is useful for debugging, especially when you have template functions or classes. -KPIC
Same as the -PIC option, as described in "-PIC" on page 40.
-Kpic
Same as the -pic option, as described in "-pic" on page 40.
-Ldir
Adds dir to the list of directories to be searched by the linker for libraries that contain object-library routines during the link step with ld. The directory, dir, is searched before compiler-provided directories. -llib
Specifies additional libraries for linking with object files. This option behaves like the -l option when it is used with CC or ld. Normal libraries have names like libsomething.a, where the lib and .a parts are required. You can specify the something part with this option. Put as many libraries as you want on a single command line; they are searched in the order specified with -L. -libmieee
Causes libm to return values in the spirit of IEEE 754. The default behavior of libm is SVID-compliant. -libmil
Inlines some library routines for faster execution. This option selects the best assembly language inline templates for the floating-point option and platform on your system.
Note - This option does not affect C++ inline functions.
-library=l
Determines C++ library use.
-Y P,-R and -l) paths are passed to the compiler in order to use a particular library. During the link phase, the correct libraries will be presented to the linker.
If selected, libraries are linked in the order: rwtools7, rwtools6, complex, libC, libm. The default is -library=%none, libC, libm.
-migration
Displays the contents of the C++ Migration Guide, which contains information about incompatibilities between C++ 3.0, based on Cfront, and the current compiler. -misalign
(SPARC) (PowerPC) Permits misaligned data, which would otherwise generate an error, in memory, as shown in the following code:
char b[100]; int f(int *ar){ return *(int *)(b +2) + *ar; } |
Thus, very conservative loads and stores must be used for the data, that is, one byte at a time. Using this option can cause significant degradation in performance when you run the program.
-mt
Compiles and links a multithreaded program and passes -D_REENTRANT to the preprocessor. The following command is passed to ld: -lC -lm -lw -lthread -lcx -lc
instead of:-lC -lm -lC_mtstubs -lw -lcx -lc
Note - To ensure proper library linking order, use this option rather than -lthread, to link with libthread.
-native
Chooses the correct code generator option. This option directs the compiler to generate code targeted for the machine that is doing the compilation. -noex
Instructs the compiler not to generate code that supports C++ exceptions. -nofstore
(Intel) Does not convert the value of a floating-point expression or function to the type on the left side of an assignment when that expression or function is assigned to a variable, or is cast to a shorter floating-point type; rather, leaves the value in a register.
-nolib
Does not link with any system or library by default; that is, no -l options are passed to ld. You can then link some of the default libraries statically instead of dynamically.% CC test.c -nolib -Bstatic -lC -lm -lC_mtstubs -lw -lcx \
The order of the -l options is significant. The -lC, -lm, -lC_mtstubs, -lw,and -lcx options must appear before -lc.
-Bdynamic -lc
(PowerPC only) In this release, all C++ programs will be linked with -labi, which the driver adds to the link line. If you choose to use -nolib, you must manually link with -labi. If you do not link with -labi, the following three entry points required for exception handling will not be defined, and the program will not link:
-nolibmil
Resets -fast so that it does not include inline templates. Use this option after the -fast option, as in:% CC -fast -nolibmil
-noqueue
If no license is available, returns without queuing your request and without compiling. A nonzero status is returned for testing makefiles. -norunpath
Does not build the path for shared libraries into the executable. If an executable file uses shared libraries, then the compiler normally builds in a path that points the runtime linker to those shared libraries. To do so, the compiler passes the -R option to ld. The path depends on the directory where you have installed the compiler. -Olevel
Optimizes the execution time. Omitting level is equivalent to using level -O2. For SPARC Systems
-O1: Does only the minimum amount of optimization (peephole), which is postpass, assembly-level optimization. Do not use -O1 unless using -O2 or -O3 results in excessive compilation time, or you are running out of swap space.
-O3: Also optimizes references and definitions for external variables in addition to optimizations performed at the -O2 level. This level does not trace the effects of pointer assignments. Do not use it when compiling either device drivers, or programs that modify external variables from within signal handlers. In general, this level results in increased code size.
-O4: Does automatic inlining of functions contained in the same file in addition to performing -O3 optimizations. This automatic inlining usually improves execution speed, but sometimes makes it worse. In general, this level results in increased code size.
-O5: Generates the highest level of optimization. Uses optimization algorithms that take more compilation time or that do not have as high a certainty of improving execution time. Optimization at this level is more likely to improve performance if it is done with profile feedback. See"-xprofile=p" on page 56.
For Intel and PowerPC Systems
-O1: Preloads arguments from memory; causes cross jumping (tail merging), as well as the single pass of the default optimization. For All Systems
For most programs:
If the optimizer runs out of memory, it tries to recover by retrying the current procedure at a lower level of optimization. The optimizer then resumes subsequent procedures at the original level specified in the -O option.
If you optimize at -O3 or -O4 with very large procedures (thousands of lines of code in a single procedure) the optimizer may require an unreasonable amount of memory. In such cases, machine performance may be degraded.
To prevent this degradation from taking place, use the limit command to limit the amount of virtual memory available to a single process; see the csh(1) man page. For example, to limit virtual memory to 16 megabytes:
% limit datasize 16MThis command causes the optimizer to try to recover if it reaches 16 megabytes of data space.
This limit cannot be greater than the total available swap space of the machine, and the limit should be small enough to permit normal use of the machine while a large compilation is in progress.
For example, on a machine with 32 megabytes of swap space, the limit datasize 16M command ensures that a single compilation never consumes more than half of the machine swap space.
The best setting for data size depends on the degree of optimization requested, the amount of real memory, and virtual memory available.
You can also perform this task with the Analyzer. Refer to the analyzer(1) man page.
You can also perform this task with the Analyzer. Refer to the analyzer(1) man page.
Same as -pic.
(SPARC) (PowerPC) Similar to -pic, but the global offset table spans the range of 32-bit addresses in those rare cases where there are too many global data objects for --pic.
Each reference to a global datum is generated as a dereference of a pointer in the global offset table. Each function call is generated in pc-relative addressing mode through a procedure linkage table. The size of the global offset table is limited to 8 Kbytes on SPARC processors.
-ptipath
Specifies an additional search directory for template source.
-Ipathname. If the -ptipath flag is used, the compiler looks for template definition files on this path and ignores the -Ipathname flag. It is recommended that you use the -Ipathname flag instead of -ptipath. -pto
Use -instances=static. See also "-instances=a" on page 30.
-ptrdatabase-path
Specifies the directory of primary and secondary build repositories. The template database is named Templates.DB; database-path is the directory containing the database, but does not include the name itself.
If you omit this option, a default database, ./Templates.DB, is created for you. To avoid confusion when multiple databases are involved, use -ptr. as the first entry on the command line.
-Qoption|-qoption prog opt
Passes the option, opt, to the phase, prog. Refer to "Components" on page 13 for more information on compiler phases.
Table 2-2 shows the possible values for prog:
SPARC Architecture |
x86 Architectures |
ccfe |
ccfe |
iropt |
cg386 |
cg |
codegen |
tdb_link |
tdb_link |
ld |
ld |
To pass more than one option, specify them in order as a comma-separated list. In the following command line, when ld is invoked by the CC driver, -Qoption passes the -i and -m options to ld:
.i
Preprocessed C++ source from ccfe
.o
Object file from cg, the code generator
.s
Assembler source from cg
% CC -Qoption ld -i,-m test.c
-qp
Use -p. -Qproduce|-qproduce sourcetype
Causes the CC driver to produce source code of the type sourcetype. Source code types are shown in Table 2-3.
Table 2-3 Source Code Types
-R pathname
Passes a colon-separated list of search paths to the linker for library searching during runtime linking. -readme
Displays the contents of the README file, paged by the command specified by the environment variable, PAGER. If PAGER is not set, the default paging command is more.
-S
Causes the CC driver to compile the program and output an assembly source file, but not assemble the program. The assembly source file is named with a .s suffix. -s
Removes all symbol information from output executable files. This option is passed to ld. -sb
Causes the CC driver to generate extra symbol table information in a SourceBrowser database in the .sb directory for the sbrowser program. -sbfast
Runs only the ccfe phase to generate extra symbol table information in a SourceBrowser database in the .sb directory for the sbrowser program. No object file is generated. -staticlib=l
Determines whether C++ libraries are linked statically.
For example, you can link libC statically as follows:
-temp=dir
Sets the name of the directory for temporary files, generated during the compilation process, to dir. See also "-keeptmp" on page 31.
-template=w
Enables/disables various template options.
-time
Causes the CC driver to report execution times for various compilation passes. -Uname
Removes any initial definition of the macro symbol name. This option:
v must be one or more of: template, diags, version.
You may specify more than one option, for example, -verbose=template, diags
The following table shows the values of v.
+w
Generates additional warnings about questionable constructs that are:
-xar
Creates archive libraries.CC -xar -O libmain.a a.o b.o c.o
archives the template functions contained in the library and object files. -xarch=a
Limits the set of instructions the compiler may use.
On SPARC: |
generic, v7, v8a, v8, v8plus, v8plusa |
On PowerPC: |
generic, ppc, ppc_nofma |
On Intel: |
generic, 386, pentium_pro |
Although this option can be used alone, it is part of the expansion of the
-xtarget option; its primary use is to override a value supplied by the
-xtarget option.
-xcache=c
(SPARC) Defines the cache properties that the optimizer can use. It does not guarantee that any particular cache property is used.
si |
The size of the data cache at level i, in kilobytes |
li |
The line size of the data cache at level i, in bytes |
ai |
The associativity of the data cache at level i |
Although this option can be used alone, it is part of the expansion of the
-xtarget option; its primary use is to override a value supplied by the
-xtarget option.
Example: -xcache=16/32/4:1024/32/1 specifies the following:
Level 1 cache has: 16 Kbytes 32 bytes line size 4-way associativity |
Level 2 cache has: 1024 Kbytes 32 bytes line size Direct mapping associativity |
-xcg89
(SPARC) Same as the -cg89 option. See "-cg[89|92]" on page 19.
-xcg92
(SPARC) Same as the -cg92 option. See "-cg[89|92]" on page 19.
-xchip=c
(SPARC) Specifies the target processor for use by the optimizer.
This option specifies timing properties by specifying the target processor.
This option affects:
-xF
With -noex, enables reordering of functions, using the compiler, the Analyzer, and the linker.LOAD ? RXO
The analyzer(1) and ld(1) man pages provide further details on this option.
Note - The -xF option will not work unless you also specify the -noex option.
-xildoff
Turns off the incremental linker and forces the use of ld. This option is the default if you do not use the -g option. Override this default by using the
-xildon option. -xildon
Turns on the incremental linker and forces the use of ild in incremental mode. This option is the default if you use the -g option. Override this default by using the -xildoff option. -xinline=rlst
Same as -inline, as described in "-inline=rlst" on page 29.
-xlibmieee
Same as -libmieee, as described in "-libmieee" on page 31.
-xlibmil
Same as -libmil, as described in "-libmil" on page 32.
-xlibmopt
(SPARC)(Intel) Uses a math routine library optimized for performance. The results may be slightly different from those produced by the normal math library. This option is implied by the -fast option.
-xlic_lib=l
(SPARC)(Intel) Links in the Sun-supplied, licensed libraries specified in l. For example, to link with the Sun Performance Library, use -xlic_lib=sunperf. This option, like -l, should appear at the end of the command line, after source or object file names. -xlicinfo
Displays information on the licensing system. In particular, this option returns the license-server name and the user ID for each user who has a license checked out. When you use this option, the compiler is not invoked, and a license is not checked out. -Xm
Allows the use of the character $ in identifier names, except as the first character. -xM
Outputs makefile dependency information. For example, with the following code, hello.c:
#include <stdio.h> main() { (void) printf ("hello0"); } |
When you compile with this option:
% CC -xM hello.c
The output shows the dependencies:hello.o: hello.c
See make(1) for details about makefiles and dependencies.
hello.o: /usr/include/stdio.h
-xM1
Same as -xM, except that this option does not output dependencies for the /usr/include header files. When you compile the same code provided with the -xM option, the output is:hello.o: hello.c
-xMerge
Merges the data segment with the text segment. The data in the object file is read-only, and is shared between processes, unless you link with ld -N. -xnolib
Same as -nolib, as described in "-nolib" on page 34.
-xnolibmopt
(SPARC)(Intel) Disables -fast and does not use the math routine library.
CC -fast -xnolibmopt -xOlevel
Same as -Olevel, as described in "-Olevel" on page 36.
-xpg
Same as -pg, as described in "-pg" on page 39.
-xprofile=p
Collects data for a profile or uses a profile to optimize.
-xregs=r
(SPARC) Specifies the usage of registers for the generated code.
The default is -xregs=appl,float.
-xs
Disables Auto-Read for dbx. Use this option in case you cannot keep the .o files around. This option passes the -s option to the assembler. -xsafe=mem
(SPARC) Allows the compiler to assume no memory-based traps occur. -xsb
Same as -sb, as described in "-sb" on page 43.
-xsbfast
Same as -sbfast, as described in "-sbfast" on page 43.
-xspace
(SPARC) Does no optimizations that increase code size. -xtarget=t
Specifies the target system for instruction set and optimization.
On Intel, -xtarget= accepts:
-xtime
Same as -time, as described in "-time" on page 45.
-xunroll=n
Same as -unroll=n, as described in "-unroll=n" on page 45.
-xwe
Converts all warnings to errors by returning non zero exit status. -Ztha
(SPARC) Prepares code for analysis by the Thread Analyzer, the performance analysis tool for multithreaded code. -ztext
Forces a fatal error if any relocations remain against non writable, allocatable sections.