Strange character name in nm command output

I created a dynamic lib called InterfaceLayer.so when I call nm InterfaceLayer, I get a symbol that:

00000e28 T _Z5startv 

but I expected it to be just the โ€œbeginningโ€ as the name of the function defined in the code. This is bad, because when I try to open with dlsym (), it will not work.

Any clues?

Tkz

+4
source share
3 answers

What because of C ++ name change

 nm -C 

unmakes them.

To prevent a name change,

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

.

my.h

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

This will give them the "C" link, that is, they cannot be overloaded, cannot follow the link, nothing C ++ :)

+8
source

Sounds like a C ++ name mangling .

+2
source

As mentioned in other answers, this is probably due to a C ++ name change. If you want a character to be accessible with this "unmangled" name and implemented in C ++, you will need extern "C" to tell the C ++ compiler that it has a C link.

In the header, which has a function prototype, you need something like:

 #if defined(__cplusplus) extern "C" { #endif // the prototype for start()... #if defined(__cplusplus) } #endif 

This ensures that if the function is used by the C ++ compiler, it will receive extern "C" in the declaration and that if it is used by the C module, it will not be confused with the extern "C" qualifier.

Implementation in a .cpp file is not necessary if you include a header before defining a function. He will use the binding specification that he saw from the previous declaration. However, I still prefer to decorate the function definition with extern "C" just to keep everything in sync (note that in the .cpp file you don't need #ifdef - it will always compile as C ++.

+1
source

All Articles