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


Groups > comp.lang.c++ > #8395

Re: Is there any software porting C++ to C?

From BGB <cr88192@hotmail.com>
Newsgroups comp.lang.c++
Subject Re: Is there any software porting C++ to C?
Date 2011-08-01 14:18 -0700
Organization albasani.net
Message-ID <j175fc$31s$1@news.albasani.net> (permalink)
References (1 earlier) <99n3n3Faa6U3@mid.individual.net> <gSsZp.127986$fQ4.108328@newsfe16.ams2> <99n9q2Faa6U4@mid.individual.net> <H1vZp.67851$Ll1.46368@newsfe24.ams2> <7lmd37la28p9tgov91qo6bipaaa7p6dlrq@4ax.com>

Show all headers | View raw


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.

Back to comp.lang.c++ | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web