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

* Re: C++ name mangling in C
  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-11 11:29 ` Florian Weimer
  2 siblings, 1 reply; 11+ messages in thread
From: Marc Glisse @ 2014-08-09 20:00 UTC (permalink / raw)
  To: daniel; +Cc: gcc-help

On Sat, 9 Aug 2014, daniel@poradnik-webmastera.com wrote:

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

How about compiling your code with g++ then? (possibly add -fpermissive to 
accept more C-isms)

If you define __cplusplus, you'll need a C++ compiler anyway because the 
headers will contain C++ code.

Compiling with -flto may also generate interesting messages.

-- 
Marc Glisse

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

* Re: C++ name mangling in C
  2014-08-09 20:00 ` Marc Glisse
@ 2014-08-09 20:28   ` daniel
  0 siblings, 0 replies; 11+ messages in thread
From: daniel @ 2014-08-09 20:28 UTC (permalink / raw)
  To: gcc-help


Cytowanie Marc Glisse <marc.glisse@inria.fr>:

>> 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.
>
> How about compiling your code with g++ then? (possibly add  
> -fpermissive to accept more C-isms)

I already tried, and got error on some typedef struct declaration (do  
not remember exact syntax now). I will try to add -fpermissive and see  
if it will help.

> If you define __cplusplus, you'll need a C++ compiler anyway because  
> the headers will contain C++ code.

I see. In my case this is needed mostly for C stdlib. Other 3rd party  
libs usually have C and C++ interface separated, so simple support for  
extern "C" would be enough.

Another option to use instead of defining __cplusplus is to generate C  
names for functions defined in system headers (including ones from  
paths specified with -isystem). But this seems to be more complex,  
probably some extra interaction between preprocessor and compiler  
would be needed.

> Compiling with -flto may also generate interesting messages.

Will give it a try too.

-- 
Regards,
Daniel

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

* Re: C++ name mangling in C
  2014-08-09 19:51 C++ name mangling in C daniel
  2014-08-09 20:00 ` Marc Glisse
@ 2014-08-10  0:50 ` Jonathan Wakely
  2014-08-10 19:55   ` daniel
  2014-08-11 11:29 ` Florian Weimer
  2 siblings, 1 reply; 11+ messages in thread
From: Jonathan Wakely @ 2014-08-10  0:50 UTC (permalink / raw)
  To: daniel; +Cc: gcc-help

On 9 August 2014 20:50,  <daniel@poradnik-webmastera.com> wrote:
> 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.

So you want GCC to behave like a C++ compiler. It can already do that.

Just use C++.

If you get errors, fix them.

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

* Re: C++ name mangling in C
  2014-08-10  0:50 ` Jonathan Wakely
@ 2014-08-10 19:55   ` daniel
  2014-08-10 23:36     ` Jonathan Wakely
  0 siblings, 1 reply; 11+ messages in thread
From: daniel @ 2014-08-10 19:55 UTC (permalink / raw)
  To: gcc-help

Cytowanie Jonathan Wakely <jwakely.gcc@gmail.com>:

> On 9 August 2014 20:50,  <daniel@poradnik-webmastera.com> wrote:
>> 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.
>
> So you want GCC to behave like a C++ compiler. It can already do that.
>
> Just use C++.
>
> If you get errors, fix them.

Easier said than done. I think it will take about month of work to  
change our code base in order to compile it using g++. We are planning  
to do this eventually, but not now. Because of this I am looking for  
some temporary solution to use now.

-- 
Regards,
Daniel

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

