g ++链接问题

时间:2010-10-30 15:57:43

标签: g++ shared-libraries

我有一个奇怪的链接问题,我只是开始使用c ++进行编程所以我不太清楚这是什么意思......

main.cpp

#include <iostream>

main(void)
{
 return 0;
}

编译

esr@athena:~/programming/cpp$ g++ main.cpp 
esr@athena:~/programming/cpp$ ./a.out 
esr@athena:~/programming/cpp$ g++ main.cpp -L/home/esr/ogre/lib -lOgreMain
esr@athena:~/programming/cpp$ ./a.out 

当我链接到同一文件夹中的另一个库时,这执行得很好......

esr@athena:~/programming/cpp$ g++ main.cpp -L/home/esr/ogre/lib -lOgreMain -lOgreTerrain
esr@athena:~/programming/cpp$ ./a.out 
./a.out: error while loading shared libraries: libOgreTerrain.so.1.7.1: cannot open shared object file: No such file or directory

那个文件夹里有什么?

esr@athena:~/programming/cpp$ ls /home/esr/ogre/lib/
libOgreMain.so                  Sample_DynTex.so
libOgreMain.so.1.7.1           Sample_FacialAnimation.so
libOgrePaging.so                Sample_Fresnel.so
libOgrePaging.so.1.7.1          Sample_Grass.so
libOgreRTShaderSystem.so        Sample_Instancing.so
libOgreRTShaderSystem.so.1.7.1  Sample_Isosurf.so
libOgreTerrain.so               Sample_Lighting.so
***libOgreTerrain.so.1.7.1***         Sample_Ocean.so

编辑:(星星是强调,而不是文件名) 它就在那里!关于我做错了什么想法?

修改

还有问题。尝试链接libOgreTerrain的所有依赖项,并且似乎不满足编译器。

ldd libOgreTerrain.so
    linux-vdso.so.1 =>  (0x00007fff56bff000)
    libOgreMain.so.1.7.1 => /home/esr/ogre/lib/libOgreMain.so.1.7.1 (0x00007fd3584c8000)
    libOgrePaging.so.1.7.1 => /home/esr/ogre/lib/libOgrePaging.so.1.7.1 (0x00007fd35829b000)
    libfreetype.so.6 => /usr/lib/libfreetype.so.6 (0x00007fd357fee000)
    libSM.so.6 => /usr/lib/libSM.so.6 (0x00007fd357de5000)
    libICE.so.6 => /usr/lib/libICE.so.6 (0x00007fd357bca000)
    libX11.so.6 => /usr/lib/libX11.so.6 (0x00007fd357893000)
    libXext.so.6 => /usr/lib/libXext.so.6 (0x00007fd357681000)
    libXt.so.6 => /usr/lib/libXt.so.6 (0x00007fd35741c000)
    libXaw.so.7 => /usr/lib/libXaw.so.7 (0x00007fd3571ac000)
    libpthread.so.0 => /lib/libpthread.so.0 (0x00007fd356f8f000)
    libdl.so.2 => /lib/libdl.so.2 (0x00007fd356d8b000)
    libfreeimage.so.3 => /usr/lib/libfreeimage.so.3 (0x00007fd3568bc000)
    libzzip-0.so.13 => /usr/lib/libzzip-0.so.13 (0x00007fd3566b5000)
    libz.so.1 => /lib/libz.so.1 (0x00007fd35649d000)
    libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007fd356196000)
    libm.so.6 => /lib/libm.so.6 (0x00007fd355f13000)
    libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007fd355cfd000)
    libc.so.6 => /lib/libc.so.6 (0x00007fd355979000)
    libuuid.so.1 => /lib/libuuid.so.1 (0x00007fd355774000)
    libxcb.so.1 => /usr/lib/libxcb.so.1 (0x00007fd355556000)
    libXmu.so.6 => /usr/lib/libXmu.so.6 (0x00007fd35533d000)
    libXpm.so.4 => /usr/lib/libXpm.so.4 (0x00007fd35512c000)
    libXau.so.6 => /usr/lib/libXau.so.6 (0x00007fd354f28000)
    libXdmcp.so.6 => /usr/lib/libXdmcp.so.6 (0x00007fd354d22000)

