Having just started on pretty much the same thing a few months ago (on a ten-year-old commercial project, originally written with the "C++ is nothing but C with smart structs" philosophy), I would suggest using the same strategy you'd use to eat an elephant: take it one bite at a time. :-)

As much as possible, split it up into stages that can be done with minimal effects on other parts. Building a facade system, as Federico Ramponi suggested, is a good start -- once everything has a C++ facade and is communicating through it, you can change the internals of the modules with fair certainty that they can't affect anything outside them.

We already had a partial C++ interface system in place (due to previous smaller refactoring efforts), so this approach wasn't difficult in our case. Once we had everything communicating as C++ objects (which took a few weeks, working on a completely separate source-code branch and integrating all changes to the main branch as they were approved), it was very seldom that we couldn't compile a totally working version before we left for the day.

The change-over isn't complete yet -- we've paused twice for interim releases (we aim for a point-release every few weeks), but it's well on the way, and no customer has complained about any problems. Our QA people have only found one problem that I recall, too. :-)

Answer from Head Geek on Stack Overflow
🌐
CodeConvert AI
codeconvert.ai › c-to-c++-converter
Free C to C++ Converter — AI Code Translation | CodeConvert AI
This tool can convert a wide range of C code to C++, from simple functions and algorithms to complete programs with classes, error handling, and complex logic.
Top answer
1 of 11
16

Having just started on pretty much the same thing a few months ago (on a ten-year-old commercial project, originally written with the "C++ is nothing but C with smart structs" philosophy), I would suggest using the same strategy you'd use to eat an elephant: take it one bite at a time. :-)

As much as possible, split it up into stages that can be done with minimal effects on other parts. Building a facade system, as Federico Ramponi suggested, is a good start -- once everything has a C++ facade and is communicating through it, you can change the internals of the modules with fair certainty that they can't affect anything outside them.

We already had a partial C++ interface system in place (due to previous smaller refactoring efforts), so this approach wasn't difficult in our case. Once we had everything communicating as C++ objects (which took a few weeks, working on a completely separate source-code branch and integrating all changes to the main branch as they were approved), it was very seldom that we couldn't compile a totally working version before we left for the day.

The change-over isn't complete yet -- we've paused twice for interim releases (we aim for a point-release every few weeks), but it's well on the way, and no customer has complained about any problems. Our QA people have only found one problem that I recall, too. :-)

2 of 11
14

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.

Discussions

How to convert C++ Code to C - Stack Overflow
The front end of a compiler analyzes ... e.g. C++ is designed to help the human programmer to write code, the intermediary form is designed to help simplify the algorithm that analyzes said intermediary form easier). The back end of a compiler takes the intermediary form and then converts it to a 'target ... More on stackoverflow.com
🌐 stackoverflow.com
How to convert C++ Code to C

Pretty much, yes.

The classes become structs with a (separate) set of functions; usually you prefix the names so it’s clear what they act on. You’ll need to to manual de-/initialization too. E.g.,

class C {
private:
    int x;
public:
    C() : x(0) {}
    C(const C &c) : x(c.x) {}
    C(int xv) : x(xv) {}
    int getX() const {return x;}
};

becomes something like

typedef struct C C;
struct C {
    int x;
    /* If you need to, you can also partition this like
    struct {
        int x;
    } private__;
    but it’s not like the C compiler will actually enforce privateness. */
};
void C_init_1(C *const inst) {inst->x = 0;}
void C_copy(C *const inst, const C *const c) {inst->x = c->x;}
void C_init_2(C *const inst, int cv) {inst->x = cv;}
int C_getX(const C *const inst) {return inst->x;}

If C extends something, it has to start with a copy of that thing; e.g.,

class D : public E, public F {…};

becomes

typedef struct D D;
struct D {
    E super_E;
    F super_F;
};

so static upcasting from D to E becomes &inst->super_E and the reverse is something like (struct D *)((char *)inst - offsetof(D, super_E)).

Virtual stuff gets into a whole other layer of crazy. A class with virtual members (including inherited ones) becomes something like

typedef struct V V;
struct V__vtable;
struct C {
    const struct V__vtable *vtable__;
    …
};
struct V__vtable {
    (virtual function pointers here)
};

and then you have to figure out a way to do dynamic casting (exercise for the reader:).

