"g++ -c" and "ar src" to Build Static Library

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

If you want to build your own static library files, you need to do it in two steps:

Here is what I did to build by first static library file: libMyStatic.a:

1. Create a source code file, MyStatic.cpp

herong$ cat MyStatic.cpp

// MyStatic.cpp
#include "iostream"
void myStaticFunc() {
  std::cout <<  "Running myStaticFunc()..." <<  std::endl;
}

2. Compile source code file, MyStatic.cpp, into object code file, MyStatic.o:

herong$ g++ -c MyStatic.cpp

herong$ ls -l MyStatic.*
-rw-rw-r--. 1 herong herong  117 Apr 10 23:50 MyStatic.cpp
-rw-rw-r--. 1 herong herong 2744 Apr 10 23:52 MyStatic.o

3. Create static library file, libMyStatic.a from object code file, MyStatic.o:

herong$ ar src libMyStatic.a MyStatic.o

herong$ ls -l *MyStatic.*
-rw-rw-r--. 1 herong herong 2898 Apr 10 23:54 libMyStatic.a
-rw-rw-r--. 1 herong herong  117 Apr 10 23:50 MyStatic.cpp
-rw-rw-r--. 1 herong herong 2744 Apr 10 23:52 MyStatic.o

herong$ ar t libMyStatic.a
MyStatic.o

4. Create a calling program, MyStaticTest.cpp:

herong$ cat MyStaticTest.cpp

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

5. Link MyStaticTest.cpp with MyStatic.o directly to confirm that the test program is working.

herong$ g++ MyStatic.o MyStaticTest.cpp

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

6. Link MyStaticTest.cpp with libMyStatic.a without giving location path:

herong$ g++ -lMyStatic MyStaticTest.cpp
/usr/bin/ld: cannot find -lMyStatic

7. Link MyStaticTest.cpp with libMyStatic.a 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 . -lMyStatic MyStaticTest.cpp
/tmp/cciFWV0h.o: In function `main':
MyStaticTest.cpp:(.text+0x21): undefined reference to `myStaticFunc()'

8. Force GCC to do static library linking:

herong$ g++ -static -L . -lMyStatic MyStaticTest.cpp
/usr/bin/ld: cannot find -lstdc++
/usr/bin/ld: cannot find -lm
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status

Looks like I am still missing somethings to make GCC working with static library files.

By the way, you can use "LIBRARY_PATH" environment variable to specify static library file path instead of the "g++ -L ..." option.

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

 SquirrelMail - Webmail in PHP

 Tools and Utilities

 References

 Full Version in PDF/EPUB