"g++ -shared" to Build Dynamic Library

This section provides a tutorial example on how to use 'g++ -shared' commands to compile your code and to build dynamic library file on Linux systems.

If you want to build your own dynamic library files, you need to do it with the "g++ -shared" command.

Here is what I did to build by first static library file: libMyDynamic.so:

1. Create a source code file, MyDynamic.cpp

herong$ cat MyDynamic.cpp

// MyDynamic.cpp
#include "iostream"
void myDynamicFunc() {
  std::cout <<  "Running myDynamicFunc()..." <<  std::endl;

2. Compile source code file, MyDynamic.cpp, into a dynamic library file:

herong$ g++ -shared -o libMyDynamic.so MyDynamic.cpp
/usr/bin/ld: /tmp/ccG05g0J.o: relocation R_X86_64_32 against `.rodata'
   can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: Nonrepresentable section on output

3. Re-compile it again with "-fPIC" option as suggested:

herong$ g++ -shared -fPIC -o libMyDynamic.so MyDynamic.cpp

herong$ ls -l *MyDynamic*
-rwxrwxr-x. 1 herong herong 8872 Apr 13 01:57 libMyDynamic.so
-rw-rw-r--. 1 herong herong  120 Apr 13 01:53 MyDynamic.cpp

4. Create a calling program, MyDynamicTest.cpp:

herong$ cat MyDynamicTest.cpp

// MyDynamicTest.cpp
#include "iostream"
extern void myDynamicFunc();
int main() {
  std::cout <<  "Calling myDynamicFunc()..." <<  std::endl;

5. Link MyDynamicTest.cpp with libMyDynamic.so without giving location path:

herong$ g++ -lMyDynamic MyDynamicTest.cpp
/usr/bin/ld: cannot find -lMyDynamic

6. Link MyDynamicTest.cpp with libMyDynamic.so with giving location path using the "g++ -L ..." option. I can also use the "LIBRARY_PATH" environment variable instead of "g++ -L ...".

herong$ g++ -L . -lMyDynamic MyDynamicTest.cpp

herong$ ./a.out
Calling myDynamicFunc()...
Running myDynamicFunc()...

7. Logout and login to run the program again. I got an error as expected, because libMyDynamic.so is no longer loaded in the CPU, and the program loader has no idea where to find it.

herong$ ./a.out
./a.out: error while loading shared libraries: libMyDynamic.so:
   cannot open shared object file: No such file or directory

8. Run it again with LD_LIBRARY_PATH set to the dynamic library path:

herong$ export LD_LIBRARY_PATH=.

herong$ ./a.out
Calling myDynamicFunc()...
Running myDynamicFunc()...

Cool. GCC C/C++ compiler is much easier to work with dynamic libraries than static libraries in Linux systems.

Table of Contents

 About This Book

 Introduction to Linux Systems

 Process Management

 Files and Directories

 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

 Conda - Environment and Package Manager

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

 Graphics Environments on Linux

 Tools and Utilities


 Full Version in PDF/EPUB