Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c++ > #8371 > unrolled thread

Is there any software porting C++ to C?

Started by"crea" <no@invalid.com>
First post2011-08-01 08:25 +0100
Last post2011-08-03 07:34 -0700
Articles 20 on this page of 21 — 15 participants

Back to article view | Back to comp.lang.c++


Contents

  Is there any software porting C++ to C? "crea" <no@invalid.com> - 2011-08-01 08:25 +0100
    Re: Is there any software porting C++ to C? Ian Collins <ian-news@hotmail.com> - 2011-08-01 19:42 +1200
      Re: Is there any software porting C++ to C? "crea" <no@invalid.com> - 2011-08-01 08:47 +0100
        Re: Is there any software porting C++ to C? "Bo Persson" <bop@gmb.dk> - 2011-08-01 11:21 +0200
          Re: Is there any software porting C++ to C? "crea" <no@invalid.com> - 2011-08-01 10:26 +0100
        Re: Is there any software porting C++ to C? Ian Collins <ian-news@hotmail.com> - 2011-08-01 21:26 +1200
          Re: Is there any software porting C++ to C? "crea" <no@invalid.com> - 2011-08-01 11:16 +0100
            Re: Is there any software porting C++ to C? Robert Wessel <robertwessel2@yahoo.com> - 2011-08-01 11:59 -0500
              Re: Is there any software porting C++ to C? BGB <cr88192@hotmail.com> - 2011-08-01 14:18 -0700
              Re: Is there any software porting C++ to C? James Kanze <james.kanze@gmail.com> - 2011-08-07 11:39 -0700
    Re: Is there any software porting C++ to C? Juha Nieminen <nospam@thanks.invalid> - 2011-08-01 08:48 +0000
      Re: Is there any software porting C++ to C? "crea" <no@invalid.com> - 2011-08-01 10:29 +0100
      Re: Is there any software porting C++ to C? "Waldek M." <wm@localhost.localdomain> - 2011-08-04 21:54 +0200
    Re: Is there any software porting C++ to C? Marcel Müller <news.5.maazl@spamgourmet.com> - 2011-08-01 11:29 +0200
    Re: Is there any software porting C++ to C? Joshua Maurice <joshuamaurice@gmail.com> - 2011-08-02 14:45 -0700
      Re: Is there any software porting C++ to C? Philipp Klaus Krause <pkk@spth.de> - 2011-08-04 19:30 +0200
        Re: Is there any software porting C++ to C? Victor Bazarov <v.bazarov@comcast.invalid> - 2011-08-04 13:53 -0400
          Re: Is there any software porting C++ to C? jt@toerring.de (Jens Thoms Toerring) - 2011-08-04 20:38 +0000
          Re: Is there any software porting C++ to C? Nobody <nobody@nowhere.com> - 2011-08-04 21:54 +0100
        Re: Is there any software porting C++ to C? BGB <cr88192@hotmail.com> - 2011-08-04 15:40 -0700
    Re: Is there any software porting C++ to C? Uwe Dolinsky <uwe@codeplay.com> - 2011-08-03 07:34 -0700

Page 1 of 2  [1] 2  Next page →


#8371 — Is there any software porting C++ to C?

From"crea" <no@invalid.com>
Date2011-08-01 08:25 +0100
SubjectIs there any software porting C++ to C?
Message-ID<xxsZp.48094$T85.36355@newsfe19.ams2>
I need to port C++ code to C code. It includes templates and classes. Is
this possible and is there any programs which would do this automatically?
Or any other help regarding this. Thank you

[toc] | [next] | [standalone]


#8372

FromIan Collins <ian-news@hotmail.com>
Date2011-08-01 19:42 +1200
Message-ID<99n3n3Faa6U3@mid.individual.net>
In reply to#8371
On 08/ 1/11 07:25 PM, crea wrote:
> I need to port C++ code to C code. It includes templates and classes. Is
> this possible and is there any programs which would do this automatically?
> Or any other help regarding this. Thank you

  Comeau C++ or a lot of pain.

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#8373

