gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:18:45 25 4
gpt4 key购买 nike

我有一个名为 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$

有人知道吗?

最佳答案

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

查看以下内容了解详情:

g++ -L/home/用户名/matrix -Wall -o test test.cpp -lmatrix

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

关于c++ - 如何通过 G++ 使用共享库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29716256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com