4

I have a file in C++ containing constant definitions, I want to use the same definitions in a C# project. Since both projects are part of a bigger project, I want if there is a change (addition/deletion) in the C++ file it should get reflected in corresponding C# file too. I want to keep the 2 files in sync. I was wondering if there is a script/tool to do this.

A reverse solution (C#-->C++) would also work.

Clarification:

Currently the code is:

//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 a single file so that any changes in C++ are reflected in C# (or other way around) so that I can have a single file across the projects for these constants.

As you see, I want to map bunch of constants defined in a struct in C++ to a enum in C#. I want to make no/minimal changes in above expected definitions as there is other code dependent (in both projects) on the above structures (but might do it, if there's not a good way of accomplishing this in the current format)

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Amit Wadhwa
  • 735
  • 1
  • 8
  • 16
  • What kinds of constants? Most importantly, what data types are involved? A short sample snippet would be handy as well. – Pavel Minaev Jul 30 '09 at 21:58
  • Great that you asked! Currently the code is: //C++ struct Colors{ static const int Red = 100; //Custom Values are important static const int Green = 101; } From the above I want to get in C#: public enum Color{ Red = 100; Green =101; } As you see, I want to map bunch of constants defined in a struct in C++ to a enum in C#. I want to make no/minimal changes in above expected definitions as there is other code dependent (in both projects) on the above structures (but might do it, if there's not a good way of accomplishing this in the current format) – Amit Wadhwa Jul 30 '09 at 23:12

5 Answers5

4

Why don't you take the constants file and package it seperately as an assembly something like App.Constants.dll and have both C# and c++ projects reference them? In this way, you can make change in one place. Have project based references to make it easy in Visual Studio.

Srikar Doddi
  • 15,499
  • 15
  • 65
  • 106
1

Assuming plain integer constants and such, it should be possible to reuse the same source file by using preprocessor creatively. Something like this:

#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 might need typedefs for some types so that their names match (e.g. typedef unsigned int uint).

Then you compile code as part of your C# project with /define:CSHARP, and also #include it into some C++ header with no additional defines.

Pavel Minaev
  • 99,783
  • 25
  • 219
  • 289
1

You probably wont find a script... You should have your own script to do this. Otherwise MACROs are the best fit...
If you have a script then you can create a rule in your makefile that will automatically run this script whenever you build your project.

FatDaemon
  • 766
  • 2
  • 8
  • 13
  • ohh i actually ment PreProcessor directives and not MACROs. "MACRO" is probably more attached to c++ in this context – FatDaemon Jul 30 '09 at 22:58
1

What you want to do is to create a managed C++ library that contains the constants and enums in a format that is reusable 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"

The actual enum definitions:

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

The consts file:

//consts.h
MAKECONST(NumFruitInABowl, 3)
MAKECONST(NumBowls, 2)
Igor Zevaka
  • 74,528
  • 26
  • 112
  • 128
0

An automated method of doing this it to use SWIG to convert your C++ code to C#.

Soo Wei Tan
  • 3,262
  • 2
  • 34
  • 36