Generation of dynamic libraries:
1,/*mysum.c*/
Copy Code code as follows:
#include <stdio.h>
#include "src.h"
int sum (int a,int b)
{
return (A+B);
}
2,/*mysum.h*/
Copy Code code as follows:
#ifndef __src_h__
#define __src_h__
int sum (int a,int b);
#endif
3,/*main.c*/
Copy Code code as follows:
#include <stdio.h>
extern int sum (int a,int b);
/*
#include <stdio.h>
#include "mysum.h"
*/
int main ()
{
int a,b,c=0;
printf ("Input A and b:\n");
scanf ("%d%d", &a,&b);
c = SUM (A,B);
printf ("c =%d\n", c);
return 0;
}
4. Generate Dynamic Library
Copy Code code as follows:
Gcc-c-fpic mysum.c mysum.h//Generate MYSUM.O file
Gcc-shared-fpic-o libmysum.so MYSUM.O//Generate dynamic library file, the default naming method is lib+ name. So
5, dynamic library static loading
Copy Code code as follows:
Gcc-o main main.c-l.-lmysum-wall
Or
Gcc-o Main Main.c-lmysum-wall
6. When executing./main, an error is reported, prompting that the Libmysum.so shared library cannot be loaded.
7, the processing method has three:
Method One:
Copy a dynamic library to/lib or/usr/lib
such as: sudo cp libmysum.so/lib
Method Two:
Changing environment variables:
Ld_library_path= $PWD Export Ld_library_path
Method Three:
Specify the path to the shared library at compile time:
Gcc-o main1-wl,-rpath,.-L.-louyang MAIN.O