Reputation: 735
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)
Upvotes: 4
Views: 5672
Reputation: 76590
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)
Upvotes: 1
Reputation: 3371
An automated method of doing this it to use SWIG to convert your C++ code to C#.
Upvotes: 0
Reputation: 806
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.
Upvotes: 1
Reputation: 101665
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.
Upvotes: 1
Reputation: 15609
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.
Upvotes: 4