views:

630

answers:

5

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)

+4  A: 

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.

CodeToGlory
This would require the use of managed C++ (which isn't really C++ at all), surely?
bdonlan
@bdonlan I am sure you can use unmanaged c++ as well. Are you sure?
CodeToGlory
You should be able to pInvoke things from a normal C++ library, much like you would existing windows libraries that don't already have a managed wrapper.
jrista
+1  A: 

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
This is a great way, but would lead to really messy code in my particular case.
Amit Wadhwa
+1  A: 

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
ohh i actually ment PreProcessor directives and not MACROs. "MACRO" is probably more attached to c++ in this context
FatDaemon
+1  A: 

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
A: 

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

Soo Wei Tan