3

I am coming towards the end of a project that I created in C#. Unfortunately, the target hardware only comes with compilers for C/C++. My dad is an embedded programmer so he will be making the necessary code to integrate with the hardware, but in the meantime I need to find a way to translate the language. Free translators are a very high preference as I am extremely tight on funds at the moment.

While I am not fluent in C++, with a dirty translation I should be able to figure out most of what is required to make it run.

Edit:

The target platform is mbed Microcontroller

Scott S
  • 187
  • 2
  • 11

8 Answers8

14

Don't. This will not work.

C# has a garbage collector. C and C++ don't. You will have to rethink how you allocate objects and release them in C++.

Chances are, since you already have completed the project, rewriting it in C/C++ will be quite easy. You already understand the problem and have expressed it once before.

Daren Thomas
  • 67,947
  • 40
  • 154
  • 200
  • As I mentioned, I am not fluent in C++, so personal conversion is out of the question for me. – Scott S Nov 10 '10 at 09:40
  • @Scott - Your biggest problem will be that the 'converted' C++ will have no way of releasing objects since its not required in C# – Richard Szalay Nov 10 '10 at 09:47
  • @Scott: And even if it did, it would result in a really **poor** code. – ereOn Nov 10 '10 at 09:53
  • 5
    @Scott: manual conversion is the only chance you have of getting something that will run for more than 8 seconds without crashing. if you want to write applications in C++, you need to learn C++. Or hire a C++ programmer. There are. no. other. options. – jalf Nov 10 '10 at 15:00
6

There is no 1 to 1 mapping from c# to c++. The programming model and platforms are very different at the lower levels. Take memory management for example.

Your best chance is either to rewrite your application or try to get .NET Compact Framework or .NET Micro Framework to run in the hardware.

Edit:
Note that at least the .NET Micro Framework has a porting kit if your hardware is not supported.

Albin Sunnanbo
  • 46,430
  • 8
  • 69
  • 108
  • +1 for alternate NetFx implementations – Richard Szalay Nov 10 '10 at 09:39
  • And there a ports to similar platforms, but you will have to do your own port. – Gerhard Nov 10 '10 at 11:32
  • 1
    Hardware should be supported: "mbed" is really an ARM7 (NXP NPC1768) with sufficient RAM (64KB) and Flash (512KB). – MSalters Nov 10 '10 at 12:32
  • 1
    @MSalters: .NET Micro requires 300Kb RAM even before application code is added (ref: http://download.microsoft.com/download/a/9/c/a9cb2192-8429-474a-aa56-534fffb5f0f1/.NET%20Micro%20Framework%20White%20Paper.doc) – Clifford Nov 10 '10 at 14:40
  • 1
    @Clifford: this page claims that "as little as 64K" is needed: http://www.microsoft.com/netmf/about/default.mspx. Maybe they've improved the RAM requirements since the 2007 whitepaper? I really don't know - I don't have experience with .NET Micro. – Michael Burr Nov 10 '10 at 17:48
  • @Micheal: Agreed the document is old, but is prominent on the MSDN .Net Micro page: http://msdn.microsoft.com/en-us/netframework/bb267253.aspx, so it seems Microsoft continue to fail to understand the embedded market or take it seriously. – Clifford Nov 10 '10 at 23:26
3

Since design is half the battle in application development, your C# prototype should serve you well, but you are unlikely to find a suitable automatic translation tool. If you have not made heavy use of the .NET class library, especially those parts that relate to the underlying OS API, C# should be easily manually translated to C++. The code body syntax and semantics are very similar; it is the enclosing structural elements such as class definitions that are more different.

The required effort depends on the size of the application code, but much of that is mechanistic. The biggest difference being that you need to be more careful with memory management in C++ since there is no automatic garbage collection.

Clifford
  • 88,407
  • 13
  • 85
  • 165
2

Learn C or C++. There are no alternatives.

Both languages are radically different from C# and .NET, and automatic conversion is not possible. (and if it were, it certainly wouldn't allow you to "figure out most of what is required to make it run". It would be completely unrecognizable code, that'd be impossible to read or extend.)

In order to write a working C or C++ program, a C or C++ programmer needs to write the code. The good news is that it doesn't have to be that difficult. You don't have to learn every corner of the C++ language. But you do need to learn the basics.

If you're looking for the quick and dirty way to get off the ground, learning C might be a better option, because the language is so much smaller and simpler. But C++ is doable too. Just don't think you can get away with reading a 15-minute online tutorial.

jalf
  • 243,077
  • 51
  • 345
  • 550
  • I should add that this is assuming you're not able to get some variant of .NET or Mono running on the device. If so, that would be the easiest approach, by far, but from other comments, it sounds like even the .NET Micro framework would be difficult to get running on your device. – jalf Nov 10 '10 at 16:03
1

There are no translators.

The .NET Micro Framework has been ported to a Phycore LPC3180 (NXP) platform that is not to dissimilar to your board so it can be done but you still need to port the .NET framwork to your platform. It is unlikely that you will be able to use Mono AOT unless you are going to port Meamo to to your mbed board. Any porting would require you to be able to program C code.

The best and fastest way forward would be for You to learn C++. The differences between C++ and C# are not to big once you get going with C++ and understand the differences. You also going to have to use the mbed library for your hardware control and communications instead of what is provided by C#.

The C# code was a good prototype to debug your program design but it is not going to help you on the target. Now that you understand the problem it should not be to hard.

Gerhard
  • 6,850
  • 8
  • 51
  • 81
  • 1
    The Phycore LPC3180 very different animal; ARM9, 208MHz, hardware floating-point, and several Mb of RAM/ROM. Microsoft's estimate of minimum system requirement (not including application code) is is 300 KB RAM and 512K of Flash (http://download.microsoft.com/download/a/9/c/a9cb2192-8429-474a-aa56-534fffb5f0f1/.NET%20Micro%20Framework%20White%20Paper.doc) – Clifford Nov 10 '10 at 14:35
0

I'm not sure how you got this far without realising that the target platform couldn't run .NET, but it might be worth seeing if Mono's Ahead-of-Time compiler is able to output to your target platform.

At least then you wouldn't be throwing out (months of?) code.

Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • It's mostly a test program for a contest. As my dad was going to write the code for the device drivers, I figured we would PInvoke the methods. Only when I had most of the code finished did I find out the device came with only c/c++ compiler. – Scott S Nov 10 '10 at 09:43
  • That makes sense. Looks like the Micro Framework might be worth checking out. If your dad can write the drivers for the Micro Fx (if its not already supported), you can probably keep the majority of your C# code. – Richard Szalay Nov 10 '10 at 09:44
  • Also, I only spent a few weeks making the code (most of that time was figuring out TCP and UDP connections). – Scott S Nov 10 '10 at 09:45
  • It does target ARM running Maemo OS. Now you just need to port Meamo to your board. – Gerhard Nov 10 '10 at 11:22
  • Mono AOT runs on Mono:ARM, which in turn runs on Linux - so not a chance I suggest, not in 64K RAM. – Clifford Nov 10 '10 at 14:38
0

Since you wrote the application in a garbarge-collected language, the fastest way to port this to an mbed platform should be to port the application to a garbage-collected language which runs on mbed.

I haven't tried it, but eLua is supposed to have a preliminary port which runs on the mbed platform, and Lua is fairly simple to learn. http://mbed.org/users/jsnyder/notebook/elua-preliminary-port/

If you can get your dad to bring up eLua on the mbed platform, I suspect you could do the conversion fairly easily compared to trying to convert your application to C++.

-2

Port the code manually, and as you do, you will learn C++. :D