public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ name mangling in C
@ 2014-08-09 19:51 daniel
  2014-08-09 20:00 ` Marc Glisse
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: daniel @ 2014-08-09 19:51 UTC (permalink / raw)
  To: gcc-help

Hi,
C language does not use name mangling like C++. This can lead to  
subtle bugs, when function prototype is declared differently in  
different files. Simple example:

/* file1.c */
int test(int x, int y)
{
     return y;
}

/* file2.c */
#include <stdio.h>

extern int test(int x);

int main()
{
     int n = test(2);
     printf("n = %d\n", n);
     return 0;
}

When this code is compiled using C++ compiler, such error will be  
reported at linking phase as "undefined reference to 'test(int)'".  
Unfortunately in C compilation and linking will succeed, so bug will  
appear at runtime. But such bugs may be very hard to find.

My code base is too big to clean up all this mess and move  
declarations to header files manually in relatively short time.  
Therefore I was looking for a way to detect such bugs with some tool.  
I thought about forcing C++ mangling when compiling C code, but looks  
that gcc does not have any command line option to this. Please correct  
me if I am wrong.

If there is no such option, I would like to open an enhancement to add  
it. I thought about it for some time and looks that this new option  
should do 3 things: enable C++ name mangling, enable extern "C"  
directive and define the __cplusplus macro. Any comments on this  
proposal is also welcome.

Regards,
Daniel

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2014-08-11 12:40 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-09 19:51 C++ name mangling in C daniel
2014-08-09 20:00 ` Marc Glisse
2014-08-09 20:28   ` daniel
2014-08-10  0:50 ` Jonathan Wakely
2014-08-10 19:55   ` daniel
2014-08-10 23:36     ` Jonathan Wakely
2014-08-11 10:45       ` daniel
2014-08-11 11:24         ` Jonathan Wakely
2014-08-11 12:16           ` daniel
     [not found]             ` <20140811141602.Horde.U2tZekSvXHQDnS4cFS-29A1@sirzooro.proh ost.pl>
2014-08-11 12:40               ` Fabian Cenedese
2014-08-11 11:29 ` Florian Weimer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).