LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-23-2004, 03:21 PM   #1
LuderForChrist
Member
 
Registered: Jun 2004
Posts: 37

Rep: Reputation: 15
C++ code to C


I am working on a project converting C++ code to C.

I understand that there are no classes in C but in the code I have(C++), there is pretty much every type of object code(classes, structs, functions, .hpp, .cpp ect ect) I was wondering if anyone could help give me a few pointers on ways to take the C++ code and make it C.

Thank you so much.

OH my knowledge in C++ is pretty decent, but my knowledge in C is only minimal.

Thanks again.
 
Old 06-23-2004, 03:58 PM   #2
jwstric2
Member
 
Registered: Jan 2004
Posts: 105

Rep: Reputation: 15
I know there is a commercial cfront compiler to accmplish this task, expensive ass license though. Google around for c+ or c translators and see what you get. Maybe someone started an open source compiler project.
 
Old 06-23-2004, 06:47 PM   #3
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Why??? You can compile C code with a C++ compiler without any complaints. C++ is just a better version of C. I don't know why you would ever want to go to the trouble to do the "minimal" conversion necisary to compile it on a compiler that is probably out of date anyway.
 
Old 06-23-2004, 07:17 PM   #4
jwstric2
Member
 
Registered: Jan 2004
Posts: 105

Rep: Reputation: 15
Yeah true, most c code will compile with c++ compiler without complaints. Going the other way however is not so friendly.

Take printing to stdout in c++

cout << "Linux \n"

If LuderForChrist wants to do the conversion just let him. Perhaps he's writing this for an older system or converting it into a linux module. Anyways I really hope this thread doesn't turn into one of these c/c++ war threads.

http://www.gamedev.net/community/for...topic_id=94411
 
Old 06-24-2004, 08:17 AM   #5
LuderForChrist
Member
 
Registered: Jun 2004
Posts: 37

Original Poster
Rep: Reputation: 15
Yea I am asked to take it from C++ to C so that this other person can take it from C to java.

Thanks again for all the help, any other ideas are welcome.
 
Old 06-24-2004, 08:48 AM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I don't quite understand that. C++ and Java both follow an object oriented model while C doesn't. Going from C++ to C will probably require rewriting things in a not so object oriented way. It seems a C++ to Java conversion would be easier than (or at least as easy as) a C to Java conversion.

P.S. Whether or not C++ is better than C is a matter of much debate. I much prefer C to C++ personally. I don't understand the "out of date compiler" statement either.

Last edited by itsme86; 06-24-2004 at 08:50 AM.
 
Old 06-24-2004, 01:46 PM   #7
LuderForChrist
Member
 
Registered: Jun 2004
Posts: 37

Original Poster
Rep: Reputation: 15
Yea getting rid of classes and then recreating them seems a little wierd to me to but the boss told me he couldn't just do a direct C++ to java but if could get it to C, he could easily take it to Java.

Maybe the boss doesn't know C++, or maybe he is keeping me busy, because I dont know java yet.

Still any ideas on what to do with the C++ classes? structs?
EXAMPLES would be wonderful!!!

Thanks again all
 
Old 06-24-2004, 03:20 PM   #8
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Well I suggest the following for classes. I don't know how the heck you would do operator overloading though... Did you ever think it might be easier to go from assembly to Java? Or even assembly to C. Because gcc, or any compiler for that matter can give you the assembly for the code. Anyhow, here it is:

C++:
Code:
class MyClass
{
    public:
        int MyInt;
        int Func(int, int);
};

MyClass T;
T.Func(T.MyInt, 0);
would be in C:

Code:
int Func(int i, int j)
{
}

struct MyClass
{
    int MyInt;
    int (*Func)(int, int);
};

struct MyClass T={0, Func);

Now to can be used as it where a class:

T.Func(T.MyInt, 0);
All I can say, is that when you are done with this, it will be a compiler in it self... (I hate to think of templates and function overloading... OW!)

Good luck!
 
Old 06-24-2004, 03:21 PM   #9
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Btw, keep in mind that #defines will most likely be your best friend in a chalange such as this. Again, good luck.
 
Old 06-24-2004, 03:38 PM   #10
jwstric2
Member
 
Registered: Jan 2004
Posts: 105

Rep: Reputation: 15
No offense, but your boss sounds like a moron. C++ to Java conversion would be god knows a lot easier. I know for a fact those translators exist under the GNU public license. Google for Cappuccino or c++ to java translator, I got a crap load of hits. If you want ot impress him take it straight to Java with ease.

But if you must do it...

1. as I mention earlier all stdout,stderr in the form cout<<"hey" need be converted into fprintf(stdout, "hey"); ect...
2. Yes as you mention the classes are going to be well sent to the great coe graveyard. Take for example the following:

class Rectangle {
int x, y;
public:
void set_values (int,int);
} rect;

void Rectangle::set_values (int a, int b) {
x = a;
y = b;
}

To convert to C I would do the following,.

/*Header declaration file*/
struct Rectangle{
int x;
int y;
};

void set_values(struct Rectangle *my_rectangle, int x, int y);
/*End of header declartion file*/

/*Code*/
void set_values(struct Rectangle *my_rectangle, int x, int y){
my_rectangle->x=x;
my_rectangle->y =y;
}
/*End of Code*/

3. There is no automatic construct. (As in above Rectangle rect (3,4)) Need to setup a function to return a contruct or initialize yourself as shown above from a previous post before I responeded.

A lot of the stuff you are going to be doing is going to be tedious. All your functions can not be called object.method anymore. You must pass a pointer to the function of the to be manipulated object. Private class variables must be kept in the structe in special addr buffers that only the functions can manipulate.

Good luck

Last edited by jwstric2; 06-24-2004 at 03:41 PM.
 
Old 06-24-2004, 03:40 PM   #11
LuderForChrist
Member
 
Registered: Jun 2004
Posts: 37

Original Poster
Rep: Reputation: 15
Thanks again, see ya all in the morning, just about 5 here.

Edit: I would take it straight to Java, but I dont know java, although I heard its just like C++.

Last edited by LuderForChrist; 06-24-2004 at 03:44 PM.
 
Old 06-24-2004, 03:51 PM   #12
LuderForChrist
Member
 
Registered: Jun 2004
Posts: 37

Original Poster
Rep: Reputation: 15
Sorry one other Question:

Do I need the #ifndef, #define, #endif in my C code like I do in my C++? Never done header files in C.
 
Old 06-24-2004, 04:45 PM   #13
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
The_Nerd had a good suggestion (I think) to making your C structs feel more like C++ classes. Using function pointers in the structs can help it feel more like you're calling a method. It might be easier to do your conversions this way. That way the code that utilizes the classes won't have to be modified (much).
 
  


Reply



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
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Editing buttons (quote, code etc.) add the code to the end vharishankar LQ Suggestions & Feedback 2 09-13-2004 09:32 AM
Diffrerence between position independent code and Relocatable code? eshwar_ind Programming 7 05-11-2004 01:40 AM
Open Firmware code for booting OS from SATA : sample code available somewhere ? drsparikh Linux - Hardware 0 03-12-2004 11:16 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration