Statically linking libraries in MinGW

前端 未结 3 2114
我在风中等你
我在风中等你 2021-02-06 05:55

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         


        
3条回答
  •  执念已碎
    2021-02-06 06:17

    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 re.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.

提交回复
热议问题