ld 未定义的引用,尽管找到了库并导出了符号

ld undefined reference, despite library found and symbols exported

现在断断续续与这个战斗了 48 小时;在尝试 link 一个动态库及其依赖项时,我仍然遇到未定义的引用错误 - 尽管所有导出都存在,并且库已成功找到。

场景:

libmemory 构建成功:

$ g++ -shared -fPIC -o ./builds/libmemory.so ...$(OBJECTS)...

libstring 编译成功,但编译失败 link:

$ gcc -shared -fPIC -o ./builds/libstring.so ...$(OBJECTS)... -L./builds -lmemory
./temp/libstring/string.o: In function `STR_duplicate':
string.c:(.text+0x1cb): undefined reference to `MEM_priv_alloc'
./temp/libstring/string.o: In function `STR_duplicate_replace':
string.c:(.text+0x2a0): undefined reference to `MEM_priv_free'
string.c:(.text+0x2bf): undefined reference to `MEM_priv_alloc'
/usr/bin/ld: ./builds/libstring.so: hidden symbol `MEM_priv_free' isn't defined
/usr/bin/ld: final link failed: Bad value
collect2: error: ld returned 1 exit status

正在验证 libmemory 导出其符号,并使用 -v 到 gcc 找到库本身:

...
attempt to open ./builds/libmemory.so succeeded
-lmemory (./builds/libmemory.so)
...

$ nm -gC ./builds/libmemory.so | grep MEM_
0000000000009178 T MEM_exit
0000000000009343 T MEM_init
00000000000093e9 T MEM_print_leaks
00000000000095be T MEM_priv_alloc
000000000000971d T MEM_priv_free
00000000000099c1 T MEM_priv_realloc
0000000000009d26 T MEM_set_callback_leak
0000000000009d3f T MEM_set_callback_noleak

$ objdump -T ./builds/libmemory.so | grep MEM_
0000000000009d3f g    DF .text  0000000000000019  Base        MEM_set_callback_noleak
00000000000093e9 g    DF .text  00000000000001d5  Base        MEM_print_leaks
0000000000009d26 g    DF .text  0000000000000019  Base        MEM_set_callback_leak
00000000000099c1 g    DF .text  0000000000000365  Base        MEM_priv_realloc
0000000000009343 g    DF .text  00000000000000a6  Base        MEM_init
00000000000095be g    DF .text  000000000000015f  Base        MEM_priv_alloc
000000000000971d g    DF .text  00000000000002a4  Base        MEM_priv_free
0000000000009178 g    DF .text  00000000000000a7  Base        MEM_exit

$ readelf -Ws ./builds/libmemory.so | grep MEM_
    49: 0000000000009d3f    25 FUNC    GLOBAL DEFAULT   11 MEM_set_callback_noleak
    95: 00000000000093e9   469 FUNC    GLOBAL DEFAULT   11 MEM_print_leaks
    99: 0000000000009d26    25 FUNC    GLOBAL DEFAULT   11 MEM_set_callback_leak
   118: 00000000000099c1   869 FUNC    GLOBAL DEFAULT   11 MEM_priv_realloc
   126: 0000000000009343   166 FUNC    GLOBAL DEFAULT   11 MEM_init
   145: 00000000000095be   351 FUNC    GLOBAL DEFAULT   11 MEM_priv_alloc
   192: 000000000000971d   676 FUNC    GLOBAL DEFAULT   11 MEM_priv_free
   272: 0000000000009178   167 FUNC    GLOBAL DEFAULT   11 MEM_exit
   103: 0000000000009343   166 FUNC    GLOBAL DEFAULT   11 MEM_init
   108: 0000000000009178   167 FUNC    GLOBAL DEFAULT   11 MEM_exit
   148: 0000000000009d3f    25 FUNC    GLOBAL DEFAULT   11 MEM_set_callback_noleak
   202: 00000000000095be   351 FUNC    GLOBAL DEFAULT   11 MEM_priv_alloc
   267: 000000000000971d   676 FUNC    GLOBAL DEFAULT   11 MEM_priv_free
   342: 0000000000009d26    25 FUNC    GLOBAL DEFAULT   11 MEM_set_callback_leak
   346: 00000000000099c1   869 FUNC    GLOBAL DEFAULT   11 MEM_priv_realloc
   366: 00000000000093e9   469 FUNC    GLOBAL DEFAULT   11 MEM_print_leaks

有没有我遗漏的非常简单的东西?所有其他与此相关的问题都有简单的答案,例如 link 库顺序和使用的路径 - 但我已经验证它们已到位并按预期工作。

修改 -fvisibility 也没有任何变化。

无论是使用 clang 还是 gcc,结果都是一样的。

Linux 3.16.0-38-generic gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)

您应该将 MEM_priv_alloc() 函数标记为 extern "C" 或将函数体包装到 extern "C" { /* function implementation */ } 中(从描述中可以看出已经完成)。 并在 header

中使用 __cplusplusextern "C" 的组合
#ifdef __cplusplus
   #define EXTERNC extern "C"
#else
   #define EXTERNC
#endif

EXTERNC int MEM_priv_alloc (void);

另请仔细检查 MEM_priv_alloc 原型是如何完成的。例如,MEM_priv_alloc 应该 而不是 inline(不幸的是我不知道这方面的物理学,但是 inline 这个例子对我来说失败了)


下面是适合我的简化示例:

文件:

$ ls
main.c  Makefile  mem.cpp  mem.h  strings.c  strings.h

mem.cpp

#include <stdio.h>
#include "mem.h"
extern "C" int MEM_priv_alloc (void)
{
   return 0;
}

mem.h

#ifdef __cplusplus
   #define EXTERNC extern "C"
#else
   #define EXTERNC
#endif
EXTERNC int MEM_priv_alloc (void);

strings.c:

#include <stdio.h>
#include "mem.h"
int STR_duplicate_replace (void)
{
   MEM_priv_alloc();
}

strings.h:

int STR_duplicate_replace (void);

main.c

#include <stdio.h>
#include "string.h"
int main (void)
{
   STR_duplicate_replace ();
   return 0;
}

生成文件:

prog: libmemory.so libstrings.so
        gcc -o $@ -L. -lmemory -lstrings main.c

libmemory.so: mem.cpp
        g++ -shared -fPIC -o $@ $^

libstrings.so: strings.c
        gcc -shared -fPIC -o $@ $^

和构建日志:

g++ -shared -fPIC -o libmemory.so mem.cpp
gcc -shared -fPIC -o libstrings.so strings.c
gcc -o prog -L. -lmemory -lstrings main.c

另请参阅更多详细信息 Using C++ library in C codewiki How to mix C and C++

所以,我剥离了合并的最后部分,并发现了问题。

我的 import/export 是仿照这个:https://gcc.gnu.org/wiki/Visibility

我的等效实现最终如下所示:

#     if GCC_IS_V4_OR_LATER
#             define DLLEXPORT        __attribute__((visibility("default")))
#             define DLLIMPORT        __attribute__((visibility("hidden")))
#     else
#             define DLLEXPORT
#             define DLLIMPORT
#     endif

DLLIMPORT(隐藏可见性)导致了问题;我用一个空白的定义替换它,就可以了。是的,我确实也有 clang 的等价物,这就是为什么它也以同样的方式失败的原因。

我从中得出的结论是,C 代码只看到这些可能的符号是隐藏的,因此无论多么努力都无法导入它们,无论它们实际存在多少!