【问题标题】:How to use shared library via G++?如何通过 G++ 使用共享库?
【发布时间】:2015-04-18 10:52:32
【问题描述】:

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

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

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$

有人知道吗?

【问题讨论】:

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


    【解决方案1】:

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

    查看以下内容了解详情:

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

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

    【讨论】:

    • 是的,确实如此——C++ 在 OOPS 中添加到 C 中——链接和其他内容几乎相同
    • 所以我生成.so文件就像我在问题中生成的那样,当我想使用它时,我必须使用你在答案中说的命令,对吧?
    猜你喜欢
    • 1970-01-01
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    相关资源
    最近更新 更多