C language to create a shared library (DLL) Step

C language to create a shared library (DLL) steps:

Creating sth.c, shared library source files:

// sth.c library source code 
unsigned Long  Long STH (unsigned int the X-, unsigned int the y-)
{
  return (x + y + x * y);
}

Create a test file:

//test.c
#include<stdio.h>
#include<stdlib.h>

int main(int argc, char *argv[])
{
  unsigned int x, y;
  sscanf(argv[1], "%u", &x);
  sscanf(argv[2], "%u", &y);

  Long  Long RES = STH (X, Y); // call the function defined in the shared library file 
  the printf ( " answer IS% U \ n- " , RES);
   return  0 ;
}

Create a library file:

First use of gcc -fPIC option to construct a dynamic object file library:

gcc -fPIC -Wall -c sth.c

Next, use -shared the options and object code has been created, generating a dynamic librarylibsth.so:

gcc -shared -o libsth.so sth.o

The following compile the test main program, it will be linked to the newly generated dynamic library libsth.so:

gcc -o test test.c -L . -lsth
sudo cp libsth.so /lib
./test 5 6
answer is 41

Guess you like

Origin www.cnblogs.com/ming-4/p/11853705.html