c - 加载的库无法打开共享对象文件 : No such file or directory

标签 c dlopen

我从 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?我该如何解决这个问题?谢谢!

最佳答案

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

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

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

.

感谢 lps 和 ankur 的解答

关于c - 加载的库无法打开共享对象文件 : No such file or directory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43731190/

相关文章:

c++ - 在 C++ 中使用动态加载重新加载库

c - 使用 dlopen() 加载时解决动态库依赖关系

c - Windows 函数 ReadFile 一直返回 FALSE,不知道为什么

C 代码在满足 first 和 last = middle 的条件总和的区间中查找 3 位数字

c# - 保证CPU响应?

linux - 如何解析 dlopen 库中的外部变量

c - 使用 MinGW 防止在 C(而非 C++)中进行名称重整以进行动态符号搜索

c - C中的矩阵排序图形

c - GCC SSE代码优化

android-ndk - 无法在运行时在 android 应用程序中加载库