2

I've got this task that requires me to generate some basic C code using a software written in C#.

The generated code should be based on some input files I provide to my software, we'll call it btOS for easy of communication.

So when starting btOS I give it as input file1, config.xml. When I hit run it should output a file.c that contains some basic structures and/or methods based on what the input files contain.

Is there any elegant way to do this ? Maybe some already generated templates or methods or stuff like that ? The only way I could think of handling this was creating specific strings in C# and outputting them to a C file.

L.E.: It seems that somehow my question was not clear enough. I assume the fault of including C++ in the title, I have remove it but I don't see how that is relevant because the question was very simple.

Anyway, to make it more clear. All i need to do is read some config files (their content is irrelevant, all they contain are some variables that will be used to generate some function templates, which will mostly impact the name of the function) - and write an output file with the extension .C (as in Main.c) that will contain those templates I generated.

So, again, the question: Are there any "elegant" and maybe somehow "professional" ways to do this other than using custom generated strings within the code that I will write to the file ? Right now the only way I see fit to do this without too much hassle is using some template text files with a naming convention defined by me(e.g. function_variableName{...}) where I just change the [variableName] text with whatever I need to to be there and "Abracadabra" I have a function that I will write to the file.

Now as Soonts suggested please try and be helpful, read multiple times if you don't clearly understand or maybe even don't bother - let somebody who is interested in this topic, tries to help or gain some new knowledge before flagging it.

Double Cheers.

George
  • 35
  • 6
  • Hello John Doe, maybe this will be helpful to you. https://github.com/mono/CppSharp – Danieboy Dec 18 '17 at 14:27
  • 7
    C or C++? They are distinct languages. – François Andrieux Dec 18 '17 at 14:27
  • You need to present more details, such as a sample content of the configuration files. Also, are you generating source code that needs to be compiled or are you generating executable code that your C# code calls? – Thomas Matthews Dec 18 '17 at 14:40
  • Yes, there is. If you’re using Windows, the elegant way is T4 text templates. See this answer for pros and cons: https://stackoverflow.com/a/14786206/126995 – Soonts Dec 18 '17 at 15:04
  • These will run as a part of your build process. You include an XML file in your project, write a T4 template to generate C code from that, and visual studio will automagically generate code. The IDE will also re-generate the code each time you update your XML and rebuild your project. – Soonts Dec 18 '17 at 15:10
  • See also this IDE plugin for better UX: http://t4-editor.tangible-engineering.com/T4-Editor-Visual-T4-Editing.html – Soonts Dec 18 '17 at 15:11
  • 2
    Also, to the people who closed the question. Code generation is quite common problem. I did it more than once for various reasons (network RPC, hardware interface protocols, etc.). However, the elegant way i.e. T4 is not that easy to discover. By closing this question, you make sure it stays hard to discover. How’s that helpful to anyone? – Soonts Dec 18 '17 at 15:20
  • I have edited the question with further more "detailed" information, although I don't see what was wrong in the first place since I never mentioned anything about compiling the generated code, or executing it or even calling it from C#. All I said is I want to write it to a file... like writing text, but this time it's code, functions, stuff like that :) – George Dec 19 '17 at 09:02
  • 1
    If you aren’t using visual studio (e.g. on Linux) and T4 is therefore unavailable, another option is Razor view engine, a component from ASP.NET MVC. See this project for an example: https://github.com/Const-me/IntelIntrinsics In that project I use it to convert an XML into a bunch of HTML files, but you can use the same approach to generate any other text files, including C source code: http://www.codemag.com/article/1103081 – Soonts Dec 19 '17 at 16:09

0 Answers0