C File FILEA.C:
#include <stdio.h>void fun1 () {printf ("I am from Filea fun1 \ n");}
C File FILEB.C:
#include <stdio.h>void fun2 () {printf ("I am from Fileb fun2 \ n");}
Test file testso.c
void Fun1 (), void fun2 (), int main (int argc,char *argv[]) {fun1 (); fun2 (); return 0;}
1. Compile the filea.c fileb.c into the so library for TESTSO.C invocation, as follows:
gcc-wall-fpic-shared filea.c Fileb.c-o first.so
2. Compile the testso.c and link the shared library first.so
Gcc-wall testso.c First.so-o Testso
3. Implementation.
At this point, execution./testso will be thrown ./testso:error while loading shared libraries:first.so.0:cannot open shared object file:no such F Ile or directory
The program Testso has been completed, but to run it must be able to locate the shared library first.so, because the functions in the library are loaded when the program is run. It is important to note that the current working directory may not be in the lookup path of the shared library, so you need to set the environment variable ld_library_path using the following command line:
$ export ld_library_path= $LD _library_path:./
Execute the./testso again and output the correct result:
C + + compiled so with dynamic load so