* Re: C++ name mangling in C
  2014-08-10 19:55   ` daniel
@ 2014-08-10 23:36     ` Jonathan Wakely
  2014-08-11 10:45       ` daniel
  0 siblings, 1 reply; 11+ messages in thread
From: Jonathan Wakely @ 2014-08-10 23:36 UTC (permalink / raw)
  To: Daniel Frużyński; +Cc: gcc-help

On 10 August 2014 20:55,  <daniel@poradnik-webmastera.com> wrote:
> Cytowanie Jonathan Wakely <jwakely.gcc@gmail.com>:
>
>> On 9 August 2014 20:50,  <daniel@poradnik-webmastera.com> wrote:
>>>
>>> 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.
>>
>>
>> So you want GCC to behave like a C++ compiler. It can already do that.
>>
>> Just use C++.
>>
>> If you get errors, fix them.
>
>
> Easier said than done. I think it will take about month of work to change
> our code base in order to compile it using g++. We are planning to do this
> eventually, but not now. Because of this I am looking for some temporary
> solution to use now.

It will be a lot easier to fix your code than to modify GCC to do what
you're requesting.

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

* Re: C++ name mangling in C
  2014-08-10 23:36     ` Jonathan Wakely
@ 2014-08-11 10:45       ` daniel
  2014-08-11 11:24         ` Jonathan Wakely
  0 siblings, 1 reply; 11+ messages in thread
From: daniel @ 2014-08-11 10:45 UTC (permalink / raw)
  To: gcc-help


Cytowanie Jonathan Wakely <jwakely.gcc@gmail.com>:

>>> So you want GCC to behave like a C++ compiler. It can already do that.
>>>
>>> Just use C++.
>>>
>>> If you get errors, fix them.
>>
>>
>> Easier said than done. I think it will take about month of work to change
>> our code base in order to compile it using g++. We are planning to do this
>> eventually, but not now. Because of this I am looking for some temporary
>> solution to use now.
>
> It will be a lot easier to fix your code than to modify GCC to do what
> you're requesting.

I do not know GCC internals so I cannot estimate effort for such  
modification. I thought that this will not be so hard, but looks that  
is is not so easy.

BTW, I realized that there may be an easier way to do such validation  
in GCC - write information about each function prototype (found where  
function is implemented) and each function call to object files (e.g.  
as debug data or in other format), and use it to perform validation in  
linker. What do you think about this?

-- 
Regards,
Daniel

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

* Re: C++ name mangling in C
  2014-08-11 10:45       ` daniel
@ 2014-08-11 11:24         ` Jonathan Wakely
  2014-08-11 12:16           ` daniel
  0 siblings, 1 reply; 11+ messages in thread
From: Jonathan Wakely @ 2014-08-11 11:24 UTC (permalink / raw)
  To: daniel; +Cc: gcc-help

On 11 August 2014 11:45,  <daniel@poradnik-webmastera.com> wrote:
> I do not know GCC internals so I cannot estimate effort for such
> modification. I thought that this will not be so hard, but looks that is is
> not so easy.

"Make the C compiler have an option to act like the C++ compiler" is
basically an entire new language that is halfway between C and C++.
That's a huge amount of work and testing, which you're asking other
people to do because it is too difficult for you to fix your code.

> BTW, I realized that there may be an easier way to do such validation in GCC
> - write information about each function prototype (found where function is
> implemented) and each function call to object files (e.g. as debug data or
> in other format), and use it to perform validation in linker. What do you
> think about this?

That sounds more realistic (and would be useful to people using C, not
your suggested C-with-mangling) but I it's still many weeks of work by
many people, so you don't have to spend a month fixing your code. Even
if it happened, it wouldn't be available in released versions of GCC
and the linker for months, so it is not going to help you find "some
temporary solution to use now".

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

* Re: C++ name mangling in C
  2014-08-09 19:51 C++ name mangling in C daniel
  2014-08-09 20:00 ` Marc Glisse
  2014-08-10  0:50 ` Jonathan Wakely
@ 2014-08-11 11:29 ` Florian Weimer
  2 siblings, 0 replies; 11+ messages in thread
From: Florian Weimer @ 2014-08-11 11:29 UTC (permalink / raw)
  To: daniel, gcc-help

On 08/09/2014 09:50 PM, daniel@poradnik-webmastera.com wrote:
> 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.

If you have such issues in the code base, a conversion to C++ will be 
very difficult and is unlikely to uncover all such issues.  For example, 
neither function return nor structure layout influence name mangling.

-flto should really warn about this, but it didn't with my tests (based 
on GCC 4.8).  Does anybody know what's going on there?  How can GCC even 
pretend to generate correct code in the face of such blatant typing 
violations?

