DOC HOME SITE MAP MAN PAGES GNU INFO SEARCH PRINT BOOK
 
C and C++ compilation system

C++ external function name encoding

In C++, you can have multiple functions which have the same name but differ in the number or types of the formal parameters. The C++ compiler will call the correct function based on the best match for the number and types of the actual arguments. The return data type must be the same for each function with the same name. To implement support for overloading function names, the C++ compiler encodes the parameter data types into the name of the function as it appears in the generated object code.

Use the nm(CP) command to look at the symbol names in the object file generated for the C++ source program overload.C:

   extern void foo(void);
   extern void foo(int);
   extern void foo(const char *);
   

void dummy () { int i = 1; const char * pchar = "string....."; foo(); foo(i); foo(pchar); }

CC -c overload.C nm overload.o

The nm output will show the following four entries for the functions either defined or declared in the code.

Symbols from overload.o:

   [Index]   Value     Size     Type   Bind  Other Shndx   Name
   

[6] | 0| 0|NOTY |GLOB |0 |UNDEF |foo__FPCc [7] | 0| 0|NOTY |GLOB |0 |UNDEF |foo__Fi [8] | 0| 0|NOTY |GLOB |0 |UNDEF |foo__Fv [9] | 0| 51|FUNC |GLOB |0 |1 |dummy__Fv

By using the -C option of nm, the names will be displayed in a format that resembles the function declaration. Note the absence of a function return data type.
   nm -C overload.o
Symbols from overload.o:
   [Index]   Value     Size     Type   Bind  Other Shndx   Name
   

[6] | 0| 0|NOTY |GLOB |0 |UNDEF |foo(const char*) [7] | 0| 0|NOTY |GLOB |0 |UNDEF |foo(int) [8] | 0| 0|NOTY |GLOB |0 |UNDEF |foo(void) [9] | 0| 51|FUNC |GLOB |0 |1 |dummy(void)

The same output will be produced by running the output of nm through the c++ name filter:
   nm overload.o | c++filt
C++ class names are encoded in the function name when used as a parameter type or when the function is a class member function. Look at the nm output for the object file produced from the following code.

string.C:

   class String {
   public:
   	String ();
   	String (const char *);
   	String (String &);
   	~String();
   String operator=(String &);
   };
   

String str1; String str2("abcdef...");

void foo(String & refStr) { String str3;

str3 = refStr; }

CC -c string.C nm string.o

This example contains usage of two constructors, a destructor, and an assignment operator member function for the String class. The nm output also shows the presence of a "static initialization" function which will be called (automatically) to initialize the global objects str1 and str2. There is a corresponding "termination" to call the necessary class destructors for objects at the file scope.

Note that externally visible variables are not encoded and appear in the object file exactly like a C global data object.

Symbols from string.o:

   [Index]   Value    Size    Type     Bind  Other Shndx   Name
   

[8] | 0| 0|NOTY |GLOB |0 |UNDEF |__as__6StringFR6String [9] | 84| 69|FUNC |GLOB |0 |1 |foo__FR6String [10] | 0| 0|NOTY |GLOB |0 |UNDEF |__dt__6StringFv [11] | 0| 0|NOTY |GLOB |0 |UNDEF |__ct__6StringFPCc [12] | 0| 0|NOTY |GLOB |0 |UNDEF |__ct__6StringFv [13] | 40| 41|FUNC |GLOB |0 |1 |__std__string_C_Sun_Apr_24 [14] | 0| 40|FUNC |GLOB |0 |1 |__sti__string_C_Sun_Apr_24 [15] | 1| 1|OBJT |GLOB |0 |2 |str2 [16] | 0| 1|OBJT |GLOB |0 |2 |str1

Once againing the -C option of nm output is easier to understand:

   nm  -C string.o

Symbols from string.o:

   [Index]   Value      Size    Type  Bind  Other Shndx   Name
   

[8] | 0| 0|NOTY |GLOB |0 |UNDEF |String::operator=(String&) [9] | 84| 69|FUNC |GLOB |0 |1 |foo(String&) [10] | 0| 0|NOTY |GLOB |0 |UNDEF |String::~String(void) [11] | 0| 0|NOTY |GLOB |0 |UNDEF |String::String(const char*) [12] | 0| 0|NOTY |GLOB |0 |UNDEF |String::String(void) [13] | 40| 41|FUNC |GLOB |0 |1 |string_C_Sun_Apr_24_:__std [14] | 0| 40|FUNC |GLOB |0 |1 |string_C_Sun_Apr_24_:__sti [15] | 1| 1|OBJT |GLOB |0 |2 |str2 [16] | 0| 1|OBJT |GLOB |0 |2 |str1


Next topic: Accessing C functions from C++
Previous topic: Diagnostics from the dynamic linker

© 2005 The SCO Group, Inc. All rights reserved.
SCO OpenServer Release 6.0.0 -- 02 June 2005