sun.com docs.sun.com My Sun Worldwide Sites
Sun Microsystems Technical Article

Getting Started with Sun Studio Software

by Jyothi Srinath, Sun Microsystems, January 2008

This article is intended for developers and users new to Sun Studio software. It provides an introduction to Sun Studio software and its various features with links to the corresponding user guides and programming guides.

Introduction

Sun Studio software is the development environment for the SolarisTM Operating System. It is supported on both SPARC and x86 platforms. It has C, C++ and Fortran compilers along with a NetBeans-based IDE. It includes tools for multithreaded programming, debugging, programming for high-performance applications and more. Sun Studio software is also supported on the Linux OS.

Sun Studio 12 software is bundled with the latest Solaris Express Developer Edition SXDE 9/07. You can download SXDE 9/07 free of cost from http://developers.sun.com/sxde/download.jsp.

Sun Studio software is supported on systems that use the SPARC® and x86 families of processor architectures: UltraSPARC® , SPARC64, AMD64, and Pentium. Refer to the System Requirements for a complete list of supported platforms.

Installation and Uninstallation of Sun Studio 12 Software

This section describes how to install Sun Studio software on a single-user system using the batch installer and the command line installer. To use the graphical installer and for instructions on how to install on other systems including servers running the Solaris 10 Operating System with zones, please refer to the Sun Studio software installation manuals: Quick Installation and the Installation Reference Guide.

Installing Sun Studio Software Using The Batch Installer

The simplest way to install the software on a single-user system is to use the batch installer. Note that the batch installer installs all components of the Sun Studio 12 software. If you already have an earlier version of Sun Studio software installed on your system, you cannot install the newer version in the same directory.

  1. Switch to superuser mode on your system and enter the root password.

    % su
  2. Run the command to view the software license agreement.

    % ./batch_installer --show-sla
  3. Perform one of the following steps :

    • Run the system preparation tool once to check your system for the prerequisite software and again, if necessary, to install any missing software. Then run the batch installer to accept the software license agreement and install the Sun Studio 12 software.

      % ./prepare_system -C
      % ./prepare_system -s  missing_software,missing_software, ...
      % ./batch_installer --accept-sla

      To install the Sun Studio 12 software in a directory other than /opt, specify the installation directory with the -d option.

    • Run the batch installer to check for the required prerequisite software and install any missing software, accept the software license agreement, and install the Sun Studio 12 software.

      % ./batch_installer -p all --accept-sla
  4. Type the following to determine whether you need to set up your access to the Sun Studio 12 software tools and man pages:

    % /usr/bin/version

    If you receive the message /usr/bin/version:not found or the command reports an earlier version of Sun Studio software, see Setting Up Access to the Developer Tools and Man Pages in the Quick Installation Guide for information on setting your PATH and MANPATH to point to the Sun Studio 12 software.

    If the command reports Sun Studio 12 software, you do not need to set your PATH and MANPATH.

Installing Sun Studio Software Using The Command Line Installer

  1. Switch to superuser mode on your system and enter the root password.

    % su
  2. In the directory that contains the installer, start the installer by typing

    % ./installer -nodisplay
  3. Read the Software License Agreement and accept it by typing yes.

  4. Deselect any software component or subcomponent you do not want to install.

  5. Select the Chinese locale or the Japanese locale if you want to install the localized Sun Studio 12 software in either of these languages.

  6. If the installer reports that any prerequisite software is missing, install the missing software by choosing Update All Prerequisites.

  7. If you have previously installed some components of the Sun Studio 12 software, the installer will install additional components in the same directory. If you have not previously installed any components of the software, and you want to install the Sun Studio 12 software in a directory other than /opt, specify the installation directory.

  8. Select Skip advanced options.

  9. Follow the installer instructions to complete the installation.

  10. If you installed the Sun Studio 12 software on a single-user system, type the following to determine whether you need to set up your access to the Sun Studio 12 software tools and man pages:

    % /usr/bin/version

    If you receive the message /usr/bin/version:not found or the command reports an earlier version of Sun Studio software, see Setting Up Access to the Developer Tools and Man Pages in the Quick Installation Guide for information on setting your PATH and MANPATH to point to the Sun Studio 12 software.

    If the command reports Sun Studio 12 software, you do not need to set your PATH and MANPATH.

