ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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.
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.
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!!!
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!)
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.
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.