"g++ -l..." to Link with Library Files

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

 About This Book

 Introduction to Linux Systems

 Cockpit - Web Portal for Administrator

 Process Management

 Files and Directories

 Users and Groups

 File Systems

 Block Devices and Partitions

 LVM (Logical Volume Manager)

 Installing CentOS

 SELinux - Security-Enhanced Linux

 Network Connection on CentOS

 Software Package Manager on CentOS - DNF and YUM

 Running Apache Web Server (httpd) on Linux Systems

 Running PHP Scripts on Linux Systems

 Running MySQL Database Server on Linux Systems

 Running Python Scripts on Linux Systems

 vsftpd - Very Secure FTP Daemon

 Postfix - Mail Transport Agent (MTA)

 Dovecot - IMAP and POP3 Server

 Email Client Tools - Mail User Agents (MUA)

 LDAP (Lightweight Directory Access Protocol)

GCC - C/C++ Compiler

 Install GCC C/C++ Compilers

 "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

 Conda - Environment and Package Manager

 Tools and Utilities

 Administrative Tasks

 References

 Full Version in PDF/EPUB