Uninstalling Sun Studio Software With The Command Line Uninstaller

This section describes how to uninstall the Sun Studio software using the command line installer. For instructions on how to uninstall using the graphical uninstaller or to install using a remote display, refer to the Quick Installation Guide.

  1. Switch to superuser mode on your system and enter the root password.

  2. Go to the bin directory in the installation directory. The default bin directory is /opt/SUNWspro/bin on Solaris platforms and /opt/sun/sunstudio12/bin on Linux platforms.

  3. Start the interactive command-line uninstaller by typing

    % ./uninstaller -nodisplay
  4. To uninstall the software silently, type

    % ./uninstaller -nodisplay -noconsole
  5. Select Full to uninstall all components of the software, or Partial to uninstall only selected components.

  6. Follow the uninstaller instructions to complete the uninstallation

Features of Sun Studio 12 Software

This section summarizes the many features of Sun Studio 12 software.

C, C++, Fortran Compilers

C Compiler

The C compilation system consists of a compiler, an assembler, and a link editor. The cc command invokes each of these components automatically unless you use command-line options to specify otherwise. The Sun Studio 12: C User’s Guide provides complete information about the C compiler.

Invoking the C Compiler

The simplest way to invoke the C compiler is

% cc test.c

This outputs a default a.out executable file. To specify the name of the executable file, type

% cc -o test test.c

Source files, object files, and libraries are compiled and linked in the order in which they appear on the command line.

Using Lint (Source Code Checker)

The lint program checks your C code for errors that may cause a compilation failure or unexpected results at runtime. In many cases, lint also warns you about incorrect, error-prone, or nonstandard code that the compiler does not necessarily flag.

The lint program operates in two modes:

  • Basic, which is the default

  • Enhanced, which includes everything done by basic lint, as well as additional detailed analysis of code

The syntax for the lint command is :

lint file1.c file2.c

lint -Nlevel=3 file1.c file2.c

The -Nlevel options can be between 1 and 4 where option 1 analyzes only single procedures and levels 2, 3 and 4 analyze the whole program with varying levels of complexity.

New Compiler Options for the x86 Platform
-xarch option

The following are new x86 flags for the -xarch option:

  • -xarch=sse3 specifies that the compiler should generate instructions based on both the SSE3 and SSE2 architectures.

  • -xarch=sse3a specifies that the compiler should generate instructions based on the SSE3, SSE2 and AMD extended architectures as well as the 3DNow extensions.

-fast option

The -fast option on x86 now includes -xregs=frameptr, which means that the compiler can use the frameptr register (%ebp on IA32, %rbp on AMD64) as a general purpose register to generate code for all the compilation units. Consequently frame pointers will not be generated in each function or routine. Specify -xregs=no%frameptr after -fast in the compilation command and the frameptr register will not be used as a general purpose register. The following example demonstrates how to override the -fast default for -xregs:

% cc -fast -xregs=no%frameptr foo.c

For an example of using the -fast option, read the article Performance Tuning with Sun Studio Compilers and Inline Assembly Code by Timothy Jacobson.

-xopenmp

To recognize OpenMP directives, use the compiler option -xopenmp. Note that this option also raises the optimization level to -xO3. To recognize OpenMP directives without increasing the optimization level use the option -xopenmp=opt.

-Z11 and lock_lint utility

The lock_lint utility analyzes the use of mutex and multiple readers/single writer locks, and reports on inconsistent use of these locking techniques that may lead to data races and deadlocks in multithreaded applications. This tool is now ported to the Solaris OS on the x86 platform and the Linux OS. Use the -Zll flag with the C compiler to create the lock_lint database files. No C object files are output when this flag is used, but a .ll file is generated for each C source file compiled with this flag. The .ll files are used by the lock_lint utility.

-m32 | -m64

Use these options to specify the memory model. For Solaris on x86 systems, the -m32 is the default option. For Linux systems supporting 64-bit platforms, -m64 -xarch=sse2 is the default option.

C++ Compiler

The C++ compiler package consists of a front end, optimizer, code generator, assembler, template pre-linker, and link editor. The CC command invokes each of these components automatically unless you use command-line options to specify otherwise. The Sun Studio 12: C++ User’s Guide contains complete information about the C++ compiler.

Invoking the C++ Compiler

