Converting a large program from C++ to C
Hello,
What level of difficulty is it to convert a large program (let's say over 50,000 lines of code) from C++ to C. Let's say it was develop in an Object-Oriented framework. Let's assume it is non-graphical. Is this even possible? If so, how hard would it be? How much time would a developer have to dedicate to this (assuming only one developer)? Would this be feasible? Would it be beneficial? What motivations and expectations would warrant such a conversion? What motivations and expectations would make such a conversion inappropriate? Would it make a difference if it wasn't developed in Object-Oriented code? |
Is this a homework question?
If you wanted to use an object oriented framework, why are you going from C++ to C? |
Everything depends on the details. For instance, if templates were used. Code without them would be much easier to port. The same if there's much polymorphism. Simple move from classes to structures with pointers is not hard. If there are more subtle changes, it becomes harder.
Beneficial? Depends. If it's going to fit into large existing code base, it would probably require much more changes at basic (algorithmic) level. Speed? With current compilers rather not. There may be gain in output executable size. |
It totally depends on the design of the program. If it was written "in the C++ way" then you would probably end up re-writing most of it. C++ is a superset of C, so if it was C++ but just happened to use mostly regular C, then it would be a lot easier.
|
This will convert standard C++ code into C: http://www.comeaucomputing.com/, but with the intention of something like gcc compiling it later on. Obviously it wouldn't be maintainable, but you never specified that it needed to be. There's really no feasible way to convert C++ to identically-functioning C without using a compiler intended to do that.
Barring a machine-readable direct conversion to C, you still need to decide with what losses you are willing to convert. STL containers would be a nightmare to convert to C (and their associated algorithms,) and virtual functions and virtual base classes would drive you to just port a C++ compiler to this mythical C++-free world. I actually convert C++ to C wherever possible in my non-trivial projects, but some things just aren't feasible. If you have 50k lines of C++, chances are at least half uses either templates or virtual something-or-other. If it doesn't, chances are it uses one or more C APIs such as the POSIX API, making it not truly a C++-only project. The first thing I always think about when considering C vs. C++ is memory management. If I have to deal with lists or dynamic data I'll almost always choose C++ even if I have to give it a C interface in the header. There's really no reason to write lists, etc. in C unless you're writing kernel code or your own init. Doing so is not only a futile effort; it increases potential security holes exponentially. ta0kira |
Hello,
Quote:
There is an open-source project (Lilypond) that is very, very good. But, there are a couple features that I need and want that aren't developed as well as the rest of the program (I believe that they've halted development on these features). I've also been looking at the differences between C and C++ - and it is still very confusing. But, C seems to be closer to my thinking of programming - efficient code, memory management, etc. Maybe I still don't have a grasp on the differences and similarities, but that's how my thinking is now. So this thread is just to give myself an idea of what I'm looking at from more experienced programmers. Do I contact the project and ask them if I can help out on those features? Do I break off a new project maintaining it in C++ and just adding on in C++ those features I desire? Do I break off a new project and convert it to C adding those features I desire? I don't know how large it is exactly - it's no 100 line program. It spans several dozen source files. I don't know if it is in OO or programmed in "the C++ way". I wanted to get a feel if this was even possible or worthwhile before I contemplated it any further. |
Hello,
Quote:
|
Re-writing a large project just to implement a few new features in a language you have some vauge preference for seems like a lot of effort for such a small goal. Generally speaking I would only consider a re-write in a new language is there was really no way which some new features were possible using the existing language.
|
Actually matthewg42, it sounds more like a language downgrade. In other words, expressing the supposed "new" features in "old" terms.
ta0kira |
mixing C and C++
My honest suggestion would be to keep the source base as it is (in C++) and rather work towards a solution that extends the application in some modular way or maybe even plugins loaded at runtime. The additional modules could than be developed in plain C, if that's what you like. C and C++ do mix well (mostly).
This way you can focus on functionality that you need, without bothering code that works already. Rewriting code will most often introduce many new bugs, and even if the new code is perfect you still have to stress-test the whole thing to know for sure. That alone takes a lot of time. Regards, SIMP Debian User |
I'm with simplicissimus on this. Nothing stops you from adding all the functionality you want using C.
Remember that you can complile in C modules and call c functions from c++ directly. Nothing much special or difficult there. You may also find you learn some interesting and useful things by seeing how the C++ code is assembled. If you are really hung up on converting to C, you can find an old-style C++ compiler (probably for free). Originally, C++ was fed into a preprocessor called a CFRONT. The CFRONT preprocessor converts the C++ to pure C. As others mentioned, it may not be particularly readable. Just an aside, but the I've found that learning other languages gave me greater insight into the languages I already knew and made me a better programmer in them. |
Quote:
(A) Write a program in Java to do ____. (B) Rewrite program A in C. It really helps you understand both languages. |
Hello,
Quote:
|
Hello,
Quote:
Quote:
So it is feasible and ordinary to add on functionality with C code to a C++ project? Cool! |
Hello,
Quote:
It is a bit different when the problem is write a program that will calculate a tip for a waiter; do it in Java, then rewrite in C - and when it is write OpenOffice in Java; then rewrite it in C. Same concept, differing degrees of difficulty - and from the responses here, it seems it quickly becomes a degree of insanity. |
All times are GMT -5. The time now is 12:10 AM. |