Tim Ring
Tim Ring

Reputation: 1833

C++ frontend only compiler (convert C++ to C)

I'm currently managing some C++ code that runs on multiple platforms from a single source tree (Win32, Linux, Verifone CC terminals, MBED and even the Nintendo GBA/DS). However I need to build an app targetted at an embedded platform for which there is no C++ compiler (C only). I remmber that many of the early C++ compilers were only front-ends stitting on existing C compilers (Glockenspiel for example used MSC). Are there any such 'frontend' C++ compilers in use today that will generate C code.

                      Tools            Platform
                      -----------      ------------

                ______Visual C++ _____ WIN32
               /
              /_______MBED (ARM)_______MBED (ARM dev board).
             /
            /_________GCC (x86)________Linux
           /
Source____/___________GCC (ARM)________GBA/DS
          \
           \__________SDA______________Verifone Verix CC Terminals
            \
             \________ARM SDT__________Verifine VerixV CC terminals
              \
               \______????_____________Renases M8/16/32.
                \
                 \____????_____________Z8 family.

The last two platforms I have good C compilers for but no C++.

As you can see I'm supporting a large variety of platforms and I share a large body of library code (and some app code).

Upvotes: 24

Views: 13217

Answers (7)

Fabian le Maux
Fabian le Maux

Reputation: 43

Notice that the valid solution is for llvm 1.3 llvm last release was 6.0.0 so it would'nt work at all. (I post this because i've try the solution of this post but it doesn't work anymore) (Maybe i've done something bad)

In my tests llc do not accept anymore the -march=c option. And going back to the 1.3 isn't possible for me yet. So try to take care about the version.

Upvotes: 1

Jay Conrod
Jay Conrod

Reputation: 29701

If you use LLVM, llvm-g++ will compile your C++ code to LLVM bitcode, and llc has a backend which converts bitcode to C.

You could write commands like this:

llvm-g++ -emit-llvm -c foo.cpp -o foo.o
llc -march=c <foo.o >foo.c

Upvotes: 41

henkebenke
henkebenke

Reputation: 78

Of what I have understood, the c++ support in gcc for r8c/m16c/r32c isn't quite mature and good enough for production code. But for r8c/m16c/m32c/r32c there is at least one compiler supporting embedded C++ and that is IAR, I think Tasking also supports embedded C++ on m16c.

Upvotes: 1

quietbob
quietbob

Reputation: 151

Can't help with the Z8, but the Renesas M16C/M32C family has GCC these days - see http://www.kpitgnutools.com/ for prebuilt cross toolchain hosted on Windows. Haven't used it myself yet but may be a better option than a 3rd party C++ frontend, especially as your code already targets GCC on other platforms.

Upvotes: 5

Jerry Coffin
Jerry Coffin

Reputation: 490138

Comeau C++ generates C as its output, and they seem to be quite happy to port it to work with different back-end compilers, though I'm not sure about the exact pricing for that.

If you want a bit more of a "roll your own" approach, you could buy a license to the EDG C++ compiler. It's normally used as a front-end (e.g. by Comeau and Intel) but I believe as it's shipped, it includes a code generator that produces C as its output. Its licensing is oriented more toward compiler vendors, though, so a license gives you a lot of rights, but is pretty expensive.

Upvotes: 2

plinth
plinth

Reputation: 49189

Out of date, but maybe you want to try cfront?

I'll leave this for information - cfront doesn't have exception support.

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340208

Comeau C++ does this.

Upvotes: 18

Related Questions