Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ linux中共享库名称的理解_C++_Linux_Shared Libraries - Fatal编程技术网

C++ linux中共享库名称的理解

C++ linux中共享库名称的理解,c++,linux,shared-libraries,C++,Linux,Shared Libraries,我读过linux文档项目,但仍然不明白soname和real name的确切用途 我有三个文件:main.cpp、header.h和test.cpp。我已经编写了以下生成文件: bin: main.o test.so g++ -o bin main.o -ltest -Wl,-rpath /usr/local/lib main.o:main.cpp g++ -c main.cpp test.so: test.o g++ -shared -o libtest.so tes

我读过linux文档项目,但仍然不明白
soname
real name
的确切用途

我有三个文件:
main.cpp
header.h
test.cpp
。我已经编写了以下生成文件:

bin: main.o test.so
    g++ -o bin main.o -ltest  -Wl,-rpath /usr/local/lib
main.o:main.cpp
    g++ -c main.cpp
test.so: test.o
    g++ -shared -o libtest.so test.o
test.o: test.cpp
    g++ -fPIC -c test.cpp
clean:
    rm -f *.o *.so* bin
bin: main.o test.so
    g++ -o bin main.o -L. -ltest  -Wl,-rpath .
main.o:main.cpp
    g++ -c main.cpp
test.so: test.o
    g++ -shared -Wl,soname,-litest.so.1 -o libtest.so.1.0.1 test.o
test.o: test.cpp
    g++ -fPIC -c test.cpp
clean:
    rm -f *.o *.so* bin
我已经运行了这个make文件,我已经将libtest.so复制到
/usr/local/lib
中,一切正常。但是我不明白在我的情况下,
soname
real name
是什么

让我们重写这个生成文件:

bin: main.o test.so
    g++ -o bin main.o -ltest  -Wl,-rpath /usr/local/lib
main.o:main.cpp
    g++ -c main.cpp
test.so: test.o
    g++ -shared -o libtest.so test.o
test.o: test.cpp
    g++ -fPIC -c test.cpp
clean:
    rm -f *.o *.so* bin
bin: main.o test.so
    g++ -o bin main.o -L. -ltest  -Wl,-rpath .
main.o:main.cpp
    g++ -c main.cpp
test.so: test.o
    g++ -shared -Wl,soname,-litest.so.1 -o libtest.so.1.0.1 test.o
test.o: test.cpp
    g++ -fPIC -c test.cpp
clean:
    rm -f *.o *.so* bin
但它不起作用。导致以下错误:

/usr/bin/ld: cannot find -ltest
collect2: error: ld returned 1 exit status
但它不起作用。导致以下错误:/usr/bin/ld:找不到-ltest

看来你选择错了

1) -rpath用于运行库搜索路径。所有-rpath参数都被连接并传递给运行时链接器ld。因此请参见
man ld.so

2) -L表示链接程序或共享库时的
ld
(来自
man-ld
:All-L选项适用于All-L选项)

因此,在构建时,您需要使用-L告诉链接器
ld
在哪里可以找到库(请记住-
ld。因此,启动程序时使用
,链接程序时使用
ld
):


@你能读懂我的主题吗**我读过linux文档项目,但仍然不明白soname和RealName的确切用途**如何指定共享库的名称、实名和版本?由
g++-shared-o libtest.so test.o
1)创建的库的版本是什么?如何指定共享库的名称、实名和版本文档中的示例:
gcc-shared-Wl,-soname,libmystuff.so.1-o libmystuff.so.1.0.1 a.o b.o
。Soname-libmystuff.so.1,RealName-libmystuff.so.1.0.1,其接口的版本为1(因为Soname在so:libmystuff.so.1之后有1),库的版本为1.0.1。2) 在您的示例中,soname中没有libtest.so的版本。