read: gcc - g++ (Preprocessor - Compiler - Assembler - Linker - Execution)
|
Thing |
Description |
|---|---|
|
header file |
|
|
library |
|
Static Library vs. Shared Library
|
External Libraries |
Extension |
Description |
|---|---|---|
|
static library |
|
When your program is linked against a static library, the machine code of external functions used in your program is copied into the executable. A static library can be created via the archive program “ |
|
shared library |
|
When your program is linked against a shared library, only a small table is created in the executable. Before the executable starts running, the operating system loads the machine code needed for the external functions - a process known as dynamic linking. Dynamic linking makes executable files smaller and saves disk space, because one copy of a library can be shared between multiple programs. Furthermore, most operating systems allows one copy of a shared library in memory to be used by all running programs, thus, saving memory. The shared library codes can be upgraded without the need to recompile your program. |
Searching for Header Files and Object-Files/Libraries (-I, -L and -l)
when building a C/C++ program:
- preprocessor needs to know where the header files are to preprocess the source codes
- linker needs to know where the libraries are to resolve the source code’s external references to other object files
The preprocessor and linker will not find the headers & libraries unless you set the appropriate options (e.g. -I, -L, -l) specifying where things are
Header Files (-I)
For each of the headers (e.g. #include “glew.h”) used in the C/C++ program, the preprocessor searches through the include-paths for files that match the headers.
The include-paths are specified:
-I option - (e.g. g++ main.cpp -I/path/to/directory/containing/header-file)- environment variable
CPATH
Since the header’s filename is known (e.g., iostream.h, stdio.h), the compiler only needs the directories
Object-Files/Libraries (-L, -l)
The linker searches the library-paths for libraries needed to link the program into an executable.
The library-path is specified:
-L option - (e.g. g++ main.cpp -L/path/to/directory/containing/object-files)- environment variable
LIBRARY_PATH
In addition, you also have to specify the library name:
- In Unixs, the library
libxxx.ais specified via-lxxxoption (lowercase letter‘l’, without the prefix “lib” and“.a” extension) - In Windows, provide the full name such as
-lxxx.lib.
The linker needs to know both the directories as well as the library names. Hence, two options (-L, -l) need to be specified
GCC Environment Variables
GCC uses the following environment variables:
PATH: for searching the executables and run-time shared libraries (.dll,.so).CPATH: for searching the include-paths for headers. It is searched after paths specified in-I<dir>optionsC_INCLUDE_PATH&CPLUS_INCLUDE_PATHcan be used to specify C and C++ headers if the particular language was indicated in pre-processing.
LIBRARY_PATH: for searching library-paths for link libraries. It is searched after paths specified in -L<dir>options.
Default Include-Paths, Library-Paths and Libraries
list default include-paths
cpp -v
......
#include "..." search starts here:
#include <...> search starts here:
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/include
/usr/include
/usr/lib/gcc/x86_64-pc-cygwin/6.4.0/../../../../lib/../include/w32api
list default library-paths
ld --verbose | grep SEARCH_DIR | tr -s ' ;' \\012gcc passes a few extra -L paths to the linker, which you can list with the following command
gcc -print-search-dirs | sed '/^lib/b 1;d;:1;s,/[^/.][^/]*/\.\./,/,;t 1;s,:[^=]*=,:;,;s,;,; ,g' | tr \; \\012