参考:http://blog.csdn.net/ctthunagchneg/article/details/8509489
(整个工程在这里下载)
目录结构:
> main.cpp
> Makefile
> so (directory)
>> hello.h
>> hello.cpp
>> Makefile
so路径下的文件:
hello.h
void Hello();
hello.cpp
#include "hello.h"
#include <iostream>
using namespace std;
void Hello() {
cout << "Hello world!" << endl;
}
Makefile
all:
g++ -fPIC -c hello.cpp
g++ -shared -fPIC -o libhello.so hello.o
echo `pwd` >> /etc/ld.so.conf
ldconfig
clean:
rm -f *.o *.so
主工程路径下的文件:
main.cpp
#include "hello.h"
int main(int argc, char* argv[]) {
Hello();
return 0;
}
Makefile
all:
g++ main.cpp -I./so -L./so -lhello -o main
clean:
rm -f main