链接到外部库时符号名称中的下划线

发布于 2024-12-27 23:24:42 字数 1290 浏览 5 评论 0原文

我正在尝试编译一个 C 库,然后一些外部 C 代码喜欢它,并且我的外部代码在库中查找符号时遇到问题。我看不懂这部大片,希望有人能帮忙。详细信息如下:

1) 我正在 Mac 上工作。我已在 xcode 中将该库编译为静态 .a 库。

2)我正在尝试编译从库中调用函数的外部代码。我已经包含了头文件,并在 gcc 调用中指定了它的位置和库。编译似乎完成正常,但链接失败,提示找不到符号。

3)缺少的符号被列为_FunctionName,其中FunctionName是我调用的函数的名称。

我不清楚为什么编译器/链接器在我的函数名称中添加了下划线。但我天真的猜测是这就是为什么在库中找不到这些符号的原因。

外部代码编译是通过 MATLAB 的 mex() 函数完成的,该函数在后台进行下面的 gcc 调用。

我会很感激任何人可能有的任何想法。

gcc-4.2 -c  -Igsf_0303/ -I/Applications/MATLAB_R2011b.app/extern/include \
-DMATLAB_MEX_FILE -fno-common -no-cpp-precomp -arch x86_64 -isysroot \
/Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.5  -fexceptions  \
-DMX_COMPAT_32 -O2 -DNDEBUG  "gsf_tester.c"

gcc-4.2 -O -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl, \
-syslibroot,/Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.5 -bundle -Wl,\
-exported_symbols_list,/Applications/MATLAB_R2011b.app/extern/lib/maci64/mexFunction.map \
-o  "gsf_tester.mexmaci64"  gsf_tester.o  gsf_0303/libgsf.a \
-L/Applications/MATLAB_R2011b.app/bin/maci64 -lmx -lmex -lmat -lstdc++

Undefined symbols:
  "_gsfOpen", referenced from:
      _mexFunction in gsf_tester.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

mex: link of ' "gsf_tester.mexmaci64"' failed.

I'm trying to compile a C library and then some external C code liked against it and I'm having issues with my external code finding the symbols within the library. I don't understand the big flick and am hoping someone can help. Here are the details:

1) I'm working on a Mac. I've compiled the library as a static .a library in xcode.

2) I'm attempting to compile external code calling functions from the library. I've included the header file and I'm specifying it's location and the library in the gcc call. The compilation seems to complete ok, but the linking fails stating that symbols cannot be found.

3) The missing symbols are listed as _FunctionName where FunctionName is the name of the function I called.

It is not clear to me why the compiler/linker has added underbars to my function names. But my naive guess is that is why the symbols can not be found in the library.

The external code compilation is being done through MATLAB's mex() function which is making the gcc calls below in the background.

I'd be thankful for any thoughts anyone might have.

gcc-4.2 -c  -Igsf_0303/ -I/Applications/MATLAB_R2011b.app/extern/include \
-DMATLAB_MEX_FILE -fno-common -no-cpp-precomp -arch x86_64 -isysroot \
/Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.5  -fexceptions  \
-DMX_COMPAT_32 -O2 -DNDEBUG  "gsf_tester.c"

gcc-4.2 -O -Wl,-twolevel_namespace -undefined error -arch x86_64 -Wl, \
-syslibroot,/Developer/SDKs/MacOSX10.6.sdk -mmacosx-version-min=10.5 -bundle -Wl,\
-exported_symbols_list,/Applications/MATLAB_R2011b.app/extern/lib/maci64/mexFunction.map \
-o  "gsf_tester.mexmaci64"  gsf_tester.o  gsf_0303/libgsf.a \
-L/Applications/MATLAB_R2011b.app/bin/maci64 -lmx -lmex -lmat -lstdc++

Undefined symbols:
  "_gsfOpen", referenced from:
      _mexFunction in gsf_tester.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

mex: link of ' "gsf_tester.mexmaci64"' failed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

極樂鬼 2025-01-03 23:24:42

这是一个猜测。首先,约阿希姆的评论是正确的。 C 函数名称前面总是带有下划线。所以这不是问题。

因此,要么 gfsOpen() 从库中丢失,要么它不可见,或者在链接 gfs_tester.o 时看不到它。

因此检查 gfsOpen 是否在库中。 上面的代码反

otool -t -v sgsf_0303/libgsf.a | grep gsfOpen

汇编了该文件,然后从中 grep 出您感兴趣的符号。可能有更好的方法,但我没有费心去研究。

检查它是否声明为静态

确保库的架构匹配 (otool -fv)

Here's a guess. Firstly, Joachim's comment is correct. C function names will always be prepended with an underscore. So that's not the issue.

So either gfsOpen() is missing from the library or it's not visible or it can't be seen when gfs_tester.o is linked.

So check that gfsOpen is in the library. This should do it

otool -t -v sgsf_0303/libgsf.a | grep gsfOpen

The above disassembles the file and then greps the symbol you are interested in from it. There's probably a better way, but I haven't bothered to research it.

Check it's not declared static.

Make sure the architecture of the library matches (otool -fv)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文