24

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).

Tim Ring
  • 1,845
  • 1
  • 20
  • 27
  • 2
    A couple of good things to check out, in particular LLVM as a means of generating C from C++ plus I hadn't realised that GCC now supports the Renesas M16/M32 (that only leaves the Z8 without a C++ compiler and to be honest I just bought a cheap dev board to check it out, nostalgia for my Z80 origins, it is a nice microcontroller though). Also I would like to target older Hypercom terminals (I have written a good bit of code for these but I have to have a separate source tree. I know the latest Hypercoms are ARM based but there are still millions of those pesky ICE terminals out there. – Tim Ring Dec 03 '09 at 08:57
  • Related: http://stackoverflow.com/q/737257 – Fred Nurk May 06 '11 at 03:18

7 Answers7

41

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
Jay Conrod
  • 28,943
  • 19
  • 98
  • 110
  • Your second link is broken. – Baum mit Augen Dec 02 '15 at 00:03
  • 2
    The C backend was [removed in LLVM 3.1](http://releases.llvm.org/3.1/docs/ReleaseNotes.html#changes): "It had numerous problems, to the point of not being able to compile any nontrivial program." – Jeremy Sep 10 '18 at 13:47
18

Comeau C++ does this.

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Was going to say the same but you beat me to it by 11 seconds – philsquared Dec 02 '09 at 15:15
  • I think I've come across this compiler before. The only iussue is that the problem platform are for hobby use only. As far as I can recollect the Comeau compiler is a tad expensive. I'm using the ARM SDT for work (expensive but the job pays). I also use Visual Studio 2008 at work (again paid for by work). I used Visual Studio Express at home. – Tim Ring Dec 02 '09 at 15:17
  • 7
    Wow, it's "breathtaking," "amazing," "fabulous," and they even have a blinking javascript link that floats on top of the text urging you to purchase it! – Seth Johnson Dec 02 '09 at 15:18
  • 8
    @Seth: the web site is a bit overboard, but it *is* the best C++ compiler around, at least in terms of conformance. – Jerry Coffin Dec 02 '09 at 15:20
  • 4
    @JudgeDread: at least the normal Windows version of Comeau C++ is decidedly inexpensive -- $50US/copy. I'm not sure what they charge to port it to other targets though. – Jerry Coffin Dec 02 '09 at 15:21
  • 3
    @JudgeDread: you should ask Comeau about pricing - for the more popular platforms (or at least Windows & Linux) it's $50. If he already has a port for the target you're looking for, maybe it'll be the same. But for a custom port it'll likely not (this page, http://www.comeaucomputing.com/custom.html, indicates $5K - $50K). But it can't hurt to ask - worst case, you're no better off than right now. – Michael Burr Dec 02 '09 at 15:25
  • 1
    @Seth - the site is quite a sight. And if you decide to buy, the order page is pretty fun too. – Michael Burr Dec 02 '09 at 15:30
  • 1
    The site is dead now unfortunately. COHERENT is based on Comeau, so there might be an opportunity there, but I haven't explored it. https://www.autometer.de/unix4fun/coherent/#inst_coh – VoteCoffee Mar 09 '20 at 17:28
5

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.

quietbob
  • 151
  • 3
  • Thanks, will check it out (I'm currrently using the Glyn dev boards for R8 & R32, from Elektor, a European electronics mag). The dev kits are handy as they're on little DIL boards, makes it easy to use on breadboards and they can then be plugged into SIL/DIL sockets on homemade PCBs. The MBED I;m using has a similar form factor. – Tim Ring Dec 03 '09 at 08:50
2

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1

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

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

plinth
  • 48,267
  • 11
  • 78
  • 120
1

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.

henkebenke
  • 78
  • 6
1

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.