References Dynamic-Link Libraries and Static-Link Libraries With GCC

    Apr 20, 2023

    Give an example: how to write dynamic and static link libraries and reference them in C programs. And how the link library works

    Compile

    The gcc compiler can compile code into a DLL dynamic link library in Windows. However, it should be noted that in Windows, the naming convention of dynamic link libraries is different from systems such as Linux. In Windows, the naming convention of dynamic link libraries is usually <name>.dll or lib<name>.dll, where <name> is the name of the dynamic link library.

    In addition, specific compilation options and link options need to be used when compiling dynamic link libraries, such as:

    • Use the -shared option to specify compilation as a dynamic link library.
    • Use the -fPIC option to generate position-independent code so that dynamic link libraries can be loaded correctly at runtime.

    First write the code of the library. Suppose we define a Hello function in the library:

    // hello.c
    #include <stdio.h>
    void hello() {
        printf("Hello, world!\n");
    }
    

    Here is a simple command example that demonstrates how to use the gcc compiler to compile code into a DLL dynamic link library in Windows:

    gcc -shared -fPIC -o hello.dll hello.c
    

    This will generate a dynamic link library named hello.dll.

    If you are on a Unix-like platform, the dynamic link library convention is different from Windows, and it uses the so suffix. So the compilation command can be written as follows:

    gcc -shared -fPIC -o libhello.so hello.c
    

    Usage

    To use a dynamic link library, you must first give the header file of this library: define the relevant declarations in the library. We write a header file for the Hello library:

    // hello.h
    #ifndef HELLO_H_
    #define HELLO_H_
    int hello();
    #endif // HELLO_H_
    

    In Windows, introducing a dynamic link library requires the help of library functions defined in Windows.h provided by the Windows SDK

    // main.c
    #include <windows.h>
    #include <stdio.h>
    #include "hello.h"
    int main() {
        HINSTANCE hInstLibrary = LoadLibrary("hello.dll");
        if (hInstLibrary == NULL) {
            printf("Failed to load DLL.\n");
            return 1;
        }
        // Get the function address in DLL
        void (*pFnHello)() = (void (*)())GetProcAddress(hInstLibrary, "hello");
        if (pFnHello == NULL) {
            printf("Failed to get function hello.\n");
            FreeLibrary(hInstLibrary);
            return 1;
        }
        // Call the function in DLL
        pFnHello();
        // Release DLL
        FreeLibrary(hInstLibrary);
        return 0;
    }
    

    If it is a Unix-like platform, it can be referenced like this

    // main.c
    #include <stdio.h>
    #include <dlfcn.h>
    #include "hello.h"
    int main() {
        void* handle = dlopen("./libhello.so", RTLD_LAZY);
        if (!handle) {
            fprintf(stderr, "Error: %s\n", dlerror());
            return 1;
        }
        int (*hello)(int, int) = dlsym(handle, "hello");
        if (!hello) {
            fprintf(stderr, "Error: %s\n", dlerror());
            dlclose(handle);
            return 1;
        }
        hello();
        dlclose(handle);
        return 0;
    }
    

    Then compile the target program

    gcc main.c
    

    Compile

    First write the code of the library. Suppose we define a Hello function in the library:

    // hello.c
    #include <stdio.h>
    void hello() {
        printf("Hello, world!\n");
    }
    

    Here is a simple command example that demonstrates how to use the gcc compiler to compile code into a static link library:

    First, compile the hello.c code into an object file

    gcc -c hello.c -o hello.o
    

    Then use the ar command to package the target file into a static library.

    Executable on Windows systems

    ar rcs hello.lib hello.o
    

    Executable on Unix-like systems

    ar rcs libhello.a hello.o
    

    Usage

    Similarly, using a static link library also requires writing a header file for the library

    // hello.h
    #ifndef HELLO_H_
    #define HELLO_H_
    int hello();
    #endif // HELLO_H_
    

    Reference library functions in the main program

    //main.c
    #include <stdio.h>
    #include "hello.h"
    int main() {
        hello()
    }
    

    Then add the link option to the hello library when compiling the main program.

    gcc main.c -L. -lhello -o main