Contidos dentroLocalizar Mais DocumentaçãoDestaques de Recursos de Suporte | Fazer download desta apostila em PDF (1976 KB)
Part I C++ CompilerChapter 1 The C++ CompilerThis chapter provides information about the following: 1.1 New Features and Functionality of the Sun Studio 12 C++ 5.9 CompilerThis section provides a brief overview of the new C compiler features and functionality introduced in the Sun Studio 12 C++ 5.9 Compiler release. For specific explanations, follow the cross references provided for each item.
1.1.1 Compiling for 64–Bit PlatformsThe way to specify compilation of a 32-bit or 64-bit binary has changed in this release. The A.2.107 –xarch=isaoption no longer carries an implicit memory model, 32-bit ILP32 or 64-bit LP64, with each definition, and is now used only to specify the instruction set of the target processor. Use the new A.2.50 -m32|-m64 options to specify the memory model of the target compilation. The ILP32 model specifies that C-language int, long, and pointer data types are all 32-bits wide. The LP64 model specifies that long and pointer data types are all 64-bits wide. The Solaris and Linux OS also support large files and large arrays under the LP64 memory model. When you compile with -m64, the resulting executable works only on 64-bit UltraSPARC(R) or x86 processors under Solaris OS or Linux OS running a 64-bit kernel. Compilation, linking, and execution of 64-bit objects can only take place in a Solaris or Linux OS that supports 64-bit execution. 1.1.2 Special x86 NotesThere are some important issues to be aware of when compiling for x86 Solaris platforms. The legacy Sun-style parallelization pragmas are not available on x86. Use OpenMP instead. See the Sun Studio 12: OpenMP API User’s Guide for information on converting legacy parallelization directives to OpenMP. Programs compiled with -xarch set to sse, sse2, sse2a, or sse3 must be run only on platforms that provide these extensions and features. Solaris OS releases starting with Solaris 9 4/04 are SSE/SSE2-enabled on Pentium 4-compatible platforms. Earlier versions of Solaris OS are not SSE/SSE2-enabled. If an instruction set selected by -xarch is not enabled in the running Solaris OS, the compiler will not be able to generate or link code for that instruction set. If you compile and link in separate steps, always link using the compiler and with same -xarch setting to ensure that the correct startup routine is linked. Numerical results on x86 might differ from results on SPARC due to the x86 80-bit floating-point registers. To minimize these differences, use the -fstore option or compile with x-arch=sse2 if the hardware supports SSE2. Numerical results can also differ between Solaris and Linux because the intrinsic math libraries (for example, sin(x)) are not the same. 1.1.3 Binary Compatibility VerificationStarting with Sun Studio 11 and the Solaris 10 OS, program binaries compiled and built using these specialized -xarch hardware flags are verified that they are being run on the appropriate platform. On systems prior to Solaris 10, no verification is done and it is the user's responsibility to ensure objects built using these flags are deployed on suitable hardware. Running programs compiled with these -xarch options on platforms that are not enabled with the appropriate features or instruction set extensions could result in segmentation faults or incorrect results occurring without any explicit warning messages. This warning extends also to programs that employ .il inline assembly language functions or __asm() assembler code that utilize SSE, SSE2, SSE2a, and SSE3 instructions and extensions. 1.2 Standards ConformanceThe C++ compiler (CC) supports the ISO International Standard for C++, ISO IS 14882:1998, Programming Language—C++. The readme file that accompanies the current release describes any departures from requirements in the standard. On SPARCTM platforms, the compiler provides support for the optimization-exploiting features of SPARC V8 and SPARC V9, including the UltraSPARCTM implementation. These features are defined in the SPARC Architecture Manuals, Version 8 (ISBN 0-13-825001-4), and Version 9 (ISBN 0-13-099227-5), published by Prentice-Hall for SPARC International. In this document, “Standard” means conforming to the versions of the standards listed above. “Nonstandard” or “Extension” refers to features that go beyond these versions of these standards. The responsible standards bodies may revise these standards from time to time. The versions of the applicable standards to which the C++ compiler conforms may be revised or replaced, resulting in features in future releases of the Sun C++ compiler that create incompatibilities with earlier releases. 1.3 C++ Readme FileThe C++ compiler’s readme file highlights important information about the compiler, including:
To view the text version of the C++ readme file, type the following at a command prompt:
To access the HTML version of the readme, in your Netscape Communicator 4.0 or compatible version browser, open the following file: /opt/SUNWspro/docs/index.html (If your C++ compiler-software is not installed in the /opt directory, ask your system administrator for the equivalent path on your system.) Your browser displays an index of HTML documents. To open the readme, find its entry in the index, then click the title. 1.4 Man PagesOnline manual (man) pages provide immediate documentation about a command, function, subroutine, or collection of such things. You can display a man page by running the command:
Throughout the C++ documentation, man page references appear with the topic name and man section number: CC(1) is accessed with man CC. Other sections, denoted by ieee_flags(3M) for example, are accessed using the -s option on the man command:
1.5 C++ UtilitiesThe following C++ utilities are now incorporated into traditional UNIX® tools and are bundled with the UNIX operating system:
See Program Performance Analysis Tools and associated man pages for further information on these UNIX tools. 1.6 Native-Language SupportThis release of C++ supports the development of applications in languages other than English, including most European languages and Japanese. As a result, you can easily switch your application from one native language to another. This feature is known as internationalization. In general, the C++ compiler implements internationalization as follows:
Variable names cannot be internationalized and must be in the English character set. You can change your application from one native language to another by setting the locale. For information on this and other native-language support features, see the operating system documentation. Chapter 2 Using the C++ CompilerThis chapter describes how to use the C++ compiler. The principal use of any compiler is to transform a program written in a high-level language like C++ into a data file that is executable by the target computer hardware. You can use the C++ compiler to:
2.1 Getting StartedThis section gives you a brief overview of how to use the C++ compiler to compile and run C++ programs. See 16.8 Using dlopen to Access a C++ Library From a C Program 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 program displays a message on the screen:
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:
In this example, the -o option tells the compiler to write the executable code to the file greetings. (It is common 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:
2.2 Invoking the CompilerThe remainder of this chapter discuss the conventions used by the CC command, compiler source line directives, and other issues concerning the use of the compiler. 2.2.1 Command SyntaxThe general syntax of a compiler command line is as follows:
An option is an option keyword prefixed by either a dash (–) or a plus sign (+). Some options take arguments. In general, the processing of the compiler options is from left to right, allowing selective overriding of macro options (options that include other options). In most cases, if you specify the same option more than once, the rightmost assignment overrides and there is no accumulation. Note the following exceptions:
Source files, object files, and libraries are compiled and linked in the order in which they appear on the command line. In the following example, CC is used to compile two source files (growth.C and fft.C) to produce an executable file named growth with runtime debugging enabled:
2.2.2 File Name ConventionsThe suffix attached to a file name appearing on the command line determines how the compiler processes the file. A file name with a suffix other than those listed in the following table, or without a suffix, is passed to the linker. Table 2–1 File Name Suffixes Recognized by the C++ Compiler
2.2.3 Using Multiple Source FilesThe C++ compiler accepts multiple source files on the command line. A single source file compiled by the compiler, together with any files that it directly or indirectly supports, is referred to as a compilation unit. C++ treats each source as a separate compilation unit. 2.3 Compiling With Different Compiler VersionsBeginning with the C++ 5.1 compiler, the compiler marks a template cache directory with a string that identifies the template cache’s version. This compiler does not use the cache by default. It only uses the cache if you specify -instances=extern. If the compiler makes use of the cache, it checks the cache directory’s version and issues error messages whenever it encounters cache version problems. Future C++ compilers will also check cache versions. For example, a future compiler that has a different template cache version identification and that processes a cache directory produced by this release of the compiler might issue an error that is similar to the following message:
Similarly, the compiler issues an error if it encounters a cache directory that was produced by a later version of the compiler. Although the template cache directories produced by the C++ 5.0 compiler are not marked with version identifiers, the current compiler processes the 5.0 cache directories without an error or a warning. The compiler converts the 5.0 cache directories to the directory format that it uses. The C++ 5.0 compiler cannot use a cache directory that is produced by a later release of the compiler. The C++ 5.0 compiler is not capable of recognizing format differences and it will issue an assertion when it encounters a cache directory that is produced by the C++ 5.1 compiler or by a later release. When you upgrade your compiler, it is always good practice to clean the cache. Run CCadmin -clean on every directory that contains a template cache directory (in most cases, a template cache directory is named SunWS_cache). Alternatively, you can use rm -rf SunWS_cache. 2.4 Compiling and LinkingThis section describes some aspects of compiling and linking programs. In the following example, CC is used to compile three source files and to link the object files to produce an executable file named prgrm.
2.4.1 Compile-Link SequenceIn the previous example, the compiler automatically generates the loader object files (file1.o, file2.o and file3.o) and then invokes the system linker to create the executable program for the file prgrm. After compilation, the object files (file1.o, file2.o,and file3.o) remain. This convention permits you to easily relink and recompile your files. Note – If only one source file is compiled and a program is linked in the same operation, the corresponding .o file is deleted automatically. To preserve all .o files, do not compile and link in the same operation unless more than one source file gets compiled. If the compilation fails, you will receive a message for each error. No .o files are generated for those source files with errors, and no executable program is written. 2.4.2 Separate Compiling and LinkingYou can compile and link in separate steps. The -c option compiles source files and generates .o object files, but does not create an executable. Without the -c option, the compiler invokes the linker. By splitting the compile and link steps, a complete recompilation is not needed just to fix one file. The following example shows how to compile one file and link with others in separate steps:
Be sure that the link step lists all the object files needed to make the complete program. If any object files are missing from this step, the link will fail with “undefined external reference” errors (missing routines). 2.4.3 Consistent Compiling and LinkingIf you do compile and link in separate steps, consistent compiling and linking is critical when using the following compiler options:
If you compile any subprogram using any of these options, be sure to link using the same option as well:
In the following example, the programs are compiled using the -xcg92 compiler option. This option is a macro for -xtarget=ss1000 and expands to: -xarch=v8 -xchip=super -xcache=16/64/4:1024/64/1.
If the program uses templates, it is possible that some templates will get instantiated at link time. In that case the command line options from the last line (the link line) will be used to compile the instantiated templates. 2.4.4 Compiling for SPARC V9The compilation, linking, and execution of 64-bit objects is supported only in a V9 SPARC, Solaris 8 operating system with a 64-bit kernel running. Compilation for 64-bits is indicated by the –xarch=v9, –xarch=v9a, and -xarch=v9b options. 2.4.5 Diagnosing the CompilerYou can use the –verbose option to display helpful information while compiling a program, such as the names and version numbers of the programs that it invokes and the command line for each compilation phase. Any arguments on the command line that the compiler does not recognize are interpreted as linker options, object program file names, or library names. The basic distinctions are:
In the following example, note that -bit is not recognized by CC and the option is passed on to the linker (ld), which tries to interpret it. Because single letter ld options can be strung together, the linker sees -bit as -b -i -t, all of which are legitimate ld options. This might not be what you intend or expect:
In the next example, the user intended to type the CC option -fast but omitted the leading dash. The compiler again passes the argument to the linker, which in turn interprets it as a file name:
2.4.6 Understanding the Compiler OrganizationThe C++ compiler package consists of a front end, optimizer, code generator, assembler, template prelinker, and link editor. The CC command invokes each of these components automatically unless you use command-line options to specify otherwise. Because any of these components may generate an error, and the components perform different tasks, it may be helpful to identify the component that generates an error. Use the -v and -dryrun options to help with this. As shown in the following table, input files to the various compiler components have different file name suffixes. The suffix establishes the kind of compilation that is done. Refer to Table 2–1 for the meanings of the file suffixes. Table 2–2 Components of the C++ Compilation System
2.5 Preprocessing Directives and NamesThis section discusses information about preprocessing directives that is specific to the C++ compiler. 2.5.1 PragmasThe preprocessor keyword pragma is part of the C++ standard, but the form, content, and meaning of pragmas is different for every compiler. See A.2.185 -z[ ]arg for a list of the pragmas that the C++ compiler recognizes. 2.5.2 Macros With a Variable Number of ArgumentsThe C++ compiler accepts #define preprocessor directives of the following form.
If the macro parameter list ends with an ellipsis, an invocation of the macro is allowed to have more arguments than there are macro parameters. The additional arguments are collected into a single string, including commas, that can be referenced by the name __VA_ARGS__ in the macro replacement list. The following example demonstrates how to use a variable-argument-list macro.
which results in the following:
2.5.3 Predefined NamesTable A–2 in the appendix shows the predefined macros. You can use these values in such preprocessor conditionals as #ifdef.The +p option prevents the automatic definition of the sun, unix, sparc, and i386 predefined macros. 2.5.4 #errorThe #error directive no longer continues compilation after issuing a warning. The previous behavior of the directive was to issue a warning and continue compilation. The new behavior, consistent with other compilers, is to issue an error message and immediately halt compilation. The compiler quits and reports the failure. 2.6 Memory RequirementsThe amount of memory a compilation requires depends on several parameters, including:
On the SPARC platform, 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 routines at the original level specified in the -xOlevel option on the command line. If you compile a single source file that contains many routines, the compiler might run out of memory or swap space. If the compiler runs out of memory, try reducing the level of optimization. Alternately, split multiple-routine source files into files with one routine per file. 2.6.1 Swap Space SizeThe swap -s command displays available swap space. See the swap(1M) man page for more information. The following example demonstrates the use of the swap command:
2.6.2 Increasing Swap SpaceUse mkfile(1M) and swap (1M) to increase the size of the swap space on a workstation. (You must become superuser to do this.) The mkfile command creates a file of a specific size, and swap -a adds the file to the system swap space:
2.6.3 Control of Virtual MemoryCompiling very large routines (thousands of lines of code in a single procedure) at -xO3 or higher can require a large amount of memory. In such cases, performance of the system might degrade. You can control this by limiting the amount of virtual memory available to a single process. To limit virtual memory in an sh shell, use the ulimit command. See the sh(1) man page for more information. The following example shows how to limit virtual memory to 16 Mbytes:
In a csh shell, use the limit command to limit virtual memory. See the csh(1) man page for more information. The next example also shows how to limit virtual memory to 16 Mbytes:
Each of these examples causes the optimizer to try to recover at 16 Mbytes of data space. The limit on virtual memory cannot be greater than the system’s total available swap space and, in practice, must be small enough to permit normal use of the system while a large compilation is in progress. Be sure that no compilation consumes more than half the swap space. With 32 Mbytes of swap space, use the following commands: In an sh shell:
In a csh shell:
The best setting depends on the degree of optimization requested and the amount of real memory and virtual memory available. 2.6.4 Memory RequirementsA workstation should have at least 64 megabytes of memory; 128 Mbytes are recommended. To determine the actual real memory, use the following command:
2.7 Simplifying CommandsYou can simplify complicated compiler commands by defining special shell aliases, using the CCFLAGS environment variable, or by using make. 2.7.1 Using Aliases Within the C ShellThe following example defines an alias for a command with frequently used options.
The next example uses the alias CCfx.
The command CCfx is now the same as:
2.7.2 Using CCFLAGS to Specify Compile OptionsYou 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):
The next example uses CCFLAGS explicitly.
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 -xO2 -xsb files... 2.7.3 Using makeThe make utility is a very powerful program development tool that you can easily use with all Sun compilers. See the make(1S) man page for additional information. 2.7.3.1 Using CCFLAGS Within makeWhen you are using the implicit compilation rules of the makefile (that is, there is no C++ compile line), the make program uses CCFLAGS automatically. 2.7.3.2 Adding a Suffix to Your MakefileYou can incorporate different file suffixes into C++ by adding them to your makefile. The following example adds .cpp 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.
2.7.3.3 Using make With Standard Library Header FilesThe standard library 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. Chapter 3 Using the C++ Compiler OptionsThis chapter explains how to use the command-line C++ compiler options and then summarizes their use by function. Detailed explanations of the options are provided in 16.8 Using dlopen to Access a C++ Library From a C Program. 3.1 SyntaxThe following table shows examples of typical option syntax formats that are used in this book. Table 3–1 Option Syntax Format Examples
Parentheses, braces, brackets, pipe characters, and ellipses are metacharacters used in the descriptions of the options and are not part of the options themselves. See the typographical conventions in “Before You Begin” at the front of this manual for a detailed explanation of the usage syntax. 3.2 General GuidelinesSome general guidelines for the C++ compiler options are:
Source files, object files, and libraries are compiled and linked in the order in which they appear on the command line. 3.3 Options Summarized by FunctionIn this section, the compiler options are grouped by function to provide a quick reference. For a detailed description of each option, refer to 16.8 Using dlopen to Access a C++ Library From a C Program. The options apply to all platforms except as noted; features that are unique to the Solaris OS on SPARC-based systems are identified as SPARC, and the features that are unique to the Solaris OS on x86-based systems are identified as x86. 3.3.1 Code Generation OptionsTable 3–2 Code Generation Options
3.3.2 Compile-Time Performance OptionsTable 3–3 Compile-Time Performance Options
3.3.3 Compile-Time and Link-Time OptionsThe following table lists the options that must be specified both at link-time and at compile-time. Table 3–4 Compile-Time and Link-Time Options
3.3.4 Debugging OptionsTable 3–5 Debugging Options
3.3.5 Floating-Point OptionsTable 3–6 Floating-Point Options
3.3.6 Language OptionsTable 3–7 Language Options
3.3.7 Library OptionsTable 3–8 Library Options
3.3.8 Licensing OptionsTable 3–9 Licensing Options
3.3.9 Obsolete OptionsNote – The following options are either currently obsolete and so no longer accepted by the compiler, or they will be removed in a future release. Table 3–10 Obsolete Options
3.3.10 Output OptionsTable 3–11 Output Options
3.3.11 Run-Time Performance OptionsTable 3–12 Run-Time Performance Options
3.3.12 Preprocessor OptionsTable 3–13 Preprocessor Options
3.3.13 Profiling OptionsTable 3–14 Profiling Options
3.3.14 Reference OptionsTable 3–15 Reference Options
3.3.15 Source OptionsTable 3–16 Source Options
3.3.16 Template OptionsTable 3–17 Template Options
3.3.17 Thread OptionsTable 3–18 Thread Options
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||