Click here to Skip to main content
14,573,349 members

MinGW Static and Dynamic Libraries

Rate this:
4.82 (28 votes)
Please Sign up or sign in to vote.
4.82 (28 votes)
4 Jun 2010CPOL
A simple introduction to static and dynamic libraries with GCC.



This article is intended to give a brief and simple introduction to compiling your own libraries with the MinGW (Minimal GNU for Windows) compiler. This article assumes you have a basic understanding of the C language. The first thing you'll need to do is download MinGW. I strongly suggest that you set the Windows environmental path to the MinGW directory. This allows you to call the MinGW GCC compiler from any directory on your computer. On Windows 7, it is right click on My Computer->Properties->Advanced->Environmental Variables and add the MinGW directory to the PATH, or simply type this into cmd.exe:


SET will temporarily set your environmental variables for the duration of your command prompt session. If you would like to permanently change your environmental variables, use SETx instead of SET.

The Static Library

The following code was used to create both the static and dynamic library. It is a simple example to demonstrate a possible math library.

#include "add.h"
int add(int a, int b) {
    return a + b;
#ifndef ADD_H
#define ADD_H
int add(int a, int b);
#endif  // ADD_H

The main file demonstrates how to call the library file after it's compiled. Notice that you require the header file or an external reference to execute a static library. In most cases, if you share your library, it will be in the form of a header file. The function declaration is necessary for the linker. If you did not have access to the header file, you would need to go about reverse engineering the library to create the function declarations.

#include "add.h"
#include <stdio.h>
//extern int add(int a, int b);

int main() {
    int a = 2;
    int b = 1;
    printf("a=%d, b=%d\n", a,b);
    printf("add: %d\n", add(a,b));
    return 0;

Compiling the Static Library


Building the Library

>gcc -c add.c -o add.o
>ar rcs libadd.a add.o

gcc flags:

  • -c: Disables the linker invocation
  • -o: A custom output file

ar flags:

  • -r: Insert the files member... into archive (with replacement).
  • -c: Indicates you are creating an archive file
  • -s: Write an object-file index into the archive, change is made to the archive

It is important that you recognize that the GCC compiler requires that you prefix your static library with the keyword lib and suffix .a, like libadd.a. The lib prefix is required by the linker to find the static library.

Referencing the Library

>gcc -c main.c -o main.o
>gcc -c main.exe main.o -L. -ladd

gcc flags:

  • -L: Indicates the location of the library you want to reference (. = current directory)
  • -l: Specifies the specific library you want to attach

Compiling the Dynamic Library

The Dynamic Library uses the exact same code as before. Most tutorials suggest using __declspec(dllexport) to export your function references. This is not necessary for GCC as __declspec is a Microsoft specific modifier. Secondly, many think of dynamic libraries as DLLs (dynamic linked libraries); however, GNU refers to their dynamic libraries as so (shared objects). Although it is possible to name your shared library with the DLL extension, I do not recommend it as GCC uses the ELF (Executable Linkable Format), and Microsoft Linker uses the older COFF (Common Object File Format). By naming your shared library as a so, you are indicating that it is a GCC shared library, not a Windows specific library.


Referencing the Library

gcc -c add.c -o add.o
gcc -shared -o add.o
gcc -o main.exe main.c

gcc flags:

  • -shared tells the compiler that you want to compile a shared library

Remember that your main.c still requires access to the header of add.h to get the function references. Otherwise you have to manually enter the external reference into your main file.

Comparison of Static to Dynamic


The first thing I noticed was that the dynamic executable was larger than the static. This is due to the fact that the dynamic library has overhead to reference the dynamic library. This normally would not be the case if the DLL contained a large collection of functions. With only one function in the DLL, the overhead of a dynamic library outweighs the need for a static library. In the example image, once the DLL was deleted, the dynamic executable would not run, as it depends on the dynamic library. The static library executes without an external library because they are compiled directly into the executable.



  • Multiple executables can reference the DLL
  • Executables are generally smaller
  • Updates are easier as code is centralized
  • Good for large projects


  • Requires an external library



  • Does not rely on external libraries
  • Good for small projects or write-once-only projects


  • Executables are generally larger as machine code is replicated across files


Dynamic and static libraries both have their places within your projects. Be sure to select the right library for the right job. Keep in mind that regardless of the library type, you will still require the header file to execute the functions contained within the library. I have found that producing libraries is generally easier with GCC. Remember to name your static libraries *.a and your shared library *.so so that they will not be confused as Windows specific *.lib and *.dll libraries. In my next article, I will describe generating libraries in Microsoft Visual C++ and using classes.


Version 1.0.

My next article will explain how to reference libraries with Microsoft Visual C++.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Canada Canada
No Biography provided

Comments and Discussions

SuggestionMissing Linker flags Pin
Member 1330167931-Oct-17 11:20
MemberMember 1330167931-Oct-17 11:20 
QuestionMisleading Pin
Member 1089724220-Jun-14 8:54
MemberMember 1089724220-Jun-14 8:54 
AnswerRe: Misleading Pin
Member 1330167931-Oct-17 11:12
MemberMember 1330167931-Oct-17 11:12 
GeneralMy vote of 4 Pin
AlphaDeltaTheta16-May-13 15:17
MemberAlphaDeltaTheta16-May-13 15:17 
GeneralMy vote of 4 Pin
Louai Haimour30-Jan-13 19:29
MemberLouai Haimour30-Jan-13 19:29 
GeneralThanks Pin
zssure23-Aug-12 15:46
Memberzssure23-Aug-12 15:46 
GeneralMy vote of 4 Pin
zhjxin2-Jul-12 4:34
Memberzhjxin2-Jul-12 4:34 
GeneralMy vote of 5 Pin
Ghoditsttoawn20-Jul-11 20:48
MemberGhoditsttoawn20-Jul-11 20:48 
GeneralMy vote of 5 Pin
kottalovag16-Jun-11 12:12
Memberkottalovag16-Jun-11 12:12 
GeneralA suggestion Pin
Hugh S. Myers31-Mar-11 19:39
MemberHugh S. Myers31-Mar-11 19:39 
GeneralMy vote of 5 Pin
Harris Antrin22-Oct-10 3:11
MemberHarris Antrin22-Oct-10 3:11 
GeneralMy vote of 1 Pin
krisshak011-Aug-10 0:48
Memberkrisshak011-Aug-10 0:48 
GeneralGood Work. Pin
Chris Meech3-Jun-10 1:54
MemberChris Meech3-Jun-10 1:54 
GeneralRe: Good Work. Pin
ShaneMcDonald4-Jun-10 16:01
MemberShaneMcDonald4-Jun-10 16:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 28 May 2010

Tagged as


54 bookmarked