The simplest command to invoke the C++ compiler is:

% CC test.cc

(Note the uppercase CC as compared to lowercase cc for the C compiler.)

This outputs a default a.out executable file. To specify the name of the executable file, type

% CC -o test test.cc

Source files, object files, and libraries are compiled and linked in the order in which they appear on the command line.

Examples
  • Compiling and linking multiple source files

    % CC test1.cc test2.cc test3.cc -o test

    The compiler automatically generates the object files and links them together to output the final executable test.

  • Compiling and linking in separate steps

    % CC -c test1.cc
    % CC -c test2.cc
    % CC -c test3.cc
    % CC -o test test1.o test2.o test3.o
  • To display additional information while compiling the program, type

    % CC -verbose test1.cc
Setting Up Aliases and Compile Options Inside a C Shell

If you use some options frequently with your C++ compiler, it is useful to set up an alias for the command so that it can be invoked faster. For example, if you use the -fast option frequently, you can set up the alias as follows:

% alias CCfx 'CC -fast'

To compile a program with the -fast options, now type

% CCfx test1.cc

This is the same as

% CC -fast test1.cc

You can also use the CCFLAGS variable to set options that you use together:

% setenv CCFLAGS `-features=extensions`

(Note the “back quotes” used here. The -features=extensions option is used when you have to compile invalid code that you cannot alter).

When you want to use this combination of options, type

% CC $CCFLAGS test1.cc
Fortran Compiler

The Sun Studio Fortran 95 compiler provides the following features and extensions:

  • Global program checking across routines for consistency of arguments, commons, parameters, and the like.

  • Optimized automatic and explicit loop parallelization fo multiprocessor systems.

  • VAX/VMS Fortran extensions, including:

    • Structures, records, unions, maps

    • Recursion

  • OpenMP 2.5 parallelization directives.

  • Global, peephole, and potential parallelization optimizations produce high performance applications. Benchmarks show that optimized applications can run significantly faster when compared to non-optimized code.

  • Common calling conventions permit routines written in C or C++ to be combined with Fortran programs.

  • Support for 64-bit enabled Solaris environments on UltraSPARC and AMD64 platforms.

  • Call-by-value using %VAL.

  • Compatibility between Fortran 77 and Fortran 95 programs and object binaries.

  • Interval Arithmetic programming.

  • Some Fortran 2003 features, including Stream I/O.

The Sun Studio Performance Analyzer can be used with the Fortran 95 compiler for analysis of single-threaded and multithreaded applications.

The dbx debugger and Sun Performance Library also work with the Fortran compiler.

Runtime Checking

The Sun Studio Fortran compiler provides several command line options to enable runtime checking. Run time checking (RTC) is an integral debugging feature that lets you automatically detect runtime errors, such as memory access errors and memory leak, in a native code application during the development phase. It also lets you monitor memory usage. You cannot use runtime checking on Java code.

The following runtime checking options for the Fortran compiler are listed by the f95 man page:

-C

enables array bounds and array syntax conformance checking

-xcheck=init_local

This option is used for debugging purposes. It helps the user to detect if a local variable is used before it is set.

-xcheck=stkovf

is currently available only on SPARC platforms and is used to check stack overflow.

-xassume_control=[check[,fatal]]

This option causes the compiler to check the value of ASSUME pragmas, and generate a message when the value is not true.

-xcommonchk

This option detects when a common block is defined THREADPRIVATE in some but not all modules and should be used for debugging purposes only as it can affect performance.

-fpover

This option enables a program to handle floating-point overflow in formatted input.

Invoking the Fortran Compiler

To compile multiple source files and enable runtime debugging , type

% f95 -o test test1.f test2.f

The compiler automatically generates the object files and then produces the executable test.

To perform compiling and linking in separate steps, type

% f95 -c test1.f 
% f95 -o test test1.o test2.o

In this case, only test1.f is recompiled and then linked with the previously compiled test2.o to produce the executable. Note that consistency should be maintained with options when compiling and linking are performed in different steps. Also some compilation options require that they be used consistently across all source files.

The Sun Studio 12: Fortran User’s Guide provides complete information about the Fortran compiler.

Understanding and Using the Compiler Options

