Exetools  

Go Back   Exetools > General > General Discussion

Notices

Reply
 
Thread Tools Display Modes
  #1  
Old 01-17-2005, 09:03
arkanoid
 
Posts: n/a
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.
Reply With Quote
  #2  
Old 01-17-2005, 14:25
omidgl omidgl is offline
Friend
 
Join Date: Jul 2004
Posts: 86
Rept. Given: 10
Rept. Rcvd 4 Times in 3 Posts
Thanks Given: 0
Thanks Rcvd at 5 Times in 5 Posts
omidgl Reputation: 4
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
Reply With Quote
  #3  
Old 01-17-2005, 15:21
nuemga2000 nuemga2000 is offline
Friend
 
Join Date: Jan 2002
Posts: 59
Rept. Given: 1
Rept. Rcvd 2 Times in 2 Posts
Thanks Given: 0
Thanks Rcvd at 5 Times in 5 Posts
nuemga2000 Reputation: 2
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
Reply With Quote
  #4  
Old 01-17-2005, 16:53
arkanoid
 
Posts: n/a
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.
Reply With Quote
  #5  
Old 01-17-2005, 20:30
Kerlingen
 
Posts: n/a
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.
Reply With Quote
  #6  
Old 01-17-2005, 21:25
dmownz
 
Posts: n/a
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.
Reply With Quote
  #7  
Old 01-17-2005, 21:28
infern0 infern0 is offline
Friend
 
Join Date: Dec 2004
Posts: 72
Rept. Given: 4
Rept. Rcvd 0 Times in 0 Posts
Thanks Given: 0
Thanks Rcvd at 2 Times in 2 Posts
infern0 Reputation: 0
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?).
Reply With Quote
  #8  
Old 01-17-2005, 22:58
arkanoid
 
Posts: n/a
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.
Reply With Quote
  #9  
Old 01-17-2005, 23:46
mihaliczaj
 
Posts: n/a
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.

Last edited by mihaliczaj; 01-18-2005 at 20:26.
Reply With Quote
  #10  
Old 01-18-2005, 12:17
arkanoid
 
Posts: n/a
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.
Reply With Quote
  #11  
Old 01-18-2005, 15:52
nuemga2000 nuemga2000 is offline
Friend
 
Join Date: Jan 2002
Posts: 59
Rept. Given: 1
Rept. Rcvd 2 Times in 2 Posts
Thanks Given: 0
Thanks Rcvd at 5 Times in 5 Posts
nuemga2000 Reputation: 2
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
Reply With Quote
  #12  
Old 01-18-2005, 16:02
arkanoid
 
Posts: n/a
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.
Reply With Quote
  #13  
Old 01-18-2005, 20:36
mihaliczaj
 
Posts: n/a
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.
Reply With Quote
  #14  
Old 01-18-2005, 22:26
arkanoid
 
Posts: n/a
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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Ollydbg v1 to v2 porting question sendersu General Discussion 0 09-02-2014 13:29
porting GNU tdestroy() function to SPARC Solaris WhoCares General Discussion 3 01-07-2005 23:53


All times are GMT +8. The time now is 05:30.


Always Your Best Friend: Aaron, JMI, ahmadmansoor, ZeNiX, chessgod101
( Since 1998 )