????1????? c ???????????mylib.so??????
???????????mylib.h

 

#ifndef __MY_LIB_H__
#define __MY_LIB_H__
void foobar(int i);
#endif /* __MY_LIB_H__ */

??????????mylib.c

 

#include <stdio.h>
#include "mylib.h"
void foobar(int i)
{
printf("Printing from mylib.so %d "?? i);
}

??????? gcc ?? mylib.c ????????????????????????????
????[steven@sasd c++]$ gcc-fPIC -shared -o mylib.so mylib.c
????[steven@sasd c++]$ ll
????total 16
????-rw-rw-r--. 1 steven steven  109 Apr 13 09:27 mylib.c
????-rw-rw-r--. 1 steven steven   90 Apr 13 09:26 mylib.h
????-rwxrwxr-x. 1 steven steven 6210 Apr 1309:28 mylib.so
???????
????2?????????????????????????????????Dynamic Shared Objects????д?DSO????
????2-fPIC???????????Position-Independent Code?????ü????????????SO?ж????????λ????
????2?????????.so
???????????????c++???????????????.so???????C++?У?????? linkage directive ???????????C++???????????????????extern “C”??????????????????C?????????????????????е?????????????????C????????????????????????????

 

// use_mylib.cpp
#include <iostream>
#include <string>
using namespace std;
#ifdef __cplusplus
extern "C" {
#endif
#include "mylib.h" // for foobar()
#ifdef __cplusplus
}
#endif
int main()
{
foobar(100);
return 0;
}

???????????н????
????[steven@sasd c++]$ g++ -o use_mylibuse_mylib.cpp ./mylib.so
????[steven@sasd c++]$ ./use_mylib
????Printing from mylib.so 100
????[steven@sasd c++]$