Thread: I'm converting a C++ project to C

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    35

    I'm converting a C++ project to C

    The major issues are as follows:

    structs/classes -
    classes with functions not allowed in C so I have to pull them out and rename them/change parameters
    no constructors or destructors
    so I need init and destroy methods

    new/delete -
    all of these have to be replaced with malloc/free with corresponding init and destroy calls.

    the use of booleans has to be corrected since there is none in C, but this is fixable with a simple typedef and find/replace.

    the biggest problem is templates.

    how the hell do I get rid of template data structures/classes elegantly. almost every solution that I can think of is beyond sloppy

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you have code that relies heavily on templates, then you are probably going to end up with a lot of hard work. Simple templates can sometimes be replaced by #define macros, but it's tricky and far from trivial. Some ways of using templates is almost impossible to replace.

    What is the reason you feel the need to translate to C anyways - don't tell me speed - if that's your reasoning, then find out where the C++ code is actually spending large amounts of time, and rewrite those bits of code to something that takes less time - you can always write it like in C for these portions, avoiding all the OTHER bits that you would have to change simply because the compiler doesn't support it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    35
    Well realistically it appears that my only option is to go through the existing code, and find each type of item stored in a given template data structure and add each type to a union and use that as the data objects. Then I can add a bit field to the data structure to be set on initialization which would specify the type of data stored in an instance of the structure.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    35
    umm, well it's a library that needs to be integrated to work with some legacy code...

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, would it be possible to wrap the library itself in some C wrapper functions - that way, only those portions of the code that actually are exposed out of the library would need changing, rather than ALL the code.

    I have done simpler porting jobs, such as translating Pascal to Modula-2 or C, and even then, it's quite hard. C++ has A LOT of things that are really difficult to replace with standard C code without some helper functionality - one option is of course to use Cfront [assuming you can find a copy that is standards compliant] to create C code - however, that would of course lead to pretty horrible and unportable code.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Would it not be easier to convert the C code to work with the C++ library?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    What you might want to do then is to create a set of wrapper functions. The template stuff might be a little difficult to do but should be possible. As long as the function signature is legal in C it really doesn't matter if the function body is C or C++.


    edit: Bah I'm slow at posting today

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    35

    Wink

    Quote Originally Posted by laserlight View Post
    Would it not be easier to convert the C code to work with the C++ library?
    I would imagine so, but that's not mine to tamper with

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    35
    Quote Originally Posted by Thantos View Post
    What you might want to do then is to create a set of wrapper functions. The template stuff might be a little difficult to do but should be possible. As long as the function signature is legal in C it really doesn't matter if the function body is C or C++.


    edit: Bah I'm slow at posting today
    I think you are mistaken

  10. #10
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Nope. If I have a function
    Code:
    int foo(int par1, char par2, double par3);
    Then I can call it from a C program. In the body of foo I could go to town on all the C++ code I want. I simply compile the file with foo into an object file, compile the C program into object code and then link them together.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    35
    Quote Originally Posted by Thantos View Post
    Nope. If I have a function
    Code:
    int foo(int par1, char par2, double par3);
    Then I can call it from a C program. In the body of foo I could go to town on all the C++ code I want. I simply compile the file with foo into an object file, compile the C program into object code and then link them together.
    I see what your intention is, however linking is something that requires overhead as well. I've essentially done something similar with managed C++ and C# and I ran into some issues that were not worth while.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by gotclout View Post
    I think you are mistaken
    Well, it obviously depends on a lot of things. Maybe you can explain further why you think this is the case in your particular situation.

    I can certainly say that C and C++ can be successfully mixed in various ways. You may need a bit of glue if your C-library is C only, and not C++ capable, and some restrictions may be needed to support such a thing - but it's highly likely that this work is simpler than porting existing and correct C++ code into C code. The reason being that the glue-code and restriction parts are fairly easy to identify, and a relatively small portion - whilst it's likely that almost ALL of the C++ code would have to be modified to port it to C - which leads to great risk of introducing bugs (and it's usually subtle bugs that only affect corner cases).

    Where I work, we have C code that calls C++ code - the C code is a 3D graphics driver for a hardware chip (we also have a software only 3D graphics driver, also written in C), which then interfaces to the main OS which is in C++. This is an embedded OS, so it doesn't have a normal malloc/free in a C-library, and we have to actually interface the C++ low-level allocation routines (the same ones that new is based on) to make the C code have malloc and friends.

    I have also worked on a Windows graphics driver that uses a C++ chip-simulator to simulate a not-yet-existing 3D graphics chip. Windows (NT and earlier at least) do not normally support C++-code in kernel drivers. So we had to work around those bits. But it's entirely possible to do, if you follow some restrictions and limitations.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by gotclout View Post
    I see what your intention is, however linking is something that requires overhead as well. I've essentially done something similar with managed C++ and C# and I ran into some issues that were not worth while.
    Linking between two functions in different files require no more overhead then two functions in the same file. The calling method is the same.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Between C and C++ should be no problem, and as Thantos says, little performance loss (not enough to notice unless you are doing some really huge amounts of calling back and forth). C++ managed and C#, I have no idea what that does (other than being managed it has some overhead in itsel).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    To call C++ functions in C code you need to prefix your C++ functions with extern "C" so that the linkage will be compatible with C.
    Code:
    extern "C" int func(int something);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM