Search code examples
c#c++headerhfile

Generate a dynamic c++ header file using c#


my goal is to generate c++ header file (or anything else that fulfils my requirement) for a c++ application via c#.

in my c# application I have a list of custom items. each item contains:

  1. Name property
  2. Type property (i.e. int, string)
  3. Value property

In the end the c++ application needs to create/implement the variables according to 1. and 2. then, the user assigns values to those variables and the application needs to check whether the values are equal to 3. or not.

any ideas on what would be the best method and how to do it? Thanks!

I've managed to create an h file which implements 1 and 2 as described above.

The resulted file is:

#ifndef GENERATED_HEADER_H
#define GENERATED_HEADER_H

class MyClass {
public:
int test;
};

#endif

Solution

  • 1. Let's understand what a header file is

    A header file in C++ is a file where you declare your variables, classes, functions, etc. "Declaring" means that you lay down your intentions regarding it. You do not necessarily define your tools, for example, declaring a function header in a header file is perfectly fine.

    You can declare your variables like this:

    int var_a = 100;
    int var_b;
    int var_c = 27;
    

    So, for now, please do create a header.h file where you do this:

    int a = 3;
    int b = 4;
    int c = 3;
    

    2. Let's implement a test functionality

    Let's do this:

    void test() {
        if (a == 3) cout << "a ";
        if (b == 3) cout << "b ";
        if (c == 3) cout << "c ";
    }
    

    3. In C# generate the string

    //converts arrays of (type, name, value) tuples into a header script
    String getHeaderContent(String params[][]) {
        String output = "";
        for (int i = 0; i < params.Length; i++) {
            String type = params[i][0];
            String name = params[i][1];
            String value = params[i][2];
            output += type + " " + name + " = " + value + ";\n";
        }
    }
    

    4. You should learn saving this into a file

    Saving into a file in C#: https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-write-text-to-a-file

    Now, let's suppose you are getting some settings like you described. All you need to do is to construct an array of arrays, each outer array containing three elements, one for type, one for name and one for value and then call the method at point 3. and write the result out to header.h.

    5. Adjust the tester

    Like at point 3, you can also generate your cpp file, by looping through the parameters you received and generating its corresponding line like we have seen earlier, but this time we are not generating a header line, but a cout. It should be easy to do it at this point. And of course, the tester will have an include command and function start and end too.

    6. Types

    Since the type is to defined too, surely you will have to handle the intricacies and differences of types, like having a char and an int, but that's more about the details of your work and it could be a topic of a follow-up question.