There are a number of options for use with the Sun Studio C, C++, and Fortran compilation directives. Refer to the Sun Studio Compiler Options table for a complete list of all the available options. The options are grouped by language, tasks, and platforms.

OpenMP Support

Sun Studio software is customized to support OpenMPTM API. The OpenMP Application Program Interface is a portable, parallel programming model for shared memory multiprocessor architectures. Use the -xopenmp flag with the cc, CC, or f95 compiler to enable explicit parallelization with OpenMP directives. The Sun Studio 12: OpenMP API User’s Guide describes using the OpenMP API with the Sun Studio software.

dbx Debugger

dbx is an interactive, source-level, command-line debugging tool. You can use it to run a program in a controlled manner and to inspect the state of a stopped program. dbx gives you complete control of the dynamic execution of a program, including collecting performance and memory usage data, monitoring memory access, and detecting memory leaks. Runtime checking (RTC), as mentioned earlier, is an integral debugging feature lets you automatically detect runtime errors, such as memory access errors and memory leak, in a native code application during the development phase. It also lets your monitor memory usage. You cannot use runtime checking on Java code.

  • Works on Solaris/x86 as well as Linux/86 in addition to Solaris/SPARC

  • Detects memory access errors

  • Detects memory leaks

  • Collects data on memory use

  • Works with all languages

  • Works with multithreaded code

  • Requires no recompiling, relinking, or makefile changes

  • Integral debugging features, so can be mixed with all other debugging operations

Basic dbx usage
  • Start the debugger.

    % dbx
  • Start the debugger and load the program to be debugged.

    % dbx program_name
  • Start the debugger and attach it to a process that is already running.

    % dbx -process_id
  • Run the most recently loaded program in dbx

    % run

Chapter 1, Getting Started With dbx, in Sun Studio 12: Debugging a Program With dbx provides a high-level overview of dbx and its functionality.

Detailed and functional description of all dbx commands is available in Appendix C, Command Reference, in Sun Studio 12: Debugging a Program With dbx.

dmake

Distributed Make (dmake) is a command-line tool compatible with make. It parses your makefiles and determines which target can be built concurrently, and distributes the build of those targets over a number of hosts set by you. dmake is available on the following platforms:

  • Solaris Operating System (Solaris OS), 9 and 10 1/06

  • SuSE Linux Enterprise Server 9 with Service pack 3

  • RedHat Enterprise Linux 4

See the Sun Studio 12: Distributed Make (dmake) manual for more information.

Thread Analyzer

The Thread Analyzer is a tool that you can use to analyze the execution of a multithreaded program. The Thread Analyzer works on the Solaris OS on SPARC and x86 platforms, and on the Linux OS. The Thread Analyzer has the following features:

  • Detects data races and deadlocks in a multithreaded application.

  • Points to non-deterministic or incorrect execution

  • Points out actual and potential deadlock situations

  • Works with OpenMP, Pthreads, Solaris Threads (parallel programming methods)

  • Provides API for user-defined synchronization primitives

The thread analyzer detects two common threading issues in a multithreaded program - data race and deadlock. A data race can occur when two threads (in a multithreaded program) access the same (=shared) memory location concurrently and without holding any common exclusive locks and at least one of the accesses is a write/store.

Deadlock can occur when two or more competing actions are waiting for the other to finish and neither is able to complete the task. Deadlocks may be caused by misuse of synchronizations or communications, such as barriers.

The following new compiler option causes the compiler to instrument your multithreaded application for analysis by the new Thread Analyzer.

-xinstrument[=[no%]datarace]

The default is -xinstrument=no%datarace.

Using the Thread Analyzer
  1. Instrument the code

    % cc -xinstrument=datarace source.c
  2. Run the resulting executable under the collect command. At runtime, memory accesses and thread synchronizations will be monitored. Any data races found will be recorded into a log file.

    % collect -r [ race | deadlock ] a.out
  3. Display the results

    % er_print [-race | -deadlock] tha.1.er

    (Command-line utility to display Analyzer results)

    % tha tha.1.er

    (Customized Analyzer GUI)

More information about Thread Analyzer can be found in the Thread Analyzer README.

The Sun Studio 12: Thread Analyzer User’s Guide contains complete information about the Thread Analyzer.

Tools of Performance Analysis: Collector and Performance Analyzer

