ITS provides all of the industry-standard programming tools and facilities necessary to write programming projects in C or C++. While the ITS customer support center supports the usage and basic functionality of all the programming tools described in this document, ITS does not support any programming syntax in any programming language, including C, C++, or Makefile syntax.
C/C++ on Windows
C and C++ along with J++, Visual Basic and some extra tools are made available by Microsoft Visual Studio in the SAL computer lab. If you have the choice between programming on Visual Studio and programming on UNIX, Visual Studio is often easier to learn. Visual Studio has a help feature accessible from the Help menu near the upper right corner of the program window.
C/C++ on Unix
There are two different sets of compilers on the Solaris systems at USC. The first are the cc/CC sparcompilers developed by SUN. The second are the gcc/g++ GNU compilers developed by the GNU Project.
Setting up a Compiler
The SUN compilers can be found in /usr/spac/sparcompilers/. There are several different versions in that directory. It is advised to use the default version. To use the sparcompilers package, you will need to set up your Solaris environment by typing
This command will allow you to use these compilers with the cc (C) and CC (C++) commands. It also sets up your MANPATH variable such that you will be able to type man cc and man CC for more information on this compiler set.
The GNU compilers can be found in /usr/usc/gnu/gcc. There are several different versions, but it is advised that you use the default version. To use the gcc package, you will need to set up your environment by typing
This command will allow you to use these compilers with the gcc (C) and g++ (C++) commands. It also sets up your MANPATH variable such that you will be able to type man gcc and man g++ for more information on this compiler set.
NOTE: To avoid typing the source line every time you want to use a particular compiler, you can add it to a new line at the end of your ~/.login file.
When creating a software project in C or C++, the compiler you choose is up to you. However, when compiling the program, it is important to compile the entire project using only one set of compilers. If writing a program for a class, you may want to use the same compiler that the project will be graded using.
Using the Compiler
The first step is to write a program using a text editor. To get you started, a sample C program is given at the bottom of this page. Next, let’s assume that you are going to be using gcc to compile this program (since it is a C program you could use cc to compile it instead) and that you named the program myProgram.c. The simplest way to compile this program would be to type
This command creates a file called a.out, which is the executable version of this program. To name the program something else (and to keep from overwriting other a.out files), use the -o flag as in:
gcc -o myProgram myProgram.c
This will create an executable called myProgram. This program can be executed by typing ./myProgram.
Compilers generally use a plethora of extra options for optimizing the speed of the program, allowing sloppy programming code, compiling a program of multiple source files, and many other options. To learn more about these options, you can read them in the manpage by typing man then the name of the compiler. For instance, to learn about gcc options, you would type man gcc.
Large Projects and Makefiles
Using the compiler commands to compile a program is useful if you are compiling one source file into a small program, but if you are building a larger piece of software involving multiple source files, multiple executables and/or shared object files, you are going to want to use a Makefile.
A Makefile is a file read by the program make (or the GNU version gmake), which basically scripts the compilation of multiple files. While the differences between cc and gcc are subtle, gmake supports a larger set of Makefile syntax than make, and is therefore often considered to be definitively better. Some Makefiles require gmake and will not compile under make. Learning to write a Makefile can be similar to learning to program, but documentation is available from GNU.
Unix has a lot of shared libraries (pre-compiled code that programs can link into as if it were source code in the project). The standard place to find libraries on a unix system is /usr/lib. At USC /lib and /usr/lib are the same directory. However, there are additional libraries spread throughout /usr/usc/, such as /usr/usc/jpeg/default/lib.
To call a library, it must first be in your library path. Standard libraries needed by the compiler and used commonly in programs, such as the math libraries are already in the path. The library path is built in three steps. First, the compiler reads your $LD_LIBRARY_PATH shell variable. Then it adds its own libraries (for instance /usr/usc/gnu/gcc/default/lib). Finally it adds paths specified by the -L flag.
Once the library is in your library path, you call the library with the -l flag. If you used math functions such as sin and cos in your program, it may require the math library. You can include the math library with -lm. If you wanted to include your own library called myLib that you put in ~/lib, your command line might look something like
gcc -o progWithLibs -L$HOME/lib -lmyLib progWithLibs.c
It may be necessary to use $HOME rather than ~ because the compiler expects an absolute path (a path that starts with the system root /). There must not be any space between -L or -l and its argument.
Adding a directory to your LD_LIBRARY_PATH has a similar effect for compiling, but it also is included as a search path for every program that is compiled or is executed, regardless of whether it needs that library. Because of this, it is not a good idea to create a massive LD_LIBRARY_PATH. Proper use of -L flags, and linking tools like ld and libtool can make the use of LD_LIBRARY_PATH unnecessary.
It is suggested that for every -L flag you use, you use an identical -R flag. the -R flag sets the RPATH, which is an LD_LIBRARY_PATH built directly into the program itself. If you do not use -R flags, you may need to set a permanent LD_LIBRARY_PATH.
Other UNIX C/C++ Tools
If you are writing your own C/C++ program on UNIX, you can benefit from some additional tools. First, you will need a text editor. If your compiled program crashes, you may want a debugger, such as gdb or ddd. To avoid hidden problems such as memory leaks, you may want to use purify. For a quick answer to where your program is crashing, you may want to use a stack tracer.
The three main debuggers on Solaris are GNU gdb and ddd, and SUN’s adb. It is recommended (although not totally necessary) that you debug cc/CC programs in adb, and gcc/g++ programs in gdb or ddd. adb and gdb are text based debuggers. ddd is a window based debugger, which can be easier to learn. Please refer the the man pages of the debugger you wish to use.
Purify is a professional programming tool that works with a compiler to add extra debugging information into any program. Because ITS has a limited number of licenses for purify, it is available by request only. Information on this program can be found in the text file /usr/usc/purify/default/README.USC. Requests for a purify license may be sent to firstname.lastname@example.org
Stack tracing is a very quick, but very convoluted method of getting to the bottom of a crash or early exit. A stack trace shows information about every function that was called, including some system-level functions. Some stack tracing programs are truss, sotruss, and apptrace. Please refer to the manpages of each of these commands for more information.
Here is a very simple sample program for use in learning how to use a compiler. You write this into a text file, or you can download the code for it here .
/* Sample Program
* filename: myProgram.c
* executable: myProgram
printf("%s%s","This is a program that I created.\n",
"It is called myProgram.\n");
/* End of Program */
There is ample help available for programming in text form. There are manpages for every command listed in this document. Type man man for more information on viewing manpages. There are many programming resources available on the Internet, along with resources for UNIX programming tools. There are also many newsgroups and mailing lists available to answer questions. The ITS only supports programming in the capacity of:
- Providing the tools necessary to create and compile programs
- Accepting reports that these tools are not functioning properly
- Providing help with starting and basic operation of these tools
ITS does not provide any help in programming syntax or program debugging. If you are programming for a class, please contact your TA or professor for help with programming. If you need help in starting a programming tool, contact the ITS Customer Support Center.