Convert simple C ++ code to C # automatically

I have a C ++ file containing persistent definitions, I want to use the same definitions in a C # project. Since both projects are part of a larger project, I want if there is a change in the C ++ file (add / remove), it should also be reflected in the corresponding C # file. I want to keep 2 files in sync. I was wondering if there is a script / tool for this.

The reverse solution will also work (C # โ†’ C ++).

Clarification:

Currently code:

//C++ struct Colors{ static const int Red = 100; //Custom Values are important static const int Green = 101; } //C# public enum Color{ Red = 100; Green =101; } 

Now I want to have one file, so that any changes in C ++ are reflected in C # (or vice versa), so that I can have one file for projects for these constants.

As you can see, I want to map a bunch of constants defined in a structure in C ++ to an enumeration in C #. I want to make minimal changes above the expected definitions, since in these projects there are other code-dependent (in both projects) (but can do this if there is no good way to do this in the current format)

+4
source share
5 answers

You probably won't find a script ... You must have your own script for this. Otherwise, MACROs are best suited ...
If you have a script, you can create a rule in your makefile that will automatically run this script whenever you create your project.

+1
source

Why don't you take the constants file and pack it separately, like an assembly such as App.Constants.dll and have links to C # and C ++ projects? This way you can make changes in one place. Visual Studio has project-based links.

+4
source

Assuming simple integer constants, etc., it should be possible to reuse the same source file using the preprocessor creatively. Something like that:

 #if CSHARP public class Constants { #else # define public #endif // Easy stuff public const int FOO = 1; public const int BAR = 2; // Enums can be done too, but you have to handle the comma public enum Color { COLOR_RED, COLOR_GREEN, COLOR_BLUE } #if !CSHARP ; #endif #if CSHARP } #else # undef public #endif 

You may need a typedef for some types so that their names match (e.g. typedef unsigned int uint ).

You will then compile the code as part of your C # project using /define:CSHARP , as well as #include it in some C ++ header without additional definitions.

+1
source

What you want to do is create a C ++ managed library that contains constants and enumerations in a format that can be reused in both unmanaged C ++ and C #.

Managed Version:

 //managed.cpp #define MAKECONST(name, value) public const int ##name = ##value; public enum class FruitType { #include "FruitType.h" }; pubilc ref class Constants { #include "const.h" }; 

Unmanaged Version:

 //unmanaged.cpp #define MAKECONST(name, value) const int ##name = ##value; enum FruitType { #include "FruitType.h" }; #include "const.h" 

Actual definitions of transfers:

 //FruitType.h Apple = 1, Banana, Lychee 

Consts file:

 //consts.h MAKECONST(NumFruitInABowl, 3) MAKECONST(NumBowls, 2) 
+1
source

The automatic way to do this is to use SWIG to convert your C ++ code to C #.

0
source

All Articles