2015-10-06 01:58
浏览 97


I'm trying to make a C api around a C++ library, so that I can later wrap it in Golang. I started by simply generating a dylib with one function so that I have a reference to look at. Then I made a wrapper around the actual library I wanted to use. When I generated all the symbols from the simple dylib i got this:

MacbookMainframe:c hydroflame$ nm -a clib/libxyz.dylib 
0000000000000f90 T _Hello
                 U dyld_stub_binder

and I only declared a single function named Hello, so far so good

When I did what i thought was equivalent for the actual library, the go wrapper wouldn't compile and the symbols generated where

MacbookMainframe:c hydroflame$ nm -a ../ 
                 U _SteamAPI_Init
0000000000000f60 T __Z14SteamCAPI_Initv
                 U dyld_stub_binder

The symbol I was expecting was _SteamCAPI_Init (with the underscore because apparently Hello generated _Hello but instead i get something weirdish.

Is my compilation wrong, or is that the normal symbol that should be generated ?

The source files are available here (theres only like 30 important lines):

EDIT (for future readers):

my problem at the time of writing was that my header file declaration had the extern "C" { but my source file did not, so gcc would mangle the names anyway and cgo wouldn't find it.

MacbookMainframe:steamc hydroflame$ nm -a libsteam.dylib 
                 U _SteamAPI_Init
0000000000000f60 T _SteamCAPI_Init
                 U dyld_stub_binder

图片转代码服务由CSDN问答提供 功能建议

我正在尝试围绕C ++库制作C api,以便以后将其包装在Golang中。 我首先简单地用一个函数生成一个dylib,以便我参考一下。 然后,我对要使用的实际库进行了包装。 当我从简单的dylib生成所有符号时,我得到了:

  MacbookMainframe:c hydroflame $ nm -a clib / libxyz.dylib 
0000000000000f90 T _Hello 
 U dyld_stub_binder \  n   

,而我只声明了一个名为 Hello 的函数,到目前为止效果很好


  MacbookMainframe:c hydroflame $ nm -a ../luxengine生成符号。  net / steamc / libsteam.dylib 
 U _SteamAPI_Init 
0000000000000f60 T __Z14SteamCAPI_Initv 
 U dyld_stub_binder 

我期望的符号是 _SteamCAPI_Init (带有下划线,因为显然 Hello 生成了 _Hello ,但相反,我得到了一些奇怪的东西。

我的编译错误了,还是 应该生成的普通符号?

源文件位于此处(t 这里只喜欢30条重要的代码):


我在撰写本文时的问题是我的头文件声明具有 extern“ C” { 但是我的源文件没有,所以gcc还是会弄乱名称,而cgo找不到它。

  MacbookMainframe:steamc hydroflame $ nm -a libsteam.dylib 
 U  _SteamAPI_Init 
0000000000000f60 T _SteamCAPI_Init 
 U dyld_stub_binder 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dongxunhua2054 2015-10-06 02:59

    Firstly, dyld_stub_binder is the default generated symbol when you compile C++. You don't need to care about it.

    Secondly, __Z14SteamCAPI_Initv is actually the right symbol. Since C++ supports overloading, C++ functions are compiled with mangled symbol names, so that the functions name won't clash with each other. For example, you got two functions void do_something(int a) and void do_something(int a, int b), if the function names are not mangled, how would the linker resolve symbol names.

    Info about C++ name mangling can be found here.

    打赏 评论

相关推荐 更多相似问题