-- 
Florian Weimer / Red Hat Product Security

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

* Re: C++ name mangling in C
  2014-08-11 11:24         ` Jonathan Wakely
@ 2014-08-11 12:16           ` daniel
       [not found]             ` <20140811141602.Horde.U2tZekSvXHQDnS4cFS-29A1@sirzooro.proh ost.pl>
  0 siblings, 1 reply; 11+ messages in thread
From: daniel @ 2014-08-11 12:16 UTC (permalink / raw)
  To: gcc-help


Cytowanie Jonathan Wakely <jwakely.gcc@gmail.com>:

> On 11 August 2014 11:45,  <daniel@poradnik-webmastera.com> wrote:
>> I do not know GCC internals so I cannot estimate effort for such
>> modification. I thought that this will not be so hard, but looks that is is
>> not so easy.
>
> "Make the C compiler have an option to act like the C++ compiler" is
> basically an entire new language that is halfway between C and C++.
> That's a huge amount of work and testing, which you're asking other
> people to do because it is too difficult for you to fix your code.

It is not too difficult, it is time consuming. And we do not have much  
free time now. Therefore I am looking for some temporary solutions to  
make our code better. Now I see that my proposal is not very realistic  
to implement from your point of view.

>
>> BTW, I realized that there may be an easier way to do such validation in GCC
>> - write information about each function prototype (found where function is
>> implemented) and each function call to object files (e.g. as debug data or
>> in other format), and use it to perform validation in linker. What do you
>> think about this?
>
> That sounds more realistic (and would be useful to people using C, not
> your suggested C-with-mangling) but I it's still many weeks of work by
> many people, so you don't have to spend a month fixing your code. Even
> if it happened, it wouldn't be available in released versions of GCC
> and the linker for months, so it is not going to help you find "some
> temporary solution to use now".

I know that you have to stick with your release plans, so this is not  
something to expect soon. I understand this. What I am looking for are  
things which I could use now; I thought there may be something in gcc  
already to help me with my work now, so I asked here.

I also look for long term solutions which could be helpful for me and  
for others, so I asked more questions. Sorry if I annoyed you.

Thank you for your answer. I will wait for some more feedback on my  
last proposal about adding and using this extra info, and log and  
enhancement.

-- 
Regards,
Daniel

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

* Re: C++ name mangling in C
       [not found]             ` <20140811141602.Horde.U2tZekSvXHQDnS4cFS-29A1@sirzooro.proh ost.pl>
@ 2014-08-11 12:40               ` Fabian Cenedese
  0 siblings, 0 replies; 11+ messages in thread
From: Fabian Cenedese @ 2014-08-11 12:40 UTC (permalink / raw)
  To: gcc-help


>>>BTW, I realized that there may be an easier way to do such validation in GCC
>>>- write information about each function prototype (found where function is
>>>implemented) and each function call to object files (e.g. as debug data or
>>>in other format), and use it to perform validation in linker. What do you
>>>think about this?
>>
>>That sounds more realistic (and would be useful to people using C, not
>>your suggested C-with-mangling) but I it's still many weeks of work by
>>many people, so you don't have to spend a month fixing your code. Even
>>if it happened, it wouldn't be available in released versions of GCC
>>and the linker for months, so it is not going to help you find "some
>>temporary solution to use now".
>
>I know that you have to stick with your release plans, so this is not  
>something to expect soon. I understand this. What I am looking for are  
>things which I could use now; I thought there may be something in gcc  
>already to help me with my work now, so I asked here.
>
>I also look for long term solutions which could be helpful for me and  
>for others, so I asked more questions. Sorry if I annoyed you.
>
>Thank you for your answer. I will wait for some more feedback on my  
>last proposal about adding and using this extra info, and log and  
>enhancement.

Maybe you could get more results by using a static code analyzer
which does basically the same as the compiler (without actually
compiling) e.g. print a lot of errors and warnings that might show
you the problematic places in the code. We've found pclint very
helpful but of course there are also other programs.

bye  Fabi

^ 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).