Exetools

Exetools (https://forum.exetools.com/index.php)
-   General Discussion (https://forum.exetools.com/forumdisplay.php?f=2)
-   -   Need a guideline in porting cpp to c (https://forum.exetools.com/showthread.php?t=6446)

arkanoid 01-17-2005 09:03

Need a guideline in porting cpp to c
 
How are you doing?
I'm trying to port cpp code to c. It's not a huge code.
But I'm kind of an old style guy so I feel dizzy. I'm studying cpp syntax again cause I forgot it almost.
Anyway, is there any guidance fot this subject? If so please let me know.
I googled the net but with no succeed.
By the way I found Comeau C/C++ compiler can convert cpp source to c.
Does anybody have tried it before? Is it reliable?
Thanks for reading.

omidgl 01-17-2005 14:25

As far as I know most of C++ compilers first convert C++ code to C before compiling.
I've used convertors, none of them is 100% reliable. You can use another way to do your job by static linking . First compile your C++ codes using C++ compilers and make .LIB file. Then you can use extern in your C codes to use methods from your compiled C++ code (.lib)

Your C compiler will link the lib file to your final exe and the job is done.

Regards
OMID

nuemga2000 01-17-2005 15:21

Hi,
it's not quite clear, what you want to do :
- port the C++ source code to C ...
- use your C++ code from some other C stuff ...

Kerstin

arkanoid 01-17-2005 16:53

Thanks for your answers.
My aim is porting cpp code to asm.
Porting to c is an intermediate step.
I want to optimize the code and make it smaller, run faster and so on.
It would be better if I can convert it directly to asm.
But It looks little bit hard (for me) because of many injected codes by cpp compiler.
I think I need to understand the code first.
That's why I'm porting it to c.
But many features of cpp are hard to port to c.
I just wonder if there are any tips or guidances for doing that.

Kerlingen 01-17-2005 20:30

If you want to port C++ into ASM you should use the Borland C++ compiler. It has an option called "compile via assembly" (commandline switch "-B"). This first translates the CPP file to ASM and then passes it to TASM32.

If you rename TASM32.EXE to something else you will get an error message, but BCC32 won't delete the ASM file so you can take the code you want out of it.

dmownz 01-17-2005 21:25

The original C++ compiler by Bjarne Stroustroup (called cfront) converts c++ to c. You might be able to find it by searching for cfront on the net.

infern0 01-17-2005 21:28

Quote:

Originally Posted by Kerlingen
If you want to port C++ into ASM you should use the Borland C++ compiler.

all compilers has such feature. For borland switch are -B, for ms VC -Fa (generally -F?).

arkanoid 01-17-2005 22:58

Wow it works.
Although it looks messy, anyway I got an asm form of it.
I'm ashamed of my ignorance.
Thank you again. You guys really helped me.

mihaliczaj 01-17-2005 23:46

there is a good book on this
 
There is a book
Stanley B. Lippman : Inside the C++ Object Model
that goes through the C++ language elements one by one and shows how they are actually compiled to C (based on the cfront compiler).
It covers all the issues including virtual base classes and all kinds of member function pointers as well.
Quote:

Originally Posted by omidgl
As far as I know most of C++ compilers first convert C++ code to C before compiling.

This was true for cfront, but when new language elements (exceptions for example) appeared it was no longer possible to do it that way. The book mentioned above describes the details.

AFAIK I have this book in digital format. PM me if you want it to be uploaded or e-mailed to you.

arkanoid 01-18-2005 12:17

You mean "Inside the C++ Object Model"?
Title is little different but same author.
I could find it on the net.
Thank you for your concern.

nuemga2000 01-18-2005 15:52

Quote:

Originally Posted by arkanoid
Thanks for your answers.
My aim is porting cpp code to asm.
Porting to c is an intermediate step.
I want to optimize the code and make it smaller, run faster and so on.
It would be better if I can convert it directly to asm.
But It looks little bit hard (for me) because of many injected codes by cpp compiler.
I think I need to understand the code first.
That's why I'm porting it to c.
But many features of cpp are hard to port to c.
I just wonder if there are any tips or guidances for doing that.

I don't think, that it really makes sense just to use some compilers asm output.
To get a real benefit, you should try to catch the algorithm(s) used and reimplement them from scratch ...

Kerstin

arkanoid 01-18-2005 16:02

nuemga2000 // That's what I'm talking about. I agree with you.
I'm still trying to understand that code.
Compiler produced asm code is just a reference. Better than nothing.

mihaliczaj 01-18-2005 20:36

Quote:

Originally Posted by arkanoid
You mean "Inside the C++ Object Model"?

Yes, I meant that, I'd just forgotten the exact title, thanks for telling, I've corrected my post.

Are you sure that the asm code is the best way to understand the code? You loose all the info that is buried in the source code (variable/function names etc.).
Isn't it easier simply debug the application step by step? Even if you are not a C++ expert this would be much easier IMHO. In most debuggers there is a mixed C++/asm view where you can debug the asm if you want, and you can see to what asm code the C++ source is compiled.

arkanoid 01-18-2005 22:26

Well, I'll rewrite it with asm from the scratch. So It should be free from variable/function name problem.
And of course I'm using debugger almost everytime along with cpp source and compiler produced asm source. Compiler produced asm source has name information so it's worth to take a look.


All times are GMT +8. The time now is 01:27.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX