Linux Apps Tutorials - Herong's Tutorial Examples - v1.03, by Herong Yang
"g++ -l..." to Link with Library Files
This section provides a tutorial example on how to use 'g++ -static' and 'g++ -l...' options to link your program with static or dynamic library files.
GCC C/C++ compilers support 2 types of library files:
1. Static Library Files - Static library files are also called Archive files and have .a file extension on Linux systems. When you link your program with static library files, all objects referenced by your program will be verified and copied into the final executable file. This makes the executable file much bigger. But it makes the execution easier, since static library files are no longer needed at the execution time.
2. Dynamic Library Files - Static library files are also called Shared Object files and have .so file extension on Linux systems. When you link your program with Dynamic library files, all objects referenced by your program will be verified but not copied into the final executable file. This makes the executable file much smaller. But it makes the execution harder, since dynamic library files needs to be loaded into CPU together with your executable file.
The GCC C/C++ compilers allows you to control which type of library files to use and specify library file names using "-static" and "-l..." options. Note that the actual library file name is computed from whatever specified in the "-l..." option with "lib" prefix and .a or .so extension.
Example 1 - the following command links 2 static library files, libAbc.a and libXyz.a in the current directory:
herong$ g++ -static -lAbc -lXyz myProgram.cpp
Example 2 - the following command links 2 static library files, libAbc.so and libXyz.so in the current directory:
herong$ g++ -lAbc -lXyz myProgram.cpp
Table of Contents
Running Apache HTTP Server (httpd) on Linux Systems
Running Apache Tomcat on Linux Systems
Running PHP Scripts on Linux Systems
Running MySQL Database Server on Linux Systems
Running Python Scripts on Linux Systems
Conda - Environment and Package Manager
"g++ --verbose" - GCC Compiler Steps and Settings
"g++ -I..." and CPATH Environment Variable
►"g++ -l..." to Link with Library Files
"g++ -c" and "ar src" to Build Static Library
"g++ -shared" to Build Dynamic Library
"ldd" - Dynamic Library Dependency Checker
"make" - Manage Program Build Process