Linux 6 statically linked shared libraries so

1. First compile .c files

gcc -c -o sharelib.o -fPIC $ sharelib.c
-c indicates that only the compiler (compile), without connecting.
-o option is used to illustrate output (output) file name.
gcc generates an object (object) file sharelib.o.
-fPIC refers Position Independent Code. This option requires a shared library, in order to achieve dynamic linking (dynamic linking).

2. Generate a shared library

gcc -shared -o $ libsharelib.so sharelib.o
library file begins with lib. Shared library files with .so extension.
-shared represent generate a shared library.

3. shared library

We need .h and .so files in the current directory, therefore, use the following command to compile:
$ gcc -o test test.c -lsharelib -L.
The test generated executable file.

Use
$. / Test
execution program

We have the following problems:

./test: error while loading shared libraries: libsharelib.so: cannot open shared object file: No such file or directory

This is because the operating system can not find the library. libsharelib.so in the current path, the default path is located outside the library files. Although we provide the location of the .so file at compile time (compile time), but this information is not written to test the executable file (runtime). You can use the following command to test:
$ ldd the Test
ldd is used to display an executable file is dependent libraries.

To solve the above problem, we can .so files in the default search path. But sometimes, especially under multi-user environment, we do not enjoy the privileges of the default search path is written.
One solution is to set the LD_LIBRARY_PATH environment variable. For example:
. $ Export LD_LIBRARY_PATH =
Thus, when an executable file is executed, the operating system earlier in the LD_LIBRARY_PATH under the search base file, then the default search path. Harm the environment variable is that it affects all executable programs. If we compile another program, if we are not careful, it could lead to other executable files can not run. Therefore, LD_LIBRARY_PATH environment variables used for testing.
Another solution, which provides -rpath option to write test file (rpath on behalf of runtime path) search path information. This eliminates the need to set environment variables. The downside is that to do so, if the libraries have been moved position, we need to recompile the test. Using the following command to compile test.c:
$ -o Test test.c -lsharelib GCC -g -L -Wl, -rpath =..
-Wl expressed, -rpath options are passed to the linker (linker).

example

Providing a shared library ± * / function.
makefile:

.PHONE : all

all : libcaculate.so testLoad testLink

###生成动态共享库
libcaculate.so : caculate.o
	gcc -Wall -fPIC -shared $^ -o $@
testLoad: testLoad.o
	gcc -Wall -L./ -rdynamic $^ -o $@ -ldl
testLoad.o : testLoad.c
	gcc -c $<
caculate.o : caculate.c
	gcc -c $<
###编译的时候链接库libcalculate.so
testLink : testLink.o
	gcc -L./ -lcaculate -Wl,--rpath=. $< -o $@

testLink.o : testLink.c
	gcc -c $<
	
clean:
	rm -f *.so *.o testLoad testLink

Source File:

/* calculate.c */

#include "stdlib.h"

int add(int a,int b)
{
    return (a + b);
}

int sub(int a, int b)
{
    return (a - b);
}

int mul(int a, int b)
{
    return (a * b);
}

int dive(int a, int b)
{
    return (a / b);
}

When compiling a dynamic link library using the method:

/* testlink.c */
#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("add: %d\n", add(2,7));

    printf("sub: %d\n", sub(9,2));

    printf("mul: %d\n", mul(3,2));

    printf("dive: %d\n", dive(8,2));
    
    exit(0);
}

Guess you like

Origin blog.csdn.net/zzj244392657/article/details/92561607
Recommended