From"crea" <no@invalid.com>
Date2011-08-01 08:47 +0100
Message-ID<gSsZp.127986$fQ4.108328@newsfe16.ams2>
In reply to#8372
"Ian Collins" <ian-news@hotmail.com> wrote in message 
news:99n3n3Faa6U3@mid.individual.net...
> On 08/ 1/11 07:25 PM, crea wrote:
>> I need to port C++ code to C code. It includes templates and classes. Is
>> this possible and is there any programs which would do this 
>> automatically?
>> Or any other help regarding this. Thank you
>
>  Comeau C++ or a lot of pain.

I checked that, but in my understanding it does not produce (necessary) 
c-code:

http://www.comeaucomputing.com/faqs/genfaq.html#ccompiler
"the generated C code is not standalone. Therefore, it cannot be used by 
itself (note that this is both a technical and legal requirement when using 
Comeau C++), and this is why **there is not normally an option to see the 
generated C code**:"

So it only gives executable program? 

[toc] | [prev] | [next] | [standalone]


#8378

From"Bo Persson" <bop@gmb.dk>
Date2011-08-01 11:21 +0200
Message-ID<99n9gmFfr3U1@mid.individual.net>
In reply to#8373
crea wrote:
> "Ian Collins" <ian-news@hotmail.com> wrote in message
> news:99n3n3Faa6U3@mid.individual.net...
>> On 08/ 1/11 07:25 PM, crea wrote:
>>> I need to port C++ code to C code. It includes templates and
>>> classes. Is this possible and is there any programs which would
>>> do this automatically?
>>> Or any other help regarding this. Thank you
>>
>>  Comeau C++ or a lot of pain.
>
> I checked that, but in my understanding it does not produce
> (necessary) c-code:
>
> http://www.comeaucomputing.com/faqs/genfaq.html#ccompiler
> "the generated C code is not standalone. Therefore, it cannot be
> used by itself (note that this is both a technical and legal
> requirement when using Comeau C++), and this is why **there is not
> normally an option to see the generated C code**:"
>
> So it only gives executable program?

Yes.

The legal issues apart, I believe it generates badass ugly C code that 
would risk making you go blind if you try to read it. It would be 
totally unmaintanable anyway.


Bo Persson

[toc] | [prev] | [next] | [standalone]


#8379

From"crea" <no@invalid.com>
Date2011-08-01 10:26 +0100
Message-ID<NiuZp.77271$Sr.49395@newsfe12.ams2>
In reply to#8378
"Bo Persson" <bop@gmb.dk> wrote in message 
news:99n9gmFfr3U1@mid.individual.net...
> crea wrote:
>> "Ian Collins" <ian-news@hotmail.com> wrote in message
>> news:99n3n3Faa6U3@mid.individual.net...
>>> On 08/ 1/11 07:25 PM, crea wrote:
>>>> I need to port C++ code to C code. It includes templates and
>>>> classes. Is this possible and is there any programs which would
>>>> do this automatically?
>>>> Or any other help regarding this. Thank you
>>>
>>>  Comeau C++ or a lot of pain.
>>
>> I checked that, but in my understanding it does not produce
>> (necessary) c-code:
>>
>> http://www.comeaucomputing.com/faqs/genfaq.html#ccompiler
>> "the generated C code is not standalone. Therefore, it cannot be
>> used by itself (note that this is both a technical and legal
>> requirement when using Comeau C++), and this is why **there is not
>> normally an option to see the generated C code**:"
>>
>> So it only gives executable program?
>
> Yes.
>
> The legal issues apart, I believe it generates badass ugly C code that 
> would risk making you go blind if you try to read it. It would be totally 
> unmaintanable anyway.

I see :) . 

[toc] | [prev] | [next] | [standalone]


#8380

