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)

2
  • What kinds of constants? Most importantly, what data types are involved? A short sample snippet would be handy as well. Commented Jul 30, 2009 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) Commented Jul 30, 2009 at 23:12

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This would require the use of managed C++ (which isn't really C++ at all), surely?
@bdonlan I am sure you can use unmanaged c++ as well. Are you sure?
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.
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.

1 Comment

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

1 Comment

This is a great way, but would lead to really messy code in my particular case.
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"
};

public 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)

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.