LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Converting a large program from C++ to C (https://www.linuxquestions.org/questions/programming-9/converting-a-large-program-from-c-to-c-628434/)

JMJ_coder 03-16-2008 11:05 AM

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?

jschiwal 03-16-2008 11:22 AM

Is this a homework question?

If you wanted to use an object oriented framework, why are you going from C++ to C?

Mara 03-16-2008 11:27 AM

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.

matthewg42 03-16-2008 11:59 AM

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.

ta0kira 03-16-2008 12:06 PM

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

JMJ_coder 03-16-2008 03:46 PM

Hello,

Quote:

Originally Posted by jschiwal (Post 3090355)
Is this a homework question?

If you wanted to use an object oriented framework, why are you going from C++ to C?

Not in the least. This is a real world question.

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.

JMJ_coder 03-16-2008 03:48 PM

Hello,

Quote:

Originally Posted by jschiwal (Post 3090355)
Is this a homework question?

Hey, if you did want to do my homework - I have to write a mathematical expression evaluator using stacks in C++ that is due by Thursday. :p

matthewg42 03-16-2008 04:15 PM

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.

ta0kira 03-16-2008 04:36 PM

Actually matthewg42, it sounds more like a language downgrade. In other words, expressing the supposed "new" features in "old" terms.
ta0kira

simplicissimus 03-16-2008 06:50 PM

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

shambler 03-16-2008 08:20 PM

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.

Dan04 03-16-2008 09:46 PM

Quote:

Originally Posted by JMJ_coder (Post 3090596)
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.

My Data Structures course in college always gave its assignments in pairs:

(A) Write a program in Java to do ____.
(B) Rewrite program A in C.

It really helps you understand both languages.

JMJ_coder 03-17-2008 06:56 AM

Hello,

Quote:

Originally Posted by matthewg42 (Post 3090621)
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.

The features I wish to implement are significant - of the order of differing notational systems. There are two I am looking at - one is semi-implemented; that is it is there but not perfected, and from what I hear, it is no longer under active development. The other isn't implemented at all (as far as I know).

JMJ_coder 03-17-2008 06:58 AM

Hello,

Quote:

Originally Posted by simplicissimus (Post 3090698)
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

Quote:

Originally Posted by shambler (Post 3090768)
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.

How did you know that that was going to be my next question. Do you have predictions on this week's lotto drawing? :D



So it is feasible and ordinary to add on functionality with C code to a C++ project? Cool!

JMJ_coder 03-17-2008 07:02 AM

Hello,

Quote:

Originally Posted by Dan04 (Post 3090836)
My Data Structures course in college always gave its assignments in pairs:

(A) Write a program in Java to do ____.
(B) Rewrite program A in C.

It really helps you understand both languages.

I had to do that with my shell scripting class: Write script in Bourne shell and then a week later rewrite in C shell.

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.