FromIan Collins <ian-news@hotmail.com>
Date2011-08-01 21:26 +1200
Message-ID<99n9q2Faa6U4@mid.individual.net>
In reply to#8373
On 08/ 1/11 07:47 PM, crea wrote:
> "Ian Collins"<ian-news@hotmail.com>  wrote in message
> news:99n3n3Faa6U3@mid.individual.net...
>> On 08/ 1/11 07:25 PM, crea wrote:
>>> I need to port C++ code to C code. It includes templates and classes. Is
>>> this possible and is there any programs which would do this
>>> automatically?
>>> Or any other help regarding this. Thank you
>>
>>   Comeau C++ or a lot of pain.
>
> I checked that, but in my understanding it does not produce (necessary)
> c-code:
>
> http://www.comeaucomputing.com/faqs/genfaq.html#ccompiler
> "the generated C code is not standalone. Therefore, it cannot be used by
> itself (note that this is both a technical and legal requirement when using
> Comeau C++), and this is why **there is not normally an option to see the
> generated C code**:"
>
> So it only gives executable program?

It enables you to run C++ code on a target without a native C++ 
compiler, which I assumed was your problem.

If that isn't he case, what is your reason?

-- 
Ian Collins

[toc] | [prev] | [next] | [standalone]


#8383

From"crea" <no@invalid.com>
Date2011-08-01 11:16 +0100
Message-ID<H1vZp.67851$Ll1.46368@newsfe24.ams2>
In reply to#8380
"Ian Collins" <ian-news@hotmail.com> wrote in message 
news:99n9q2Faa6U4@mid.individual.net...
> On 08/ 1/11 07:47 PM, crea wrote:
>> "Ian Collins"<ian-news@hotmail.com>  wrote in message
>> news:99n3n3Faa6U3@mid.individual.net...
>>> On 08/ 1/11 07:25 PM, crea wrote:
>>>> I need to port C++ code to C code. It includes templates and classes. 
>>>> Is
>>>> this possible and is there any programs which would do this
>>>> automatically?
>>>> Or any other help regarding this. Thank you
>>>
>>>   Comeau C++ or a lot of pain.
>>
>> I checked that, but in my understanding it does not produce (necessary)
>> c-code:
>>
>> http://www.comeaucomputing.com/faqs/genfaq.html#ccompiler
>> "the generated C code is not standalone. Therefore, it cannot be used by
>> itself (note that this is both a technical and legal requirement when 
>> using
>> Comeau C++), and this is why **there is not normally an option to see the
>> generated C code**:"
>>
>> So it only gives executable program?
>
> It enables you to run C++ code on a target without a native C++ compiler, 
> which I assumed was your problem.
>
> If that isn't he case, what is your reason?

I think I really need to c-code, because there are other libraries included 
and this section is only part of the whole project. Although am not expert 
in making projects.. so not sure. Maybe they could be compiled separately 
and then link... 

[toc] | [prev] | [next] | [standalone]


#8389

FromRobert Wessel <robertwessel2@yahoo.com>
Date2011-08-01 11:59 -0500
Message-ID<7lmd37la28p9tgov91qo6bipaaa7p6dlrq@4ax.com>
In reply to#8383
On Mon, 1 Aug 2011 11:16:05 +0100, "crea" <no@invalid.com> wrote:

>
>"Ian Collins" <ian-news@hotmail.com> wrote in message 
>news:99n9q2Faa6U4@mid.individual.net...
>> On 08/ 1/11 07:47 PM, crea wrote:
>>> "Ian Collins"<ian-news@hotmail.com>  wrote in message
>>> news:99n3n3Faa6U3@mid.individual.net...
>>>> On 08/ 1/11 07:25 PM, crea wrote:
>>>>> I need to port C++ code to C code. It includes templates and classes. 
>>>>> Is
>>>>> this possible and is there any programs which would do this
>>>>> automatically?
>>>>> Or any other help regarding this. Thank you
>>>>
>>>>   Comeau C++ or a lot of pain.
>>>
>>> I checked that, but in my understanding it does not produce (necessary)
>>> c-code:
>>>
>>> http://www.comeaucomputing.com/faqs/genfaq.html#ccompiler
>>> "the generated C code is not standalone. Therefore, it cannot be used by
>>> itself (note that this is both a technical and legal requirement when 
>>> using
>>> Comeau C++), and this is why **there is not normally an option to see the
>>> generated C code**:"
>>>
>>> So it only gives executable program?
>>
>> It enables you to run C++ code on a target without a native C++ compiler, 
>> which I assumed was your problem.
>>
>> If that isn't he case, what is your reason?
>
>I think I really need to c-code, because there are other libraries included 
>and this section is only part of the whole project. Although am not expert 
>in making projects.. so not sure. Maybe they could be compiled separately 
>and then link... 


