我複製粘貼了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?我如何解決這個問題?謝謝!
您的本地目錄不是Linux查找庫的文件夾之一。例如,將你的lib移動到'/ usr/lib'中,或者看一下[this SO answer](http://stackoverflow.com/questions/13428910/how-to-set-the-environmental-variable-ld-library -path-in-linux) – LPs
在你的dlopen調用中,如果你想從當前目錄加載庫,你需要指定相對路徑。 EX:'./ libdlo.so' – Ankur
重新命令鏈接命令:'gcc -rdynamic main.c -ldl' –