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

时间:2015-04-18 10:52:33

标签: c++ g++ shared-libraries

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

我可以成功生成并使用 static 库,但是当我想将它用作共享库时,我收到以下错误:

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$

有没有人有任何想法?

1 个答案:

答案 0 :(得分:1)

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

请查看以下详细信息:

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

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