0

In some other thread here have read, there are some tools able to convert C# to C++. Unfortunatelly it seems they all are comercial and I am not willing to pay money. Is there also some free alternatives for converting C# to C++? To be honest I have no idea about C#, never used it and not going to learn it, but have found some interesting open sorce tools writen in C#. So if there is such free tool for converting C# to C++, can it be used to convert the sorces of that tools?

7
  • 1
    Even if a converter could translate from C# to C++, it would be managed C++ which uses the DotNet libraries, is this what you want? Jan 6, 2022 at 10:40
  • Not really. C++ code which can´t be compiled with MINGW ist of no use to me.
    – convert
    Jan 6, 2022 at 11:34
  • 1
    I know of products.codeporting.com/native however this tool is not free. I am not sure you will find any free solution
    – Tomislav
    Jan 15, 2022 at 9:12
  • @Tomislav. Like I said there is an other thread in which some comercial solutions were mentioned. Just out of curiosity, is that trial version just limited by time or functionality? Does that tool produce real C++ code, or just some managed trash?
    – convert
    Jan 15, 2022 at 12:49
  • There are open source implementations of C#, e.g. mono-project.com Jan 15, 2022 at 18:29

3 Answers 3

3

I don't think that this is even possible, C# is relying on the DotNet libraries and on the garbage collector, so a converter had to convert the whole DotNet environment and reimplement features of the language.

(I'm assuming that you don't mean to use managed C++, which requires the DotNet environment as well.)

7
  • Read here: softwarerecs.stackexchange.com/questions/26859/… about such tool and was wonderin if there is a similar freeware tool. Somebody there tolld me to ask it as separate question, which like I see now was posibly not such good idea. Thanks for clarification.
    – convert
    Jan 6, 2022 at 15:20
  • I'm impressed that somebody even tried to write such a converter, but apart from the most basic examples I would never trust this code, the concepts (especially for freeing memory) are too far away. I wonder how restricted one is in using the DotNet framework libraries, it seems impossible that they found an equivalent for a significant part of all available classes. Jan 6, 2022 at 15:57
  • As I commented on another similar answer: while C# has a garbage collector, it does not need one. The resources could be freed right away. Whether or not an object is garbage collected in 3 minutes or right after the scope does not really matter. Jan 6, 2022 at 20:17
  • @ThomasWeller - The problem is see is a different one, because there is a garbage collector, developers write different code. Returning a new created list inside a function is common, and nobody cares whether the list owns its items or if it holds references only. Switching from an unmanaged language to a language with garbage collector is easy, the opposite is very tricky, a lot of developers are not used to it anymore. Jan 6, 2022 at 20:30
  • You can return a vector, can't you? Modern C++ much more similar to C# than I thought. I always made my way around C++, but since I coded ~20 challenges on CodeWars, I must admit that C++ may finally be a language which I consider coding in. Jan 6, 2022 at 20:35
1

You may check C# to C++ translator. It has .Net BCL implementation for C++. The converter is available here or here but it has limitations. You may write to developers, they are open for requests.

The converter is used on production to convert solutions up to 1M lines of code

0

If you want to compile and use open source C# software, you could use mono.

It is some open source implementation of C# (and its CLR runtime) - documented to run on Linux, Windows, MacOSX.

Converting C# to C++ requires a lot of work. You need to be expert in both C# and C++ (so take a week of work reading books about both of them). Be aware of the C++ rule of five (it has no equivalent in C#). I tend to think that fully automatic conversion (to readable and human-editable C++ code) is in practice impossible (e.g. because of Rice's theorem).

With GCC, you probably could call C++ libraries from C# code (assuming an open source mindset).

BTW, backtracing is possible in C++ on Linux with Ian Lance Taylor's libbacktrace, that the RefPerSys project and the GCC compiler uses. You would compile your C++ code with g++ -Wall -Wextra -g -O -fPIC and use dlopen(3) on Linux. Read the C++ dlopen minihowto.

4
  • But since I would like to convert from C# to C++ and not from C++ to C# not have much to vory about rule of five, even not about rule of three.
    – convert
    Jan 15, 2022 at 22:56
  • You probably do need the generated C++ code to follow the rule of five. Jan 16, 2022 at 9:44
  • Why? I am never using that move stuf. Also that rules make sence only when memory is alocated dynamically.
    – convert
    Jan 16, 2022 at 10:49
  • Also if using only smart pointers instead of that old C style ones, can just ignore that rules.
    – convert
    Jan 19, 2022 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.