Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Statically linking libraries in MinGW

Suppose I have three C source files. The first two to be LIBs (lib*.a?), and the third is an application which uses them.

The first is (re.c):

int re(int i) {
    return i;
}

The second is (test.c):

int re(int); // Depends on re.c

int test(int i) {
    return re(i);
}

And the third is (main.c):

#include<stdio.h>

int test(int); // Uses test.c

int main(void) {
    
    printf("%d\n",test(0));
    return 0;
}

Now how can I create the first two LIBs in such a way that allows me to statically link them later with main application?

I know how to create the DLLs and link them dynamically in my application such as:

cc -o re.dll re.c -shared -Wl,--out-implib=libre.a (for re.c)

cc -o test.dll test.c -L. -lre -shared -Wl,--out-implib=libtest.a (for test.c)

cc -o main.exe main.c -L. -lre -ltest


So how to create equivalent LIBs to be statically linked within my executable binary in MinGW, and how to link them?

Obviously, under Windows :)

like image 369
Ghasan غسان Avatar asked May 27 '12 15:05

Ghasan غسان


People also ask

How static library is linked?

In computer science, a static library or statically-linked library is a set of routines, external functions and variables which are resolved in a caller at compile-time and copied into a target application by a compiler, linker, or binder, producing an object file and a stand-alone executable.

Is libc statically-linked?

Statically linking libc is it's own minefield. It can and is done but even if you statically link everything else you should almost always dynamically link against your platform's libc. Statically linking libc is harder than dynamically linking it, but certainly easier than rewriting it.

What is the difference between using dynamically and statically-linked libraries?

What are the differences between static and dynamic libraries? Static libraries, while reusable in multiple programs, are locked into a program at compile time. Dynamic, or shared libraries, on the other hand, exist as separate files outside of the executable file.


2 Answers

I found the solution here: http://www.codeproject.com/Articles/84461/MinGW-Static-and-Dynamic-Libraries

The idea being is to compile all libraries (source files) without linking. Then converting the output objects with ar rcs -o lib*.a *.o where * is the name of objects created (converting them one by one). After that, we simply compile the application with -L. to specify the directory and with -l* to specify libraries names without GNU naming decoration.

For those libs which depends on others, they should be specified first and then the referenced libs, or else errors such as undefined reference to re will occur when I did -lre -ltest, where -ltest -lre is the right one, because test library refers to re library.

Here is how I compiled it:

cc -c -o test.o test.c

cc -c -o re.o re.c

ar rcs -o libtest.a test.o

ar rcs -o libre.a re.o

cc -o main.exe main.c -L. -ltest -lre

It also works for Tiny C Compiler.

like image 159
Ghasan غسان Avatar answered Oct 16 '22 14:10

Ghasan غسان


Have you tried using the options: -static -mwindows

like image 20
rotator Avatar answered Oct 16 '22 14:10

rotator