converting C to C++



  • I have a very BASIC and ESSENTIAL QUESTION.
    I have been asked by a researcher to convert some old code he has from C to C++. The code was last touched in 1993. My first question is: how can this request be useful? Aren't the current C++ compilers able to compile and run the old code, as is? What are the advantages of the expected C++ version? Is it a common task to ask programmers/developers to convert old C codes to C++? Are there any automatic converters available?
    There are some Tiff-image related functions that are NOT functioning properly, may that reveal the reason behind the request(the old Tiff format?
    Please do help me understand to get motivated and directed.



  • Normally a C++ Compiler has not much trouble compiling C code - except if this Code was written in die old K&R style, for example:

    foo()
    int a
    {
    /*do something*/
    }
    

    C++ compiler normally can't parse this.

    And in old C you do not need prototypes for functions - you can call them even if the compiler doesn't know them. Here cproto helps a lot (it generates the prototypes automatically).

    Normally it shouldn't be too difficult to move Code from C89 (the old standard, besides the even older K&R C) to C++. It's mostly only some minor Syntax Adaption and you hardly need to touch the semantic of the code.



  • hi,
    it depends on how many codes you have...
    you might get weird errors (c++ can't handle void* correctly and/or you need some c-features that are absent in c++). if there are too much errors, compile the code with a c-compiler and put prototypes into a .h file like this:

    #ifdef __cplusplus
    extern "C"
    {
    #endif
      // put the prototypes hehe
      ...
      ...
    #ifdef __cplusplus
    } // extern "C" closing brace
    #endif
    

    #include the .h into your c++ files
    now you can call them from c++ code without worry.

    but always keep in mind: "programming in c++ is premature optimization" 😉
    ...and look at this page: http://david.tribble.com/text/cdiffs.htm


Anmelden zum Antworten