如何通过 G++ 使用共享库

How to use shared library via G++?

本文关键字:共享 何通过 G++      更新时间:2023-10-16

我有一个名为 matrix 的库,并在名为 test.cpp 的程序中使用。

我可以成功生成和使用静态库,但是当我想将其用作共享库时,我收到以下错误:

ap1019@sharifvm:~/the03-copy$ ls
matrix.cpp  matrix.h  test.cpp
ap1019@sharifvm:~/the03-copy$ g++ -c matrix.cpp
ap1019@sharifvm:~/the03-copy$ g++ -shared -Wl,-soname,matrix.so -o matrix.so matrix.o
ap1019@sharifvm:~/the03-copy$ ls
matrix.cpp  matrix.h  matrix.o  matrix.so  test.cpp
ap1019@sharifvm:~/the03-copy$ g++ test.cpp matrix.so
ap1019@sharifvm:~/the03-copy$ ./a.out
./a.out: error while loading shared libraries: matrix.so: cannot open shared object file: No such file or directory
ap1019@sharifvm:~/the03-copy$

有人知道吗?

最好遵循共享库的命名约定。您以错误的方式链接。

有关详细信息,请查看以下内容:

g++ -L/home/username/matrix -Wall -o test test.cpp -lmatrix

http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html