Strange symbol name in output of nm command

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

Tags:

Linux

C

Linker

Nm