将编译代码更改为以下内容:

g++ main.cpp -L/home/esr/ogre/lib -lOgreMain -lOgreTerrain -L/lib -L/usr/lib -lfreetype -lSM -lICE -lX11 -lXext -lXt -lXaw -lpthread -lfreeimage  -lxcb -lXmu -lXpm -lXau -lXdmcp -ldl -lzzip -lz -lstdc++ -lm -lgcc_s -lc 

与上述afaik相关的所有依赖关系链接(uuid不会链接)。

esr@athena:~/programming/cpp$ g++ main.cpp -L/home/esr/ogre/lib -lOgreMain -lOgreTerrain -L/lib -L/usr/lib -lfreetype -lSM -lICE -lX11 -lXext -lXt -lXaw -lpthread -lfreeimage  -lxcb -lXmu -lXpm -lXau -lXdmcp -ldl -lzzip -lz -lstdc++ -lm -lgcc_s -lc 
esr@athena:~/programming/cpp$ ./a.out 
./a.out: error while loading shared libraries: libOgreTerrain.so.1.7.1: cannot open shared object file: No such file or directory
esr@athena:~/programming/cpp$ LD_LIBRARY_PATH=/home/esr/ogre/lib ./a.out
esr@athena:~/programming/cpp$ 

编辑2

链接a.out

esr@athena:~/programming/cpp$ ldd a.out 
    linux-vdso.so.1 =>  (0x00007fff978a4000)
    libOgreMain.so.1.7.1 => /usr/lib/libOgreMain.so.1.7.1 (0x00007f612d528000)
    libOgreTerrain.so.1.7.1 => not found
    libfreetype.so.6 => /usr/lib/libfreetype.so.6 (0x00007f612d2a0000)
    libSM.so.6 => /usr/lib/libSM.so.6 (0x00007f612d097000)
    libICE.so.6 => /usr/lib/libICE.so.6 (0x00007f612ce7c000)
    libX11.so.6 => /usr/lib/libX11.so.6 (0x00007f612cb45000)
    libXext.so.6 => /usr/lib/libXext.so.6 (0x00007f612c933000)
    libXt.so.6 => /usr/lib/libXt.so.6 (0x00007f612c6ce000)
    libXaw.so.7 => /usr/lib/libXaw.so.7 (0x00007f612c45e000)
    libpthread.so.0 => /lib/libpthread.so.0 (0x00007f612c241000)
    libfreeimage.so.3 => /usr/lib/libfreeimage.so.3 (0x00007f612bd73000)
    libxcb.so.1 => /usr/lib/libxcb.so.1 (0x00007f612bb55000)
    libXmu.so.6 => /usr/lib/libXmu.so.6 (0x00007f612b93c000)
    libXpm.so.4 => /usr/lib/libXpm.so.4 (0x00007f612b72b000)
    libXau.so.6 => /usr/lib/libXau.so.6 (0x00007f612b527000)
    libXdmcp.so.6 => /usr/lib/libXdmcp.so.6 (0x00007f612b321000)
    libdl.so.2 => /lib/libdl.so.2 (0x00007f612b11d000)
    libzzip-0.so.13 => /usr/lib/libzzip-0.so.13 (0x00007f612af15000)
    libz.so.1 => /lib/libz.so.1 (0x00007f612acfd000)
    libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x00007f612a9f7000)
    libgcc_s.so.1 => /lib/libgcc_s.so.1 (0x00007f612a7e0000)
    libm.so.6 => /lib/libm.so.6 (0x00007f612a55d000)
    libc.so.6 => /lib/libc.so.6 (0x00007f612a1da000)
    libuuid.so.1 => /lib/libuuid.so.1 (0x00007f6129fd4000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f612dcfc000)

分辨率

因此部分解决了,我链接到/ usr / lib文件夹中的共享库,我不确定为什么g ++不会在那里看,但它不会。

有用的链接herehere

1 个答案:

答案 0 :(得分:3)

这很简单。

操作系统在ldconfig中的目录中查找库。

似乎libOgreTerrain.so链接了一些未明确指定的其他库。

首先尝试:

LD_LIBRARY_PATH=/home/esr/ogre/lib ./a.out

您可以显示动态链接路径:

ldd ./a.out