依然使用前两文章中的h1.c 和h1.c及main.c文件
$ gcc -shared -fPIC -o libdhello.so h1.o h2.o
这样就能创建libdhello.so动态库了.-f后面跟一些编译选项,PIC 是其中一种,表示生成位置无关代码(Position Independent Code)。
编译使用的时候, $ gcc -o dhello main.c -L. -ldhello
运行./dhello
./dhello: error while loading shared libraries: libdhello.so: cannot open shared object file: No such file or directory
结果出错!!!!
这是因为动态库还没有加载到内存中呢.
如何加载呢? 加上$ LD_LIBRARY_PATH的路径!!
$ LD_LIBRARY_PATH=. ./dhello
First Hello.
Second Hello.