2017-05-02 71 views
0

我複製粘貼了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?我如何解決這個問題?謝謝!

+0

您的本地目錄不是Linux查找庫的文件夾之一。例如,將你的lib移動到'/ usr/lib'中,或者看一下[this SO answer](http://stackoverflow.com/questions/13428910/how-to-set-the-environmental-variable-ld-library -path-in-linux) – LPs

+0

在你的dlopen調用中,如果你想從當前目錄加載庫,你需要指定相對路徑。 EX:'./ libdlo.so' – Ankur

+0

重新命令鏈接命令:'gcc -rdynamic main.c -ldl' –

回答

2

OS不查找庫在您的本地目錄中,無論是

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,該文章深入探討了動態鏈接器鏈接的庫,即共享庫和動態鏈接庫。

相關問題