Software Tools

Preprocessor

Preprocessor Directives

Symbolic constants

	#define PI 3.14159		// no ;

Macros

	#define CIRCLE_AREA(x) ( PI * (x) * (x) )

	The statement 
		area = CIRCLE_AREA(4);
	will be expanded to
		area = ( 3.14159 * (4) * (4) );

Conditional Compilation

	#ifdef DEBUG
		cout << "Test version. Debugging is on.\n";
	#else /* DEBUG */
		cout << "Production version.\n";
	#endif /* DEBUG */

	#define DEBUG	/* Turn debugging on */
	#undef DEBUG	/* Turn debugging off */
	cc -DDEBUG -g -o prog prog.cc

Avoid Multiple Inclusion

	// bool.h
	#ifndef BOOL_H
	#define BOOL_H
	typedef int Boolean;
	const Boolean TRUE = 1;
	const Boolean FALSE = 0;
	#endif

    More information about Preprocessor

Compiling and Linking with GCC

Reference

Compiling

g++ -g -Wall -o prog test.cc The options are: -g: Instructs g++ to create debugging info for gdb -Wall: Give all possible compiler warnings -o: Write the output (compiled program) to the named file, in this case "prog"

Separate Compiling

	g++ -g -Wall -c array2d.cc 	// compile only

Linking, and Compile-Linking

		g++ -g -Wall -o life life.cc array2d.o -lm

	is equivalent to the lines

		g++ -g -Wall -c life.cc
		g++ -g -Wall -o life life.o array2d.o -lm

					// -lm will link libm

Libraries

	

	ar t lib/libc.a         // display titles of files in the library 

	ar ruv g_lib.a g_fopen.o ...   	// create a lib from *.o files 
					// replace, update, verbose
	ranlib g_lib.a          // randomize the lib in a form
				//      useful for the linker

Non-Standard libraries

	 -I: Specifies a directory to search for include files
	 -L: Specifies a directory to search for the code for
			the library. 

	  g++ -g -Wall -I~35500/include myprog.cc -L~35500/lib -lmine

	which would look for the library "libmine.a" or "libmine.sl"

	environment variables (specified in .cshrc)

	setenv CPLUS_INCLUDE_PATH ~/include
	setenv LIBRARY_PATH ~/lib
	setenv LD_LIBRARY_PATH ~/lib

	echo $CPLUS_INCLUDE_PATH

Makefile

	touch filename

	Here's a very simple makefile:

	main:
		touch main
	\______/          /\
	 <TAB>           <RET>

	-----------------------
	main: helper
		touch main

	helper:
		touch helper
	-----------------------

	<target>: <dependent 1> <dependent 2> ....
		<command>
		<command>
		 

	-----------------------
	main: helper
		touch main

	helper:
		touch helper

	clean:
		rm main helper
	-----------------------

	  make
	  touch clean
	  make clean

Compiling and Linking of a Multifile program

Makefile:

all:	datetype.o birthcal.o
	gxx -o birthcal birthcal.o datetype.o

datetype.o:	datetype.h datetype.cpp
	gcc -c datetype.cpp

birthcal.o:	datetype.h birthcal.cpp
	gcc -c birthcal.cpp
	
Basic Compilation Control with Gmake (GNU make)

Debugger

reference

Commands:

	Example: gxx -g -o count count.cc

	>gdb count
	(gdb) break main
	(gdb) run
	(gdb) next
	(gdb) next
	(gdb) print seven_count
	(gdb) next
	(gdb) print seven_count
	(gdb) break get_data
	(gdb) info breakpoints
	(gdb) delete 1
	(gdb) run
	(gdb) print seven_count
	(gdb) next
	(gdb) print seven_count
	(gdb) next
	(gdb) print seven_count
	(gdb) list 22

runtime errors

  1. segmentation violation
  2. stack overflow
  3. divided by 0

Profiling

	gxx -p -o pgm pgm.cc
	prof pgm