There are a handful of environment variables that you can set that will have an affect on your ability to compile software. These are:


For compiling C code:


CC, CFLAGS


For compiling C++ code:


CXX, CPPFLAGS


For the linking stage:


LDFLAGS


For compiling any code:


TMPDIR


After the code is compiled:


LD_LIBRARY_PATH


export CC CFLAGS CXX CPPFLAGS TMPDIR LDFLAGS LD_LIBRARY_PATH


You don't have to set environment variables for GCC to use them. You could simply add them to the configure command line as shown below:


./configure CC=gcc CFLAGS=-O3 LIBS=-lposix


There is one more option to passing variables to gcc and that's to use the variable CONFIG_SITE. We'll going into some depth on CONFIG_SITE when we compile gcc.


Let's take a look at what each of these do:


CC=gcc, tells configure that your C Compiler (CC) is gcc.


CFLAGS=-O3, tells configure that you want the compiler to use the following optimization flag. By using optimization flags you can adjust how the code is compiled. For instance, -O3 tells the compiler to optimize the code for speed. You should check the GCC manual for a listing of all of the different flags that you can set to see if there are any that you think you should use.


If you are currently running SPARC hardware it is strongly suggested that you set at least -mv8 so GCC can use some hardware registers that it otherwise would not be able to use. In addition to compiler optimizations you can also add include directories here. The terms include and header files are used interchangeably.


If the compiler cannot find a filename.h file, you can tell it to look in the directories listed here to help the compiler find the files it needs to compile the program. An example of this would be:


CFLAGS=-I/usr/local/include


If the program that you're trying to compile needs to access the network then be sure to include the following libraries: socket and nsl. In the makefile these should show up as -lsocket -lnsl.


CXX=g++, tells configure that your C++ compiler is named g++, the GNU equivalent of c++.


CPPFLAGS=-I/usr/local/include, same as CFLAGS listed above, except for use with C++.


LDFLAGS="-L/usr/sfw/lib -R/usr/sfw/lib"


Linking is the final stage of compiling. One of the things that you need to be concerned with is where do the programs libraries go. Libraries can be thought of as binaries as the system does not know where they are unless you specifically tell it. When you go to run a program you will sometimes run into a situation where you need to set the LD_LIBRARY_PATH environment variable.


On Solaris 8 and above you can get around this by using the crle command instead. Or you can fix the issue right here so you don't have to do either. The LD_LIBRARY_PATH variable is to libraries what PATH is to executables. When you run a program that is dynamically linked it will search LD_LIBRARY_PATH directories to find the libraries that it needs to run. If the program cannot find all of it's libraries it will not run. Just like PATH, LD_LIBRARY_PATH is a colon-delimited list of directories.


There are two parts to LDFLAGS. The first part is used for compiling, that's the -L part. The other part is used at runtime, that's the -R part. When you're telling GCC to look into this directory to find the libraries it needs to compile the program you set the -L half of LDFLAGS. When you're telling GCC to embed the path to the library into the executable so it will know where to find them when it goes to run the program you use the -R part. Properly setting -R should remove the need to use LD_LIBRARY_PATH or crle. The linker normally only includes the name of the library and not the path to the library. When you use -R you are telling the linker to add the full path as well as the name of the library to the executable.


There are two types of library files. Static library files end in .a. Dynamic libraries are also called shared libraries and the files end in .so. To see which libraries an executable program uses you use the ldd command.


ldd binary_program_name, this command will send an output to your screen with all the library files that the program needs to run. If it can find the library it will display the full path to and the name of the library. If it cannot find the library file it will report "(not found)". Unless the program can find all of its library files it will not run. There will be a discussion on static versus dynamic libraries later on.


TMPDIR=/tmp


If TMPDIR is set it specifies the directory to use for temporary files. GCC uses temporary files to hold the output of one stage of compilation which is to be used as input to the next stage. For example the output of the pre-processor which is the input to the compiler proper.


Those are the main environment variables that you need to be concerned with.


Be careful what you include in an environment variable. When you run configure, configure will attempt to compile a small program to ensure that your compiler works. When it compiles this small program it includes all of the environment variables that you have set. If a variable is improperly set then the test program will fail to compile and configure will report your compiler as unusable. An easy fix is to just unset your environment variables or set them again using different values.


Somewhere along the line someone started to work on a utility called pkg-config.


This is an amazing little utility as it allows for the creation of a text file that holds information pertaining to the utility that you just installed.


If the utility that you just installed creates a .pc file as shown below:


/bin/bash ./mkinstalldirs /opt/local/lib/pkgconfig
./install-sh -c -m 644 atk.pc /opt/local/lib/pkgconfig/atk.pc


then the .pc file contains information about the utility that you just installed.


It contains the name, version information, base directory, library and include directory locations, and dependencies as shown below:


prefix=/opt/local
exec_prefix=/opt/local
libdir=${exec_prefix}/lib
includedir=${prefix}/include


Name: Atk
Description: Accessibility Toolkit
Version: 1.9.0
Requires: gobject-2.0 gmodule-no-export-2.0
Libs: -L${libdir} -latk-1.0
Cflags: -I${includedir}/atk-1.0


On a Solaris 10 system the default directory is /usr/lib/pkgconfig. Other pkg-config files can be found in /usr/sfw/lib/pkgconfig.


You can have several of these directories by setting an environment variable.


man pkg-config for details.


Then instead of passing the include and library directories to configure, you let the .pc file do all of the work for you.


As always, consult the GCC Manual for a list of flags that you may want to set.


Next Section:  Running Configure (4 of 11)



This Web Site Copyright © 1997 - 2008
by Alan Pae - All Rights Reserved