Collector and Performance Analyzer are used to collect and analyze performance data for your application. These tools are used by developers for performance tuning of their applications. The tools help the developers answer specific questions with regards to their application such as:

  • How much of the available resources does the program consume?

  • Which functions or load objects are consuming the most resources?

  • Which source lines and instructions are responsible for resource consumption?

  • How did the program arrive at this point in the execution?

  • Which resources are being consumed by a function or load object?

The Collector tool collects performance data using a statistical method called profiling and by tracing function calls. The Performance Analyzer tool displays the data recorded by the Collector. The Analyzer processes the data and displays various metrics of performance at the level of the program, the functions, the source lines, and the instructions.

The Sun Studio 12: Performance Analyzer manual describes how to use the Performance Analyzer.

Sun Performance Libraries

Sun Performance Library is a set of optimized, high-speed mathematical subroutines for solving linear algebra and other numerically intensive problems. Sun Performance Library contains enhanced versions of the standard libraries available at NetLib.

Sun Performance Library routines can increase application performance on both serial and multiprocessor (MP) platforms and contains routines that have SPARC and AMD specific optimizations.

The Sun Performance Library routines are used to solve various types of linear algebra and numerical problems. Some examples are Elementary vector and matrix operations , Linear systems, Least squares Eigen problems, Matrix factorizations or decompositions etc.

Sun Performance Library provides a Fortran module for use with Fortran 95 programs. To use this module, include the following line in Fortran 95 codes:

USE SUNPERF

If a routine in your program makes a Performance Library call, use the following guidelines to compile and link your program:

  • Use -dalign on all files at compile time or, on SPARC platforms, enable trap 6

  • Use the same command line options for compiling and linking: -dalign -xlic_lib=sunperf for Fortran and C and -dalign -library=sunperf for C++.

Read the Sun Studio 12: Sun Performance Library User’s Guide for details on how to use the Sun Performance Library.

HPC

High Performance and Technical Computing (HPTC) applies numerical computation techniques to highly complex scientific and engineering problems. Sun Studio compilers and tools provide a seamless, integrated environment from desktop to TeraFLOPS for both floating point and data intensive computing.

See the article Commands for Sun Studio 12 Compilers and Tools for a list of Sun Studio commands organized by functionality. Also see this Sun Studio: High Performance and Technical Computing page for information relevant to developing HPTC applications on Sun platforms.

IDE (Integrated Development Environment)

This short tutorial will guide you through setting up a project which will help you understand how to create, build, and run applications in the Sun Studio IDE.

Setting up a Project

To create a Sun Studio project from existing code

  1. Start the Sun Studio IDE. If you are using the latest SXDE, the Sun Studio IDE is found under All Applications ⇒ Developer Tools. You can also type the following at the command line in a terminal window to bring up the application.

    % sunstudio &
  2. Create a new project. Choose File ⇒ New project. In this example, we will select the “C/C++/Fortran Project from Existing Code”. Click Next.

    Create a project window
  3. Browse to choose an existing makefile or choose a makefile generated by a configure script.We will choose an existing makefile. Click Next.

    Choose a makefile
  4. Specify the working directory and build commands. Click Next.

    Specify build commands
  5. Specify the folders that contain the source files that need to be added to the project. Click Next.

    Specify source file folders
  6. Select the configuration mode for your project. If you select Manual Configuration, you will need to specify the Include Directories and the Macro Definitions. We will select Automatic Configuration.

    Select the configuration mode
  7. Specify the project name (helloapp_2 in this example) and location and click Finish.

    Specify the project name

The project is created. Choose Build ⇒ Build Main Project to compile the project. Once the project is successfully compiled, choose Run ⇒ Run Main Project to execute the project.

Accessing Sun Studio Documentation

Once you have downloaded and installed the Sun Studio 12 software, you will find the documentation index at file:/opt/SUNWspro/docs/index.html on Solaris platforms and at file:/opt/sun/sunstudio12/docs/index.html on Linux platforms. If you installed the Sun Studio software in a directory other than /opt, the documentation index is at /installation_directory/SUNWspro/docs/index.html or /installation_directory/sunstudio12/docs/index.html.

You can also access the Sun Studio Information Center on docs.sun.com.

Company Info Contact Terms of Use Privacy Copyright 1994-2008 Sun Microsystems, Inc.