Usually calling C code from C++ is not too much of a problem.  You do
compile the C modules separately, and then link the various bits.  C++
provides:

  extern "C" {...}

for just that purpose.

http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html

[toc] | [prev] | [next] | [standalone]


#8395

FromBGB <cr88192@hotmail.com>
Date2011-08-01 14:18 -0700
Message-ID<j175fc$31s$1@news.albasani.net>
In reply to#8389
On 8/1/2011 9:59 AM, Robert Wessel wrote:
> On Mon, 1 Aug 2011 11:16:05 +0100, "crea"<no@invalid.com>  wrote:
>
>>
>> "Ian Collins"<ian-news@hotmail.com>  wrote in message
>> news:99n9q2Faa6U4@mid.individual.net...
>>> On 08/ 1/11 07:47 PM, crea wrote:
>>>> "Ian Collins"<ian-news@hotmail.com>   wrote in message
>>>> news:99n3n3Faa6U3@mid.individual.net...
>>>>> On 08/ 1/11 07:25 PM, crea wrote:
>>>>>> I need to port C++ code to C code. It includes templates and classes.
>>>>>> Is
>>>>>> this possible and is there any programs which would do this
>>>>>> automatically?
>>>>>> Or any other help regarding this. Thank you
>>>>>
>>>>>    Comeau C++ or a lot of pain.
>>>>
>>>> I checked that, but in my understanding it does not produce (necessary)
>>>> c-code:
>>>>
>>>> http://www.comeaucomputing.com/faqs/genfaq.html#ccompiler
>>>> "the generated C code is not standalone. Therefore, it cannot be used by
>>>> itself (note that this is both a technical and legal requirement when
>>>> using
>>>> Comeau C++), and this is why **there is not normally an option to see the
>>>> generated C code**:"
>>>>
>>>> So it only gives executable program?
>>>
>>> It enables you to run C++ code on a target without a native C++ compiler,
>>> which I assumed was your problem.
>>>
>>> If that isn't he case, what is your reason?
>>
>> I think I really need to c-code, because there are other libraries included
>> and this section is only part of the whole project. Although am not expert
>> in making projects.. so not sure. Maybe they could be compiled separately
>> and then link...
>
>
> Usually calling C code from C++ is not too much of a problem.  You do
> compile the C modules separately, and then link the various bits.  C++
> provides:
>
>    extern "C" {...}
>
> for just that purpose.
>
> http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html
>

yeah...


going partly OT here:

in my own projects, extern "C" is essentially necessary to interface any 
C++ code to my scripting VM technology as, sadly, it has currently no 
real ability to understand C++ code or interface with the C++ ABIs.

I "could" eventually try to address this, but sadly this is not really 
likely to be a small or easy task (doing semi-transparent interfacing 
between C++ and a language using dynamic+soft typing, is likely to get a 
bit hairy).


previously, I had taken a more strict "C only" policy for these reasons 
in many areas in the codebase, although later I came to better 
understand how a lot of this can be made to work.

however, apart from in certain cases, such as in largish self-contained 
regions of logic code, which do something other than sit around 
exporting piles of library functions, C++ offers no particular advantage 
(it would be sort of a waste to use C++ just to have nearly everything 
marked as extern "C").

in my case, this renders C++ as a minor-use language in my case (the 
bulk of code in my case is plain C).


