g++编译运行c++代码流程以及动态库静态库的创建(2)

linuxidc@linuxidc:~/work/cpp$ ls
add.c  add.h  add.o  a.out  mian.c
linuxidc@linuxidc:~/work/cpp$ rm a.out
linuxidc@linuxidc:~/work/cpp$ ar -r libadd.a add.o
ar: creating libadd.a
linuxidc@linuxidc:~/work/cpp$ ls
add.c  add.h  add.o  libadd.a  mian.c
linuxidc@linuxidc:~/work/cpp$ g++ mian.c -llibadd -L./ -static -o main
/usr/bin/ld: cannot find -llibadd
collect2: error: ld returned 1 exit status
linuxidc@linuxidc:~/work/cpp$ g++ mian.c -libadd -L./ -static -o main
/usr/bin/ld: cannot find -libadd
collect2: error: ld returned 1 exit status
linuxidc@linuxidc:~/work/cpp$ pwd
/home/linuxidc/work/cpp
linuxidc@linuxidc:~/work/cpp$ g++ mian.c -libadd -L/home/linuxidc/work/cpp -static -o main
/usr/bin/ld: cannot find -libadd
collect2: error: ld returned 1 exit status
linuxidc@linuxidc:~/work/cpp$ g++ mian.c -ladd -L/home/linuxidc/work/cpp -static -o main
linuxidc@linuxidc:~/work/cpp$ ls
add.c  add.h  add.o  libadd.a  main  mian.c
linuxidc@linuxidc:~/work/cpp$ ./main
1+2=3

其他地方也要用的话

linuxidc@linuxidc:~/work$ ls
add.h  cpp  mian.c
linuxidc@linuxidc:~/work$ g++ mian.c -ladd -L./cpp -static -o main
linuxidc@linuxidc:~/work$ ls
add.h  cpp  main  mian.c
linuxidc@linuxidc:~/work$ ./main
1+2=3

2 动态函数库的建立与使用


  1.首先创建libmyadd.so,即利用add.c这个函数生成动态库
  g++ -share -fPIC -o libmyadd.so add.c
  -share指为共享的,-fPIC表示position independent code位置无关,这是动态库特性
  2.指定动态库生成可执行文件,-L.表示当前文件夹,-lmyadd表示去找libmyadd.so这个动态库文件。
  g++ main.c -L. -lmyadd
  3.直接使用会抱错,找不到动态库,要指定动态库的路径
  ./a.out会报错
  LD_LIBRARY_PATH=. ./a.out指定当前库的路径后在运行就可以了
  4.如果要一直用,可以将.so文件的目录添加到/etc/ld.so.conf里面,然后再执行ldconfig就行了,具体如下面:

linuxidc@linuxidc:~/work/cpp$ ls
add.c  add.h  add.o  main.c
linuxidc@linuxidc:~/work/cpp$ g++ -share -fPIC -o libmyadd.so add.c
g++: error: unrecognized command line option ‘-share’
linuxidc@linuxidc:~/work/cpp$ g++ -shared -fPIC -o libmyadd.so add.c
linuxidc@linuxidc:~/work/cpp$ ls
add.c  add.h  add.o  libmyadd.so  main.c
linuxidc@linuxidc:~/work/cpp$ g++ main.c -L. -lmyadd
linuxidc@linuxidc:~/work/cpp$ ls
add.c  add.h  add.o  a.out  libmyadd.so  main.c
linuxidc@linuxidc:~/work/cpp$ ./a.out
./a.out: error while loading shared libraries: libmyadd.so: cannot open shared object file: No such file or directory
linuxidc@linuxidc:~/work/cpp$ LD_LIBRARY_PATH=. ./a.out
1+2=3

如果要永久使用的话该怎么办呢?

linuxidc@linuxidc:~/work/cpp$ ls
add.c  add.h  add.o  a.out  libmyadd.so  main.c
linuxidc@linuxidc:~/work/cpp$ ./a.out  这里直接用会报错
./a.out: error while loading shared libraries: libmyadd.so: cannot open shared object file: No such file or directory
linuxidc@linuxidc:~/work/cpp$ vi /etc/ld.so.conf  在这个文件里面添加.so所在的目录就行了
linuxidc@linuxidc:~/work/cpp$ pwd
/home/linuxidc/work/cpp
linuxidc@linuxidc:~/work/cpp$ vi /etc/ld.so.conf
linuxidc@linuxidc:~/work/cpp$ sudo ldconfig
linuxidc@linuxidc:~/work/cpp$ ./a.out
1+2=3

例如,我的添加后的ld.so.conf文件就是这个样子:

include /etc/ld.so.conf.d/*.conf
/home/linuxidc/work/cpp

Linux公社的RSS地址https://www.linuxidc.com/rssFeed.aspx

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/3e706d1edd1492f84fa1a2f66470500a.html