加载的库无法打开共享对象文件:没有这样的文件或目录

问题描述 投票:0回答:2

我复制粘贴来自How would a loaded library function call a symbol in the main application?的代码,以帮助我理解加载的库如何工作。但是当我试图运行它时,它说它找不到文件,虽然当我执行ls时文件就在当前目录中

@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$ gcc -shared -olibdlo.so dlo.c
    /usr/bin/ld: /tmp/ccpGxAlo.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
    /tmp/ccpGxAlo.o: error adding symbols: Bad value
    collect2: error: ld returned 1 exit status
@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$ gcc -shared -fPIC -olibdlo.so dlo.c
@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$ gcc -ldl -rdynamic main.c
    /tmp/ccHtUgDf.o: In function `main':
    main.c:(.text+0x2b): undefined reference to `dlopen'
    main.c:(.text+0x3b): undefined reference to `dlerror'
    main.c:(.text+0x66): undefined reference to `dlerror'
    main.c:(.text+0x7b): undefined reference to `dlsym'
    main.c:(.text+0x83): undefined reference to `dlerror'
    main.c:(.text+0xc7): undefined reference to `dlclose'
    collect2: error: ld returned 1 exit status
@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$ gcc -Wl,--no-as-needed -ldl -rdynamic main.c
@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$ ls
    AI_A.c  AI_B.c  AI_C.c  a.out  dlo.c  Game.c  Game.h  libdlo.so  main.c  runGame.c
@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$ ./a.out
    libdlo.so: cannot open shared object file: No such file or directory
@APG9591:/mnt/c/Users/fried/Desktop/KI3/Game$

libdlo.so:无法打开共享对象文件:没有这样的文件或目录

为什么程序找不到文件libdlo.so?我该如何解决这个问题?谢谢!

c dlopen
2个回答
2
投票

操作系统也不会在本地目录中查找库

1)将你的lib移动到/usr/lib

2)在dlopen调用中指定相对路径"./libdlo.so"

.

感谢lps和ankur的答案


0
投票

在这个post中查看我的答案。我最近遇到了同样的问题,肯定会有所帮助。

阅读完之后,我建议你使用-rpath选项。

只是为了好玩,你也可以使用ldconfig $(pwd)解决这个问题,我认为有时这种方法更好。

有关更多信息,请阅读the linux how-to documentation on library,它讲述了很多关于静态库,共享库和动态链接库的知识。

有关越来越多的信息,请阅读Better understanding Linux secondary dependencies solving with examples,它深入探讨了通过动态链接器链接的库,即共享库和动态链接库。

© www.soinside.com 2019 - 2024. All rights reserved.