Compiling and Linking Code


For more information, check out the "Compile and Link" documentation on the UNIX at D0 page.



A Simple Example

If you just have a file that you want to link to the d0cernlib libraries, and/or other libraries, you can simply use the fort command to compile your Fortran (.f), or C (.c) program, and the lnk command to link it with the libraries you choose.
	% ls
	line.c

	% fort line.c
	cc -c -dollar -G 0 -woff 275 -DD0FLAVOR=SIUNIX -Wf,-XNl8192 
	-I/d0library/unix/source -I/d0library/c_inc -O2 
	/d0sgi0/usr0/perkins/www/test/line.c

	% ls
	line.c  line.o

	% lnk line.o -o line.out
	f77 -o line -Wl,-U line.o /d0library/general/general.a 
	/d0library/unix/unix.a /d0library/cernlib/packlib.a 
	/d0library/cernlib/mathlib.a /d0library/cernlib/kernlib.a -lcurses
	ld:
	The shared object /usr/lib/libftn.so did not resolve any symbols.
	        You may want to remove it from your link line.

	% ls
	line    line.c  line.o
The above example compiles and links the C program line.c to the d0cernlib libraries (the default) and creates the executable line.out. To make a debug version:
	% lnk -d line.o -o deb_line.out 
	f77 -o line -Wl,-U line.o -o deb_line.out 
	/d0library/general/deb_general.a /d0library/unix/deb_unix.a 
	/d0library/cernlib/packlib.a /d0library/cernlib/mathlib.a
	/d0library/cernlib/kernlib.a -lcurses

Using Makefiles

Using the make utility facilitates the building of executables that contain many source and library dependencies. Use make when you want to run code without a framework like
d0user. The following documentation will provide a simple example of how to use Makefiles to tell make how to compile and link code. For more detailed information on Makefiles, click here. You might also look at the man pages for make.

The example:

(1) We want to compile source and create a library in some directory,

		~usr/example/source

(2) We want to compile source and create another library in some other directory,
		~usr/example/util_source

(3) We have a root directory ( ~usr/example ) where we want to create
an executable linking these two libraries with some other source and some
cernlib libraries.


In this example, we need three Makefiles; one in each directory. The Makefiles in ~usr/example/source and ~usr/example/util_source will be similar. Here's what one would look like:
 	% cat example/source/Makefile

	#
	#  This make file will build the example library for general use
	#

	FFLAGS= -check_bounds		# Put fortran compile flags here.
					# For a list of commands, do a 'man f77'
	all: example.a		      # This is the library we want to build.

	lib_obj=src1.o src2.o src3.o src4.o \    # Here's the list of objects
	        src5.o src6.o 			 # to create if needed.

	example.a: $(lib_obj)		# Here's how to build the library.
	        rm -f example.a		# Commands are tab indented.
	        $(AR) cr example.a $(lib_obj)  

	src1.o: src1.f example.inc      # Here's how to compile the code.
	src2.o: src2.f 
	src3.o: src3.f junk.inc
	src4.o: src4.f example.inc
	src5.o: src5.f 
	src6.o: src6.f junk.inc


To execute the Makefiles, simply
	% make Makefile
~usr/example/source/Makefile creates the library example.a. Similarly, ~usr/example/source_util/Makefile might create the library example_util.a. In our top level directory, we want to link these libraries to our main source routine and create the executable text.x. Our ~usr/example/Makefile might look like:
        % cat example/Makefile

	#
	#  Build the test program
	#

	all: test.x
	
	libs = example/source/example.a /example/util_source/example_util.a
	
	master_libs =  $(d0cernlib)/mathlib.a $(d0cernlib)/kernlib.a

	objs = test.o testsrc.o

	test.x: $(libs) $(objs)
	        f77 -o test.x $(objs) $(libs) $(master_libs)

Tutorials | E-Mailing | Editing | Getting Help

D0 HOME | D0 At Work | UNIX at D0
Last updated 11 June 1997
J. Perkins