对 `dp_lib_init()` 的未定义引用

undefined reference to `dp_lib_init()`

我正在尝试使用此 productLinux TTY Driver Ver 1.0.15.0

我完成了 README_Ubuntu 中的所有步骤,将头文件复制到我的项目目录中。

但是编译失败,报错

/tmp/ccH3NfSt.o: In function `main':
main.cpp:(.text+0x5): undefined reference to `dp_lib_init()'
collect2: error: ld returned 1 exit status

为了编译我的main.cpp,我使用了

$ g++ -L/usr/lib -lsdxio main.cpp

// Same result
$ g++ -lsdxio main.cpp

// Same result
$ g++ -L-lsdxio main.cpp

我的main.cpp是,

#include <iostream>
#include "sdxmng.h"
#include "sdxsio.h"

int main()
{
    dp_lib_init();

    return 0;
}

我确认共享库在 /usr/lib

-rwxr-xr-x 1 root root 261392 Sep 13 10:53 libsdxio.so
-rwxr-xr-x 1 root root 261392 Sep 13 20:34 libsdxio.so1.0.9.0
-rwxr-xr-x 1 root root 261392 Sep 13 10:53 libsdxio.so.9

一些有用的信息

$ g++ -L/usr/lib -lsdxio main.cpp -Xlinker --verbose

    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o succeeded
    /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crt1.o
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o succeeded
    /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crti.o
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o succeeded
    /usr/lib/gcc/x86_64-linux-gnu/5/crtbegin.o
    attempt to open /usr/lib/libsdxio.so succeeded
    -lsdxio (/usr/lib/libsdxio.so)
    attempt to open /tmp/ccKEcpaB.o succeeded
    /tmp/ccKEcpaB.o
    attempt to open /usr/lib/libstdc++.so failed
    attempt to open /usr/lib/libstdc++.a failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.so succeeded
    -lstdc++ (/usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.so)
    attempt to open /usr/lib/libm.so failed
    attempt to open /usr/lib/libm.a failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libm.so failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libm.a failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libm.so succeeded
    opened script file /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libm.so
    opened script file /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libm.so
    attempt to open /lib/x86_64-linux-gnu/libm.so.6 succeeded
    /lib/x86_64-linux-gnu/libm.so.6
    attempt to open /usr/lib/x86_64-linux-gnu/libmvec_nonshared.a succeeded
    attempt to open /lib/x86_64-linux-gnu/libmvec.so.1 succeeded
    /lib/x86_64-linux-gnu/libmvec.so.1
    attempt to open /usr/lib/libgcc_s.so failed
    attempt to open /usr/lib/libgcc_s.a failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libgcc_s.so succeeded
    -lgcc_s (/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_s.so)
    attempt to open /usr/lib/libgcc.so failed
    attempt to open /usr/lib/libgcc.a failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libgcc.so failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libgcc.a succeeded
    attempt to open /usr/lib/libc.so failed
    attempt to open /usr/lib/libc.a failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libc.so failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libc.a failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libc.so succeeded
    opened script file /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libc.so
    opened script file /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/libc.so
    attempt to open /lib/x86_64-linux-gnu/libc.so.6 succeeded
    /lib/x86_64-linux-gnu/libc.so.6
    attempt to open /usr/lib/x86_64-linux-gnu/libc_nonshared.a succeeded
    (/usr/lib/x86_64-linux-gnu/libc_nonshared.a)elf-init.oS
    attempt to open /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 succeeded
    /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
    /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
    attempt to open /usr/lib/libgcc_s.so failed
    attempt to open /usr/lib/libgcc_s.a failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libgcc_s.so succeeded
    -lgcc_s (/usr/lib/gcc/x86_64-linux-gnu/5/libgcc_s.so)
    attempt to open /usr/lib/libgcc.so failed
    attempt to open /usr/lib/libgcc.a failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libgcc.so failed
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/libgcc.a succeeded
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/crtend.o succeeded
    /usr/lib/gcc/x86_64-linux-gnu/5/crtend.o
    attempt to open /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crtn.o succeeded
    /usr/lib/gcc/x86_64-linux-gnu/5/../../../x86_64-linux-gnu/crtn.o
    libm.so.6 needed by /usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.so
    found libm.so.6 at /lib/x86_64-linux-gnu/libm.so.6
    ld-linux-x86-64.so.2 needed by /usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.so
    found ld-linux-x86-64.so.2 at /lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
    libgcc_s.so.1 needed by /usr/lib/gcc/x86_64-linux-gnu/5/libstdc++.so
    found libgcc_s.so at /usr/lib/gcc/x86_64-linux-gnu/5/libgcc_s.so
    /tmp/ccKEcpaB.o: In function `main':
    main.cpp:(.text+0x5): undefined reference to `dp_lib_init()'
    collect2: error: ld returned 1 exit status
$ ld -lsdxio --verbose

    ==================================================
    attempt to open //usr/local/lib/x86_64-linux-gnu/libsdxio.so failed
    attempt to open //usr/local/lib/x86_64-linux-gnu/libsdxio.a failed
    attempt to open //lib/x86_64-linux-gnu/libsdxio.so failed
    attempt to open //lib/x86_64-linux-gnu/libsdxio.a failed
    attempt to open //usr/lib/x86_64-linux-gnu/libsdxio.so failed
    attempt to open //usr/lib/x86_64-linux-gnu/libsdxio.a failed
    attempt to open //usr/local/lib64/libsdxio.so failed
    attempt to open //usr/local/lib64/libsdxio.a failed
    attempt to open //lib64/libsdxio.so failed
    attempt to open //lib64/libsdxio.a failed
    attempt to open //usr/lib64/libsdxio.so failed
    attempt to open //usr/lib64/libsdxio.a failed
    attempt to open //usr/local/lib/libsdxio.so failed
    attempt to open //usr/local/lib/libsdxio.a failed
    attempt to open //lib/libsdxio.so failed
    attempt to open //lib/libsdxio.a failed
    attempt to open //usr/lib/libsdxio.so succeeded
    -lsdxio (//usr/lib/libsdxio.so)
    libm.so.6 needed by //usr/lib/libsdxio.so
    found libm.so.6 at //lib/x86_64-linux-gnu/libm.so.6
    libpthread.so.0 needed by //usr/lib/libsdxio.so
    found libpthread.so.0 at //lib/x86_64-linux-gnu/libpthread.so.0
    libc.so.6 needed by //usr/lib/libsdxio.so
    found libc.so.6 at //lib/x86_64-linux-gnu/libc.so.6
    ld-linux-x86-64.so.2 needed by //lib/x86_64-linux-gnu/libm.so.6
    found ld-linux-x86-64.so.2 at //lib/x86_64-linux-gnu/ld-linux-x86-64.so.2
    ld: warning: cannot find entry symbol _start; not setting start address
$ nm libsdxio.so
    [...]
    0000000000007d19 T dp_lib_free
    0000000000007beb T dp_lib_init
    0000000000007e21 T dp_mapping
    [...]

这只是猜测,但您似乎包含了 C++ 源文件中的 C 头文件(例如 #include "sdxsio.h")。

除了您的 C++ 编译器默认 mangle 函数名称外,它通常运行良好。但是二进制文件可能是由 C 编译器编译的,默认情况下名称未被破坏。

因此即使 libsdxio.so 包含 dp_lib_init,您的 C++ compiler/linker 将搜索该名称的损坏版本,但无济于事。

解决方案很简单canonical(但很丑):

#ifdef __cplusplus
extern "C" {
#endif
#include "sdxmng.h"
#include "sdxsio.h"
#ifdef __cplusplus
}
#endif