funny thing is, much like C++, my scripting HLL defines a vaguely 
similar mechanism:
native package C { ... }

which serves a similar role (it is a hint for tools to export any 
contained functions/... to C, generally spitting out headers and wrapped 
versions of the function calls as C code).

note: emitting wrapped calls is needed as C is otherwise incapable of 
late-binding or linking against thin air, so one needs wrappers to 
redirect the call into the HLL VM. well, planned, at least. the current 
mechanism is built on DLL-exported function pointers, called "proxies", 
but this is nasty and poses portability worries. sadly, there is not an 
"ideal" way to handle cross-language interfacing (one either has to call 
via a function pointer, or make an API call, or something...).


so, it is possible to interface between my language and C++ by having 
each pretend that the other is C, and operating at the level of the C ABI.

also possible (partially tested before as an experiment) was to in turn 
compile some of the code as C++/CLI, which in-turn allowed calling into 
the script VM from C# (C# -> C++/CLI -> C -> BS).


a lot of this is partly because of a personal belief that cross-language 
interfacing should (ideally) not involve manually writing piles of ugly 
glue code (and HLL VM's should not be isolated mono-language islands).


but, on to the topic:
if extern "C" can be made to work, this is likely a preferable strategy 
to rewriting all of the code in plain C.

[toc] | [prev] | [next] | [standalone]


#8553

FromJames Kanze <james.kanze@gmail.com>
Date2011-08-07 11:39 -0700
Message-ID<1d5a3695-e943-4010-86bc-43823c708dc3@y13g2000yqy.googlegroups.com>
In reply to#8389
On Aug 1, 5:59 pm, Robert Wessel <robertwess...@yahoo.com> wrote:
> On Mon, 1 Aug 2011 11:16:05 +0100, "crea" <n...@invalid.com> wrote:

    [...]
> >I think I really need to c-code, because there are other libraries included
> >and this section is only part of the whole project. Although am not expert
> >in making projects.. so not sure. Maybe they could be compiled separately
> >and then link...

> Usually calling C code from C++ is not too much of a problem.  You do
> compile the C modules separately, and then link the various bits.  C++
> provides:

>   extern "C" {...}

> for just that purpose.

Calling C code from X++ is never a problem.  Calling C++ code
from C isn't a problem either, provided 1) that the functions
you call are declared extern "C", and 2) that the main function
is written in C++ (and that you don't try to propagate any
exceptions through the C code, of course).  In practice, the
second requirement can often be dropped: at least under Windows
and Linux (and Solaris, I'm pretty sure), main can be in C as
well, at least if your code is in a separate DLL.  (I think the
main for both Python and the Java JVM is in C; their interfaces
are definitely in C; but there are lots of plugins for them
written in C++.)

--
James Kanze

[toc] | [prev] | [next] | [standalone]


#8376

FromJuha Nieminen <nospam@thanks.invalid>
Date2011-08-01 08:48 +0000
Message-ID<4e366864$0$3068$7b1e8fa0@news.nbl.fi>
In reply to#8371
crea <no@invalid.com> wrote:
> I need to port C++ code to C code. It includes templates and classes. Is
> this possible and is there any programs which would do this automatically?
> Or any other help regarding this. Thank you

  Can you explain why?

  It's possible to create a C program that is equivalent to a C++ program
in a specific environment, but there's a big chance that this generated C
source code will not compile or work properly in a different environment.

  For example, you can have templates that generate code conditionally
depending on, for example, the size or limits of a basic type. If you
mechanically translate this code to C you will be assuming that this
size or the limits are something specific, and the generic nature of
the code will be lost. This means that if you try to compile the same
C code in a different environment where those sizes of limits are
different (eg. 32-bit vs 64-bit), it probably won't work properly.

[toc] | [prev] | [next] | [standalone]


#8381

From"crea" <no@invalid.com>
Date2011-08-01 10:29 +0100
Message-ID<AluZp.68690$_r1.22183@newsfe06.ams2>
In reply to#8376
"Juha Nieminen" <nospam@thanks.invalid> wrote in message 
news:4e366864$0$3068$7b1e8fa0@news.nbl.fi...
> crea <no@invalid.com> wrote:
>> I need to port C++ code to C code. It includes templates and classes. Is
>> this possible and is there any programs which would do this 
>> automatically?
>> Or any other help regarding this. Thank you
>
>  Can you explain why?

I am porting a system to another (similar) system which is C language.

>
>  It's possible to create a C program that is equivalent to a C++ program
> in a specific environment, but there's a big chance that this generated C
> source code will not compile or work properly in a different environment.
>
>  For example, you can have templates that generate code conditionally
> depending on, for example, the size or limits of a basic type. If you
> mechanically translate this code to C you will be assuming that this
> size or the limits are something specific, and the generic nature of
> the code will be lost.

good point, I have consider this when porting

This means that if you try to compile the same
> C code in a different environment where those sizes of limits are
> different (eg. 32-bit vs 64-bit), it probably won't work properly.
>

ye. Maybe to port first only to 64... 

[toc] | [prev] | [next] | [standalone]


#8475

From"Waldek M." <wm@localhost.localdomain>
Date2011-08-04 21:54 +0200
Message-ID<k5xv5xg70fey.dlg@localhost.localdomain>
In reply to#8376
Day 01 Aug 2011 08:48:36 GMT, Juha Nieminen wrote:
>   For example, you can have templates that generate code conditionally
> depending on, for example, the size or limits of a basic type. If you
> mechanically translate this code to C you will be assuming that this
> size or the limits are something specific, and the generic nature of
> the code will be lost. This means that if you try to compile the same
> C code in a different environment where those sizes of limits are
> different (eg. 32-bit vs 64-bit), it probably won't work properly.

Well, I don't know.
Arent't there gnu autotools for preventing it?

Best regards,
Waldek

[toc] | [prev] | [next] | [standalone]


#8382

FromMarcel Müller <news.5.maazl@spamgourmet.com>
Date2011-08-01 11:29 +0200
Message-ID<4e367214$0$6580$9b4e6d93@newsspool3.arcor-online.net>
In reply to#8371
crea wrote:
> I need to port C++ code to C code. It includes templates and classes. Is
> this possible and is there any programs which would do this automatically?

The first C++ compilers worked this way (cfront). But the trace ends 
about 1990. So you won't get happy with templates.

However, if you ever succeed, you will also need a C port of the C++ 
runtime library.

So if you really want to do the C++ to C transition, drop the 
application code and write it anew in C. This will save you the time for 
the unsuccessful research.


Marcel

[toc] | [prev] | [next] | [standalone]


#8424

FromJoshua Maurice <joshuamaurice@gmail.com>
Date2011-08-02 14:45 -0700
Message-ID<49572b2b-49fc-4cd7-9d97-c6891c20d2ef@t38g2000prj.googlegroups.com>
In reply to#8371
On Aug 1, 12:25 am, "crea" <n...@invalid.com> wrote:
> I need to port C++ code to C code. It includes templates and classes. Is
> this possible and is there any programs which would do this automatically?
> Or any other help regarding this. Thank you

You are asking, as explained else-thread, that you are looking for a
way to convert the C++ code to maintainable and readable C code.

If the C++ code uses exceptions, you're in a world of pain. It's
borderline impossible for this to be automated. C++ exceptions are the
one C++ feature that does not map nicely onto C.

Ignoring C++ exceptions, it's still asking a lot to go from good C++
code to good C code. They use different idioms due to the lack (or
availability) of certain features. You can blindly transform
destructor calls into explicit function calls, but is this a good
idea? Likely no. You might get something halfway readable for the most
part, but it wouldn't be good C code.

[toc] | [prev] | [next] | [standalone]


#8472

FromPhilipp Klaus Krause <pkk@spth.de>
Date2011-08-04 19:30 +0200
Message-ID<4e3ad745$0$4151$6e1ede2f@read.cnntp.org>
In reply to#8424
Am 02.08.2011 23:45, schrieb Joshua Maurice:
> On Aug 1, 12:25 am, "crea" <n...@invalid.com> wrote:
>> I need to port C++ code to C code. It includes templates and classes. Is
>> this possible and is there any programs which would do this automatically?
>> Or any other help regarding this. Thank you
> 
> You are asking, as explained else-thread, that you are looking for a
> way to convert the C++ code to maintainable and readable C code.
> 
> If the C++ code uses exceptions, you're in a world of pain. It's
> borderline impossible for this to be automated. C++ exceptions are the
> one C++ feature that does not map nicely onto C.

Pardon my ignorance (I've never written a C++ compiler), but what's the
problem with exceptions? I'd imagine them to be rather straightforward
to implement using setjmp()/longjmp().

Philipp

[toc] | [prev] | [next] | [standalone]


#8473

FromVictor Bazarov <v.bazarov@comcast.invalid>
Date2011-08-04 13:53 -0400
Message-ID<j1ema2$feg$1@dont-email.me>
In reply to#8472
On 8/4/2011 1:30 PM, Philipp Klaus Krause wrote:
> Am 02.08.2011 23:45, schrieb Joshua Maurice:
>> On Aug 1, 12:25 am, "crea"<n...@invalid.com>  wrote:
>>> I need to port C++ code to C code. It includes templates and classes. Is
>>> this possible and is there any programs which would do this automatically?
>>> Or any other help regarding this. Thank you
>>
>> You are asking, as explained else-thread, that you are looking for a
>> way to convert the C++ code to maintainable and readable C code.
>>
>> If the C++ code uses exceptions, you're in a world of pain. It's
>> borderline impossible for this to be automated. C++ exceptions are the
>> one C++ feature that does not map nicely onto C.
>
> Pardon my ignorance (I've never written a C++ compiler), but what's the
> problem with exceptions? I'd imagine them to be rather straightforward
> to implement using setjmp()/longjmp().

I am thinking the complexity is in "stack unwinding", due to the need to 
destruct all local objects still "alive" between the 'try' and 'throw'. 
  IOW, it's not as simple as just transferring control from one part of 
the code to the other.  C doesn't have the "stack unwinding" need, you see.

V
-- 
I do not respond to top-posted replies, please don't ask

[toc] | [prev] | [next] | [standalone]


#8477

Fromjt@toerring.de (Jens Thoms Toerring)
Date2011-08-04 20:38 +0000
Message-ID<9a0eavF4qkU2@mid.uni-berlin.de>
In reply to#8473
Victor Bazarov <v.bazarov@comcast.invalid> wrote:
> On 8/4/2011 1:30 PM, Philipp Klaus Krause wrote:
> > Am 02.08.2011 23:45, schrieb Joshua Maurice:
> >> On Aug 1, 12:25 am, "crea"<n...@invalid.com>  wrote:
> >>> I need to port C++ code to C code. It includes templates and classes. Is
> >>> this possible and is there any programs which would do this automatically?
> >>> Or any other help regarding this. Thank you
> >>
> >> You are asking, as explained else-thread, that you are looking for a
> >> way to convert the C++ code to maintainable and readable C code.
> >>
> >> If the C++ code uses exceptions, you're in a world of pain. It's
> >> borderline impossible for this to be automated. C++ exceptions are the
> >> one C++ feature that does not map nicely onto C.
> >
> > Pardon my ignorance (I've never written a C++ compiler), but what's the
> > problem with exceptions? I'd imagine them to be rather straightforward
> > to implement using setjmp()/longjmp().

> I am thinking the complexity is in "stack unwinding", due to the need to 
> destruct all local objects still "alive" between the 'try' and 'throw'. 
>   IOW, it's not as simple as just transferring control from one part of 
> the code to the other.  C doesn't have the "stack unwinding" need, you see.

Having implemented some kind of analog of exceptions for a
C program of mine this is at least one of the issues (which
requires that you do the "stack unwinding" manually and if
you forget about it you may end up with a lot of trouble,
so it's a lot more "brittle" than what can be done in C++).
Other problems include that setjmp()/longjmp() normally don't
save CPU registers (so you must somehow ensure manually that
no values you may need after a try/catch block can be stored
in registers), you rather can have only a finite stack of re-
cursive try's (or you risk some really hairy problems with what
to do when you run out of memory, which also raises an excep-
tion) etc. And you're always very near at the border to unde-
fined behaviour (I am still not 100% sure that I didn't cross
that line somewhere). In that program it was worth all the
trouble, but having C++ exceptions would have been something
I would have given a lot for;-)

                              Regards, Jens
-- 
  \   Jens Thoms Toerring  ___      jt@toerring.de
   \__________________________      http://toerring.de

[toc] | [prev] | [next] | [standalone]


#8479

FromNobody <nobody@nowhere.com>
Date2011-08-04 21:54 +0100
Message-ID<pan.2011.08.04.20.54.25.130000@nowhere.com>
In reply to#8473
On Thu, 04 Aug 2011 13:53:06 -0400, Victor Bazarov wrote:

>>> If the C++ code uses exceptions, you're in a world of pain. It's
>>> borderline impossible for this to be automated. C++ exceptions are the
>>> one C++ feature that does not map nicely onto C.
>>
>> Pardon my ignorance (I've never written a C++ compiler), but what's the
>> problem with exceptions? I'd imagine them to be rather straightforward
>> to implement using setjmp()/longjmp().
> 
> I am thinking the complexity is in "stack unwinding", due to the need to 
> destruct all local objects still "alive" between the 'try' and 'throw'. 
>   IOW, it's not as simple as just transferring control from one part of 
> the code to the other.  C doesn't have the "stack unwinding" need, you see.

Right. But that's more of an issue with destructors than exceptions per
se. You still need to call destructors on leaving scope, however that
occurs.

The interaction between exceptions and destructors means that you would
need a handler (setjmp) for every block which creates one or more
automatic variables having a destructor, not just for every try/catch.

[toc] | [prev] | [next] | [standalone]


#8481

FromBGB <cr88192@hotmail.com>
Date2011-08-04 15:40 -0700
Message-ID<j1f7e7$di$1@news.albasani.net>
In reply to#8472
On 8/4/2011 10:30 AM, Philipp Klaus Krause wrote:
> Am 02.08.2011 23:45, schrieb Joshua Maurice:
>> On Aug 1, 12:25 am, "crea"<n...@invalid.com>  wrote:
>>> I need to port C++ code to C code. It includes templates and classes. Is
>>> this possible and is there any programs which would do this automatically?
>>> Or any other help regarding this. Thank you
>>
>> You are asking, as explained else-thread, that you are looking for a
>> way to convert the C++ code to maintainable and readable C code.
>>
>> If the C++ code uses exceptions, you're in a world of pain. It's
>> borderline impossible for this to be automated. C++ exceptions are the
>> one C++ feature that does not map nicely onto C.
>
> Pardon my ignorance (I've never written a C++ compiler), but what's the
> problem with exceptions? I'd imagine them to be rather straightforward
> to implement using setjmp()/longjmp().
>

as others have noted, implementing a general-purpose exception-handling 
mechanism for C is, sort-of, a horrible pain.

one has to deal with nested exception handlers;
one has to deal with unwinding;
one has to deal with the ability to pass-along / re-throw exceptions;
one has to deal with mapping exception try/catch blocks in some 
plausible way onto C (itself a big ugly issue);
worse yet if one wants to combine reflection with exceptions (useful 
when implementing a VM, and needs to propagate exceptions across languages);
...


really, there is not any "generally good" way to deal with this that I 
have found...

[toc] | [prev] | [next] | [standalone]


Page 1 of 2  [1] 2  Next page →

Back to top | Article view | comp.lang.c++


csiph-web