nm命令输出中的奇怪符号名称

时间:2021-07-12 04:54:53

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

我构建了一个名为InterfaceLayer.so的动态库。我打电话的时候:

> 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.

虽然我期待它是“开始”,就像我在代码中定义的函数的名称一样。

Any clues?

Tkz

3 个解决方案

#1


10  

That's because of C++ name mangling

那是因为C ++名称错误

nm -C

demangles them.

To prevent name mangling,

为了防止名称损坏,

  • use a C compiler (gcc, not g++), name your source file .c (not .cpp)
  • 使用C编译器(gcc,而不是g ++),命名源文件.c(不是.cpp)

  • or declare extern "C":
  • 或声明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++ :)

这会给他们“C”联动,意思是他们不能超载,不能通过引用传递,没什么c ++ :)

#2


3  

Sounds like C++ name mangling.

听起来像C ++名称错误。

#3


2  

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.

正如其他答案所提到的,这很可能是因为C ++名称错误。如果你希望符号可以通过它的'unmangled'名称访问,并且它是用C ++实现的,那么你需要我们extern“C”告诉C ++编译器它有一个C链接。

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.

这将确保如果C ++编译器使用该函数,它将在声明中获得extern“C”,并且如果它被C模块使用,它将不会被extern“C”说明符混淆。

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++.

如果在函数定义之前包含标头,则.cpp文件中的实现不需要那些东西。它将使用它从之前的声明中看到的链接规范。但是,我更喜欢用extern“C”来装饰函数定义只是为了确保一切都是同步的(请注意,在.cpp文件中你不需要#ifdef预处理的东西 - 它总是被编译为C ++ 。

#1


10  

That's because of C++ name mangling

那是因为C ++名称错误

nm -C

demangles them.

To prevent name mangling,

为了防止名称损坏,

  • use a C compiler (gcc, not g++), name your source file .c (not .cpp)
  • 使用C编译器(gcc,而不是g ++),命名源文件.c(不是.cpp)

  • or declare extern "C":
  • 或声明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++ :)

这会给他们“C”联动,意思是他们不能超载,不能通过引用传递,没什么c ++ :)

#2


3  

Sounds like C++ name mangling.

听起来像C ++名称错误。

#3


2  

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.

正如其他答案所提到的,这很可能是因为C ++名称错误。如果你希望符号可以通过它的'unmangled'名称访问,并且它是用C ++实现的,那么你需要我们extern“C”告诉C ++编译器它有一个C链接。

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.

这将确保如果C ++编译器使用该函数,它将在声明中获得extern“C”,并且如果它被C模块使用,它将不会被extern“C”说明符混淆。

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++.

如果在函数定义之前包含标头,则.cpp文件中的实现不需要那些东西。它将使用它从之前的声明中看到的链接规范。但是,我更喜欢用extern“C”来装饰函数定义只是为了确保一切都是同步的(请注意,在.cpp文件中你不需要#ifdef预处理的东西 - 它总是被编译为C ++ 。