开发者

Strange symbol name in output of nm command

I built a dynamic lib called InterfaceLayer.so. When I call:

> nm InterfaceLayer

As output, I get some symbols that look like:

00000e28  T _Z5startv

while I was expecting it to be "start", just as the name of the function I defined开发者_高级运维 in the code.

Why does this happen?


That's because of C++ name mangling

nm -C

demangles them.

To prevent name mangling,

  • use a C compiler (gcc, not g++), name your source file .c (not .cpp)
  • or declare extern "C":

.

my.h

  extern "C" 
  {
        void start();
        void finish();
  }

This will give them "C" linkage, meaning they can't be overloaded, cannot pass by reference, nothing c++ :)


Sounds like C++ name mangling.


As other answers have mentioned, this is likely becuase of C++ name mangling. If you want the symbol to be accessible by it's 'unmangled' name, and it's implemented in C++, you'll need to us extern "C" to tell the C++ compiler that it has a C linkage.

In the header that has the function prototype, you'll want something like:

#if defined(__cplusplus)
extern "C" {
#endif

// the prototype for start()...


#if defined(__cplusplus)
}
#endif

This will ensure that if the function is used by a C++ compiler, it'll get the extern "C" on the declaration, and that if it's used by a C module, it won't be confused by the extern "C" specifier.

You implementation in the .cpp file doesn't need that stuff if you include the header before the function definition. It'll use the linkage specification it saw from the previous declaration. However, I prefer to still decorate the function definition with extern "C" just to ensure that everything is in sync (note that in the .cpp file you don't need the #ifdef preprocessing stuff - it'll always be compiled as C++.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