Templates usually end up as macros, and there’s really no good way to handle that uniformly; you’ll have to manually come up with names for the template instantiations and make sure things get pasted together properly. They might not be as bad if you use a separate header for each template and take macro args (#undef at end of file), then #include the header whenever you need to instantiate the template.

So you can do it, but it’s almost not worth it if you have any way to just shim into the C++ code from C. (E.g., come up with some extern "C" functions that let you access the C++ side of things.)

More on reddit.com
🌐 r/C_Programming
46
26
June 4, 2019
Any free programs to download youtube videos these days?
yt-dlp just open terminal, type yt-dlp otherwise, if you prefer GUI, there are plenty of those too, https://github.com/kannagi0303/yt-dlp-gui , https://github.com/dsymbol/yt-dlp-gui More on reddit.com
🌐 r/software
131
237
June 4, 2024
What is the best PDF to EPUB converter?
Try using Calibre - https://calibre-ebook.com/download_osx More on reddit.com
🌐 r/software
43
19
April 23, 2024
People also ask

Is the C to C++ converter free?
Yes. You can convert C to C++ for free without creating an account for up to 5 conversions per day. For higher limits and additional features, you can sign up for a Pro account.
🌐
codeconvert.ai
codeconvert.ai › c-to-c++-converter
Free C to C++ Converter — AI Code Translation | CodeConvert AI
Is the C++ to C converter free?
Yes. You can convert C++ to C for free without creating an account for up to 2 conversions per day. For more conversions and higher limits, sign in for free — every account gets 5 credits per day with support for up to 25,000 characters per conversion.
🌐
codeconvert.ai
codeconvert.ai › c++-to-c-converter
Free C++ to C Converter — AI Code Translation | CodeConvert AI
Can I also convert C++ back to C?
Yes! CodeConvert AI supports bidirectional conversion. You can convert C++ to C just as easily by using our C++ to C converter.
🌐
codeconvert.ai
codeconvert.ai › c-to-c++-converter
Free C to C++ Converter — AI Code Translation | CodeConvert AI
🌐
CodingFleet
codingfleet.com › code-converter › c++ › c
C++ to C Converter - CodingFleet
Convert your C++ Code to C. This exceptional AI-powered tool converts your C++ code into C code easily, eliminating the need for manual re-coding. Save your precious time and unlock cross-platform development like never before with our converter tool.
🌐
Reddit
reddit.com › r/c_programming › how to convert c++ code to c
r/C_Programming on Reddit: How to convert C++ Code to C
June 4, 2019 -

I have some C++ code. In the code there are many classes defined, their member functions, constructors, destructors for those classes, few template classes and lots of C++ stuff. Now I need to convert the source to plain C code.

Will I have to do total rewrite of the code ?

Top answer
1 of 5
61

Pretty much, yes.

The classes become structs with a (separate) set of functions; usually you prefix the names so it’s clear what they act on. You’ll need to to manual de-/initialization too. E.g.,

class C {
private:
    int x;
public:
    C() : x(0) {}
    C(const C &c) : x(c.x) {}
    C(int xv) : x(xv) {}
    int getX() const {return x;}
};

becomes something like

typedef struct C C;
struct C {
    int x;
    /* If you need to, you can also partition this like
    struct {
        int x;
    } private__;
    but it’s not like the C compiler will actually enforce privateness. */
};
void C_init_1(C *const inst) {inst->x = 0;}
void C_copy(C *const inst, const C *const c) {inst->x = c->x;}
void C_init_2(C *const inst, int cv) {inst->x = cv;}
int C_getX(const C *const inst) {return inst->x;}

If C extends something, it has to start with a copy of that thing; e.g.,

class D : public E, public F {…};

becomes

typedef struct D D;
struct D {
    E super_E;
    F super_F;
};

so static upcasting from D to E becomes &inst->super_E and the reverse is something like (struct D *)((char *)inst - offsetof(D, super_E)).

Virtual stuff gets into a whole other layer of crazy. A class with virtual members (including inherited ones) becomes something like

typedef struct V V;
struct V__vtable;
struct C {
    const struct V__vtable *vtable__;
    …
};
struct V__vtable {
    (virtual function pointers here)
};

and then you have to figure out a way to do dynamic casting (exercise for the reader:).

Templates usually end up as macros, and there’s really no good way to handle that uniformly; you’ll have to manually come up with names for the template instantiations and make sure things get pasted together properly. They might not be as bad if you use a separate header for each template and take macro args (#undef at end of file), then #include the header whenever you need to instantiate the template.

So you can do it, but it’s almost not worth it if you have any way to just shim into the C++ code from C. (E.g., come up with some extern "C" functions that let you access the C++ side of things.)

2 of 5
10

Depending on the reason for this change you might prefer to do this:

By writing extern "C" in front of a function you can later link it with C code. This will not work for classes or templates, but you might be able to write a wrapper to the stuff you want to use in C from the C++-Code.

I know that for old versions of C++ there used to be some sort of transpilers to C, but not for modern C++ and this would generate very ugly C.

Sorry that this is not a very good answer to your question, but i hope this still helps.

🌐
CodingFleet
codingfleet.com › code-converter › c
Convert Your Code to C - CodingFleet
C Code Converter - this online AI-powered tool can convert any code to C. Enjoy seamless conversions and unlock cross-platform development like never before.
Find elsewhere
🌐
Softpedia
softpedia.com › windows › programming › other programming files › c to c++ converter
C to C++ converter - Download - Softpedia
C to C++ converter is a tool that can convert projects developed in C to C++ compatible syntax.
🌐
Javainuse
javainuse.com › c2cpp
Online C Code to C++ Converter Tool
Online tool to convert C source code into C++. The main difference between C and C++ is that C is function-driven procedural language with no support for objects and classes, whereas C++ is a combination of procedural and object-oriented programming languages.
🌐
Quora
quora.com › Are-there-any-tools-to-automatically-convert-C-code-to-C++-code
Are there any tools to automatically convert C code to C++ code? - Quora
Yes. Converting C to C++ is usually straightforward for many programs, but there is no single perfect automatic tool that guarantees idiomatic, modern, or safe C++ output for every codebase. Available tools fall into three categories: automated translators (mechanical), compatibility helpers, and assisted migration toolchains that combine static analysis and refactoring.
🌐
CodeConverter
codeconverter.io › convert › c-to-c-plus-plus
Converting C to C++: A Comprehensive Guide | Convert your code to any language or framework effortlessly.
Learn the essential steps to convert your C code to C++ efficiently. This guide covers the key differences and how to adapt your C projects to C++.
🌐
PTC
ptc.com › en › products › developer-tools › lex-and-yacc
PTC Lex & YACC Convert Any Language into C or C++ Code | PTC
December 3, 2023 - A powerful program generation tool which processes any language specification you provide into usable, portable, and expandable C or C++ code.
🌐
Sololearn
sololearn.com › en › Discuss › 2985266 › convert-code-to-c-language
Convert code to C language | Sololearn: Learn to code for FREE!
C structs are plain data carrier, I think you can turn that ShowInfo() into a regular function, allow it to accept a parameter of struct `Course` type, which will be used as source of data to print.
🌐
TutorialsPoint
tutorialspoint.com › online_c_formatter.htm
Online C/C++ Formatter | Tutorialspoint
Online C/C++ Formatter and Beautifier - Try online C/C++ formatter and beautifier and Editor to beautify and format C/C++ code
🌐
Code Beautify
codebeautify.org › alleditor › cbcae3e9
converter c++ code to c code
This tool helps you to write code with color full syntax and share with others.
🌐
Quora
quora.com › Can-we-convert-c++-program-to-c-language
Can we convert c++ program to c language? - Quora
Answer (1 of 17): You are in luck, because the same compiler is used for both languages. It will be simpler if you do not include any C++ libraries in your code, including the STL headers. Only use the headers. That is probably not what you are trying to do, however. What is usually done ...
🌐
Tangiblesoftwaresolutions
tangiblesoftwaresolutions.com
Source Code Converters
Source code converters: Convert between C#, C++, Java, and VB with the most accurate and reliable source code converters
🌐
Quora
quora.com › What-is-the-easiest-way-to-convert-code-from-C-to-C
What is the easiest way to convert code from C to C++? - Quora
Answer (1 of 9): While C++ isn’t a proper superset of C, the differences are so minor that they’re easy to deal with. So, your first step is to simply compile your C code with a C++ compiler. In that sense, the conversion is trivial. You’ll need unit tests.
🌐
apponic
c-to-c-converter.apponic.com
C to C++ Converter Free Download
C to C++ Converter Free Download - Convert automatically C source or project to C++ with generated classes
🌐
CodeConverter
codeconverter.io › convert › c-plus-plus-to-c
Converting C++ to C: A Comprehensive Guide | Convert your code to any language or framework effortlessly.
This guide aims to provide a solid foundation for making this transition smoothly. Understand the fundamental differences between C++ and C, including object-oriented programming in C++ vs. procedural programming in C. Remove all instances of classes and convert them into structured data types in C.