Help!! CC mangling works confusing!!

I have confusion about how CC mangling works for C code. Could you please give me a rule about when the mangling will be effect on the funcion?

In the following scenario, all three functions have been declared by extern "C" in queue.h, queue and queue2 have the same defination, but the queue2 has been declared again in queue.C.

Scenario:

bash-3.00$ more queue.h

extern "C" {

int *queue(char *q, void (*df)(void * object));

int *queue1(char *q, char *df);

int *queue2(char *q, void (*df)(void * object));

}

bash-3.00$ more queue.C

#include <stdio.h>

#include "queue.h"

int *queue(char *q, void (*df)(void * object))

{

printf("Test\n");

return 0;

}

int *queue1(char *q, char *df)

{

printf("Test1\n");

return 0;

}

extern "C" {

int *queue2(char *q, void (*df)(void * object))

{

printf("Test\n");

return 0;

}

}

queue.C

int *queue(queue_str *q, void (*df)(void * object))

{

print "Test\n";

}

On Solaris10 on x86, Sun Studio 11:

I compiled it by: C -w -c queue.C

Then

bash-3.00$ nm queue.o

queue.o:

[Index]ValueSizeType Bind Other ShndxName

[6]| 0|0|SECT |LOCL |0|6|

[2]| 0|0|SECT |LOCL |0|2|

[3]| 0|0|SECT |LOCL |0|3|

[4]| 0|0|SECT |LOCL |0|4|

[5]| 0|0|SECT |LOCL |0|5|

[7]| 0|54|FUNC |GLOB |0|2|__1cFqueue6FpcpFpv_v_pi_

[8]| 0|0|FUNC |GLOB |0|UNDEF |printf

[1]| 0|0|FILE |LOCL |0|ABS|queue.C

[9]|64|54|FUNC |GLOB |0|2|queue1

[10]|128|54|FUNC |GLOB |0|2|queue2

My question is why the function queue was mangled, but the queue1 and queue2 did not? I am appreciated for your response. Thanks!

[1800 byte] By [Daniceexia] at [2007-11-27 4:38:27]
# 1
If you declare function 'f' as extern "C" you have to define this function as extern "C" too.It might be interesting for you - compiler's warning frequently contains useful information :) If you turn off -w option you will get an answer to you question from compiler.
Atanasyana at 2007-7-12 9:48:55 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 2

For a discussion of name mangling, see my paper "The stability of the C++ ABI"

http://developers.sun.com/sunstudio/articles/CC_abi/CC_abi_content.html

In a C++ program, any function not declared extern "C" winds up with a mangled name. The reason is that the compiler must allow for the possibility that a different function with the same name might be defined in another part of the program and not seen by this compilation.

When you declare a function extern "C", you specify that the function follows the C rules for linkage (the external name seen by the linker, parameter passing conventions, etc). In particular, C does not allow more than one global function with the same name, and names are not mangled. Only one function with a given name can therefore be declared extern "C".

As Simon already explained, you generally need to be consistent about declaring and defining a function as extern "C".

clamage45a at 2007-7-12 9:48:55 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 3
Got it. Thank you all very much!
Daniceexia at 2007-7-12 9:48:55 > top of Java-index,Development Tools,Solaris and Linux Development Tools...