Compiling and Linking C Programs with Object Dependencies - spatial_geostan - 2024.00

Spatial+ Reference for Windows, UNIX/Linux, z/OS

Product type
Software
Portfolio
Locate
Product family
GeoStan Geocoding Suite
Product
GeoStan Geocoding Suite > Spatial+
Version
2024.00
Language
English
Product name
Spatial+
Title
Spatial+ Reference for Windows, UNIX/Linux, z/OS
Copyright
2024
First publish date
1994
Last updated
2024-05-07
Published on
2024-05-07T22:16:04.316305

The compilation and linking of C source code requires that many dependencies be resolved at compile time. Moreover, the C or C++ compiler attempts to resolve many of these dependencies for you based on default guesses. In this way the compiler limits the number of steps and parameters required of the user. However, in some special circumstances users may need to enter additional command line parameters in order for the compiler to create a valid system executable module.

The simplest form of compilation is:

$ cc -o hello hello.c

In this scenario, the compiler is being told to generate an executable module called "hello" based on the C source code found in the file hello.c. Assuming that there are no errors and hello.c is a valid C language program, the compiler does this in three steps.

First, the compiler translates the hello.c module into the native architecture's assembly language or object description language. Second, the compiler either calls an external assembler or utilizes a built-in assembler to translate the assembly language into architecture-dependent object code. Finally, the compiler calls the external link editor to build a loadable object module using the object code it generated. The compiler tells the link editor that this code depends on the standard C library, and that it requires the crt0.o object because it is a C program module. Thus, what appears to the user to be a single action actually involves multiple steps.

Here is the same compilation, this time separated into the multiple steps taking place behind the scenes:

$ cc -S hello.c # compilation
$ as -o hello.o hello.s # assembly
$ ld -o hello hello.o crt0.o -lc # link editing

As you can see, several steps are intentionally hidden from the user. Unfortunately, your software may have additional dependencies that cannot be resolved by the default process selected by your compiler.

If your software depends on an external library of any kind, it must be specified at link time. External libraries may be specified in one of two ways.

  • Use the -l compiler, or link editor, directive. When the -lname command line parameter is passed to the compiler or link editor, the link editor searches the default library path for libraries named libname.a or libname.so/libname.sl. The standard C library is specified as -lc, the math library as -lm, and the C++ class library is usually specified as -lC. Note that these names may vary depending on your C++ compiler option.
  • Directly specify the path and filename of the library. The compiler or link editor recognizes a library file when you specify it on the compiler command line. For example, if you compile your code with the Centrus GeoStan™ library, you can pass libgeostan.a as a command line parameter to your compiler:
$ cc -o gstest gstest.c libgeostan.a

As with any robust system, there are many ways to compile and link a program in the UNIX environment. Here is how to compile and link the command line Geocoder program in the UNIX environment.

$ cc -c -O2 -DUNIX=1 -I/net/centrus/geostan/include geocoder.c
$ ld -B static -o geocoder /usr/lib/crt0.o geocoder.o
-L/net/centrus/geostan/lib -lgeostan
-B dynamic -lm
-lc -lC

(Some system specific parameters have been removed for readability, but this is the most general case.)