LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-16-2008, 11:05 AM   #1
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Rep: Reputation: 30
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?
 
Old 03-16-2008, 11:22 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Is this a homework question?

If you wanted to use an object oriented framework, why are you going from C++ to C?
 
Old 03-16-2008, 11:27 AM   #3
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
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.
 
Old 03-16-2008, 11:59 AM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 03-16-2008, 12:06 PM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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
 
Old 03-16-2008, 03:46 PM   #6
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by jschiwal View Post
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.
 
Old 03-16-2008, 03:48 PM   #7
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by jschiwal View Post
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.
 
Old 03-16-2008, 04:15 PM   #8
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
Old 03-16-2008, 04:36 PM   #9
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Actually matthewg42, it sounds more like a language downgrade. In other words, expressing the supposed "new" features in "old" terms.
ta0kira
 
Old 03-16-2008, 06:50 PM   #10
simplicissimus
Registered User
 
Registered: Mar 2008
Posts: 104

Rep: Reputation: 15
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

Last edited by simplicissimus; 04-02-2008 at 04:39 AM.
 
Old 03-16-2008, 08:20 PM   #11
shambler
Member
 
Registered: Sep 2006
Location: Canada
Distribution: openSUSE 11.3, Xubuntu 10.10, Ubuntu 11.04
Posts: 53

Rep: Reputation: 23
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.
 
Old 03-16-2008, 09:46 PM   #12
Dan04
Member
 
Registered: Jun 2006
Location: Texas
Distribution: Ubuntu
Posts: 207

Rep: Reputation: 37
Quote:
Originally Posted by JMJ_coder View Post
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.
 
Old 03-17-2008, 06:56 AM   #13
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by matthewg42 View Post
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).
 
Old 03-17-2008, 06:58 AM   #14
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by simplicissimus View Post
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 View Post
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?



So it is feasible and ordinary to add on functionality with C code to a C++ project? Cool!
 
Old 03-17-2008, 07:02 AM   #15
JMJ_coder
Member
 
Registered: Apr 2006
Distribution: Fedora
Posts: 478

Original Poster
Rep: Reputation: 30
Hello,

Quote:
Originally Posted by Dan04 View Post
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.
 
  


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
LXer: This week at LWN: Large pages, large blocks, and large problems LXer Syndicated Linux News 0 09-27-2007 11:40 AM
Image converting program Penguin of Wonder Linux - Software 2 02-09-2006 12:08 PM
Large file with c program exvor Programming 4 12-30-2005 09:54 AM
CD Burning Program - Burns without converting to .wav? SuSE_User Linux - Software 1 02-19-2004 05:44 PM
converting from a bash program to a c program Linh Programming 5 08-01-2003 05:47 PM

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

All times are GMT -5. The time now is 11:45 PM.

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