Is there any software porting C++ to C?

C

crea

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
 
I

Ian Collins

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

crea

Ian Collins said:
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?
 
Joined
Aug 1, 2011
Messages
1
Reaction score
0
I am looking for a web based Load/stress testing tool for asp.net. Let me know if you have any.

Nagaraju6
 
J

Juha Nieminen

crea said:
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.
 
B

Bo Persson

crea said:
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
 
C

crea

Bo Persson said:
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 :) .
 
I

Ian Collins

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?
 
C

crea

Juha Nieminen said:
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...
 
M

Marcel Müller

crea said:
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
 
C

crea

Ian Collins said:
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...
 
B

BGB

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

Joshua Maurice

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

Uwe Dolinsky

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

Codeplay's Offload toolkit (http://www.codeplay.com/games/offload/)
compiles C++ with GNU, Altivec and other extensions to C expanding all
templates etc. The generated C code can then be compiled using gcc and
subsequently linked with C++ libraries (if the original C++ file used
any C++ libraries).
At the moment Offload is available for PS3- licensed developers only,
but we are considering resurrecting the free release of the Offload
toolkit for Cell Linux.

Uwe Dolinsky
Codeplay
 
P

Philipp Klaus Krause

Am 02.08.2011 23:45, schrieb Joshua Maurice:
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
 
V

Victor Bazarov

Am 02.08.2011 23:45, schrieb Joshua Maurice:

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
 
W

Waldek M.

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
 
J

Jens Thoms Toerring

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
 
N

Nobody

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

BGB

Am 02.08.2011 23:45, schrieb Joshua Maurice:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top