LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C++ code to C (https://www.linuxquestions.org/questions/programming-9/c-code-to-c-196912/)

LuderForChrist 06-23-2004 03:21 PM

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.

jwstric2 06-23-2004 03:58 PM

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.

The_Nerd 06-23-2004 06:47 PM

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.

jwstric2 06-23-2004 07:17 PM

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

LuderForChrist 06-24-2004 08:17 AM

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.

itsme86 06-24-2004 08:48 AM

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.

LuderForChrist 06-24-2004 01:46 PM

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

The_Nerd 06-24-2004 03:20 PM

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!

The_Nerd 06-24-2004 03:21 PM

Btw, keep in mind that #defines will most likely be your best friend in a chalange such as this. Again, good luck.

jwstric2 06-24-2004 03:38 PM

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

LuderForChrist 06-24-2004 03:40 PM

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

LuderForChrist 06-24-2004 03:51 PM

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.

itsme86 06-24-2004 04:45 PM

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


All times are GMT -5. The time now is 05:52 PM.