Converting C source to C++

前端 未结 11 1998
迷失自我
迷失自我 2021-01-30 16:52

How would you go about converting a reasonably large (>300K), fairly mature C codebase to C++?

The kind of C I have in mind is split into files roughly corresponding to

相关标签:
11条回答
  • 2021-01-30 17:20

    What about:

    1. Compiling everything in C++'s C subset and get that working, and
    2. Implementing a set of facades leaving the C code unaltered?

    Why is "translation into C++ mandatory"? You can wrap the C code without the pain of converting it into huge classes and so on.

    0 讨论(0)
  • 2021-01-30 17:24

    GCC is currently in midtransition to C++ from C. They started by moving everything into the common subset of C and C++, obviously. As they did so, they added warnings to GCC for everything they found, found under -Wc++-compat. That should get you on the first part of your journey.

    For the latter parts, once you actually have everything compiling with a C++ compiler, I would focus on replacing things that have idiomatic C++ counterparts. For example, if you're using lists, maps, sets, bitvectors, hashtables, etc, which are defined using C macros, you will likely gain a lot by moving these to C++. Likewise with OO, you'll likely find benefits where you are already using a C OO idiom (like struct inheritence), and where C++ will afford greater clarity and better type checking on your code.

    0 讨论(0)
  • 2021-01-30 17:26

    If you have a small or academic project (say, less than 10,000 lines), a rewrite is probably your best option. You can factor it however you want, and it won't take too much time.

    If you have a real-world application, I'd suggest getting it to compile as C++ (which usually means primarily fixing up function prototypes and the like), then work on refactoring and OO wrapping. Of course, I don't subscribe to the philosophy that code needs to be OO structured in order to be acceptable C++ code. I'd do a piece-by-piece conversion, rewriting and refactoring as you need to (for functionality or for incorporating unit testing).

    0 讨论(0)
  • 2021-01-30 17:27

    You mention that your tool is a compiler, and that: "Actually, pattern matching, not just type matching, in the multiple dispatch would be even better".

    You might want to take a look at maketea. It provides pattern matching for ASTs, as well as the AST definition from an abstract grammar, and visitors, tranformers, etc.

    0 讨论(0)
  • 2021-01-30 17:30

    I would write C++ classes over the C interface. Not touching the C code will decrease the chance of messing up and quicken the process significantly.

    Once you have your C++ interface up; then it is a trivial task of copy+pasting the code into your classes. As you mentioned - during this step it is vital to do unit testing.

    0 讨论(0)
提交回复
热议问题