views:

213

answers:

5

I have a long list of constants that I need access to in several projects which are in different languages(Verilog, C, C++ and C#). Rather than repeating them in each language, is there a good way to share these?

The only thing I could think of would be a text file and a preprocessing script? Is this the best solution or is there something easier/more elegant?

A: 

You might have an XML file with the constants to be shared and have it parsed in each language.

codaddict
That will give you runtime values, not compile-time constants.
Henk Holterman
XML is like violence: if it doesn't work, use more.
dash-tom-bang
+9  A: 

A preprocessing script which automatically updates those constants within your code is probably the best method. Commit the code with your project to ensure correctness and have it a part of the build script.

wheaties
This is what we used to do at my previous job (mixing C, VHDL, assembler and Matlab). Note that the special cases mount up quickly though, if you want to do anything more complicated than simple integer constants (e.g. being able to specify things in hex, constants defined in terms of others).
Oli Charlesworth
The M4 macro processor might well be a good choice.
caf
+6  A: 

You can keep them in a XML document and write XSLT scripts for each language to generate the appropriate source files in each build.

Henk Holterman
I think that's a superb solution - it would allow special case handling too. And it's extensible.
JBRWilkinson
+1  A: 

Can you use your makefile (or equivalent) to define these constants? For C and C++. you can use the compiler's CLI options to define pre-processor values for the constants. I haven't done much build customization for Verilog, but I suspect something similar might exist there as well.

bta
+1  A: 

You can write a simple file in the form of

const1 = value1
const2 = value2
const3 = value3

and then apply something like, for c:

s/\([a-zA-Z][a-zA-Z1-9_]*\)[ \t]*=[ \t]*\(.*\)/#define \1 \2/

Its worth noting that you might need to specify types because not all languages will allow you to use a preprossessor macro that doesn't care about type.

Alternatively, you can make a lexer/parser in Flex/Bison to parse the configuration file. This will be clearer and easier to extend.

mathepic