The search paths for header files and libraries can also be controlled through environment variables in the shell. These may be set automatically for each session using the appropriate login file, such as ‘.bash_profile’ in the case of GNU Bash.
Additional directories can be added to the include path using the environment variable C_INCLUDE_PATH
(for C header files) or CPLUS_INCLUDE_PATH
(for C++ header files). For example, the following commands will add ‘/opt/gdbm-1.8.3/include’ to the include path when compiling C programs:
$ C_INCLUDE_PATH=/opt/gdbm-1.8.3/include $ export C_INCLUDE_PATH
and similarly for C++ programs:
$ CPLUS_INCLUDE_PATH=/opt/gdbm-1.8.3/include $ export CPLUS_INCLUDE_PATH
This directory will be searched after any directories specified on the command line with the option -I
, and before the standard default directories (such as‘/usr/local/include’ and ‘/usr/include’). The shell command export
is needed to make the environment variable available to programs outside the shell itself, such as the compiler--it is only needed once for each variable in each shell session, and can also be set in the appropriate login file.(8)
Similarly, additional directories can be added to the link path using the environment variable LIBRARY_PATH
. For example, the following commands will add ‘/opt/gdbm-1.8.3/lib’ to the link path:
$ LIBRARY_PATH=/opt/gdbm-1.8.3/lib $ export LIBRARY_PATH
This directory will be searched after any directories specified on the command line with the option -L
, and before the standard default directories (such as‘/usr/local/lib’ and ‘/usr/lib’).
With the environment variable settings given above the program ‘dbmain.c’ can be compiled without the -I
and -L
options,
$ gcc -Wall dbmain.c -lgdbm
because the default paths now use the directories specified in the environment variables C_INCLUDE_PATH
and LIBRARY_PATH
. The same compilation command with g++
would use the environment variables CPLUS_INCLUDE_PATH
and LIBRARY_PATH
.
Following the standard Unix convention for search paths, several directories can be specified together in an environment variable as a colon separated list:
DIR1:DIR2:DIR3:...
The directories are then searched in order from left to right. A single dot ‘.’ can be used to specify the current directory.(9)
For example, the following settings create default include and link paths for packages installed in the current directory ‘.’ and the ‘include’ and ‘lib’ directories under‘/opt/gdbm-1.8.3’ and ‘/net’ respectively:
$ C_INCLUDE_PATH=.:/opt/gdbm-1.8.3/include:/net/include $ LIBRARY_PATH=.:/opt/gdbm-1.8.3/lib:/net/lib
Source :
An Introduction to GCC - for the GNU compilers gcc and g++ |
No comments:
Post a Comment