I have an executable file (which I get by compiling MyProg.c). Before, I statically linked the library to it (libMyL.so).

This is how the library is created:

gcc -Wall -fPIC -c f1.c f2.c f3.c -Ilib gcc -shared f1.o f2.o f3.o -o libMyL.so -lrt -lpthread 

This is how the library is linked to the program:

 gcc MyProg.c -L. -lMyL -o MyProg 

Accordingly, for the program to run on another computer, libMyL.so should be, for example, in /lib .

Everything is working. But now I need to link, preferably, absolutely everything statically. That is, so that there is no dependence on the libraries and could be transferred to a computer with the same architecture.

Those. for example, if I want to build an executable file and stuff all the libraries into it, I do this:

Hello.c file content

 #include <stdio.h> int main( int argc, char* argv[] ) { printf( "Hello world!" ); return 0; } gcc -static Hello.c -o Hello 

The Hello file is 500 Kb. All OK. Similarly, I need with MyProg + libMyL.so. It is necessary, apparently, that in MyProg everything was collected statically, and in libMyL.so to push all the dependencies. The fact that you get a large amount is okay. How do I implement this?

  • one
    So what's the difficulty? Is it not possible just to link statically? Or compile the lib so that the output will be * .a * .la files instead of .so? - cy6erGn0m
  • and how to do it and how then statically link it to the application? I try to constantly get me out ld returned 1 exit status - G71
  • one
    Well, so ... the text of the error in the studio! - cy6erGn0m

1 answer 1

Making static libraries is very simple.

  1. Compile the text into an object file (without -fPIC).
  2. Create a library using the ar utility like this:

    ar crv libmylibrary.a f1.o f2.o

The result is a static library containing the necessary modules. It always has the ending .a and the prefix lib. Connecting is easy:

gcc Hello.c -o myprog -L. -lmylibrary

If there are both static and dynamic libraries with the same name on the disk, then by default the dynamic is linked. For linking static use the -static switch. And another tip - in the assembly line write a static library at the end, after those modules that have a link to it. The same, if 2 libraries, one of which depends on the other: on which depends - at the end. Avoid the interdependency of two static libraries.

In general, all this is described in almost any book on programming in Linux. Find and read.