Linux

unixODBC:無法載入 libmaodbc.so

  • June 17, 2020

我正在一個嵌入式平台上工作,我正在嘗試通過 unixODBC 訪問 MariaDB。嘗試測試與 isql 的連接我得到了響應:

isql -v mydsn myuser mypw
[01000][unixODBC][Driver Manager]Can't open lib '/usr/lib/libmaodbc.so' : file not found

文件 /usr/lib/libmaodbc.so 存在:

ls -la /usr/lib/libmaodbc.so
-r-xr-xr-x    1 root     root        403952 Sep  1 00:00 /usr/lib/libmaodbc.so

所以我認為必須缺少依賴項。

readelf -d /usr/lib/libmaodbc.so 

Dynamic section at offset 0x4fb54 contains 30 entries:
 Tag        Type                         Name/Value
0x00000001 (NEEDED)                     Shared library: [libodbcinst.so.2]
0x00000001 (NEEDED)                     Shared library: [libm.so.6]
0x00000001 (NEEDED)                     Shared library: [libc.so.6]
0x00000001 (NEEDED)                     Shared library: [ld-linux-armhf.so.3]
0x0000000e (SONAME)                     Library soname: [libmaodbc.so]
[...]

我重複了檢查 - 所有四個依賴項都存在。

接下來我編寫了一個測試程序,它嘗試載入共享對象:

#include <iostream>
#include <dlfcn.h>

void tryLoadSo(const std::string& path) {
   void* so = dlopen(path.c_str(), RTLD_NOW);
   if(so) {
       std::cout << "Loaded " << path << "!" << std::endl;
       dlclose(so);
   } else {
       std::cout << "Failed to load " << path << "!" << std::endl;
   }
}

int main() {
   tryLoadSo("/usr/lib/libmaodbc.so");
   tryLoadSo("/usr/lib/libodbcinst.so.2");
   tryLoadSo("/usr/lib/libm.so.6");
   tryLoadSo("/usr/lib/libc.so.6");
   tryLoadSo("/usr/lib/ld-linux-armhf.so.3");
}

輸出:

Failed to load /usr/lib/libmaodbc.so!
Loaded /usr/lib/libodbcinst.so.2!
Loaded /usr/lib/libm.so.6!
Loaded /usr/lib/libc.so.6!
Loaded /usr/lib/ld-linux-armhf.so.3!

因此可以載入所有依賴項,但 libmaodbc 不能。

共享對像是否未針對正確的平台進行配置?是的。我將它與 libodbcinst.so.2 進行了比較,以確保:

readelf -h /usr/lib/libmaodbc.so 
ELF Header:
 Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
 Class:                             ELF32
 Data:                              2's complement, little endian
 Version:                           1 (current)
 OS/ABI:                            UNIX - System V
 ABI Version:                       0
 Type:                              DYN (Shared object file)
 Machine:                           ARM
 Version:                           0x1
 Entry point address:               0x9a58
 Start of program headers:          52 (bytes into file)
 Start of section headers:          402952 (bytes into file)
 Flags:                             0x5000400, Version5 EABI, hard-float ABI
 Size of this header:               52 (bytes)
 Size of program headers:           32 (bytes)
 Number of program headers:         7
 Size of section headers:           40 (bytes)
 Number of section headers:         25
 Section header string table index: 24

readelf -h /usr/lib/libodbcinst.so.2
ELF Header:
 Magic:   7f 45 4c 46 01 01 01 00 00 00 00 00 00 00 00 00 
 Class:                             ELF32
 Data:                              2's complement, little endian
 Version:                           1 (current)
 OS/ABI:                            UNIX - System V
 ABI Version:                       0
 Type:                              DYN (Shared object file)
 Machine:                           ARM
 Version:                           0x1
 Entry point address:               0x1e60
 Start of program headers:          52 (bytes into file)
 Start of section headers:          53528 (bytes into file)
 Flags:                             0x5000400, Version5 EABI, hard-float ABI
 Size of this header:               52 (bytes)
 Size of program headers:           32 (bytes)
 Number of program headers:         6
 Size of section headers:           40 (bytes)
 Number of section headers:         24
 Section header string table index: 23

可能出了什麼問題?為什麼 libmaodbc.so 不會載入?

libmaodbc.so 與 libmariadb.so.3 靜態連結。現在我動態連結了 libmariadb.so.3 並且它起作用了。

不知道是否連結了錯誤/不匹配的庫或其他一些連結設置不正確(可能在 CMakeLists.txt 中出現問題?)。所以我仍然不明白根本原因。但是症狀是固定的。

引用自:https://unix.stackexchange.com/questions/516936