C++ – Common header file for C++ and JavaScipt

cdefinitionheadersjavascript

I have an app that runs a C++ server backend and Javascript on the client. I would like to define certain strings once only, for both pieces of code. For example, I might have a CSS class "row-hover" – I want to define this class name in one place only in case I change it later.

Is there an easy way to include, or read, some sort of common definitions file into both C++ and JavaScript? Ideally as a compile / preprocessing step but any neat approach good.

Best Answer

I had proposed a quite similar approach here: https://stackoverflow.com/questions/4248831/shared-config-file-between-php-and-c/4248881#4248881 for a similar issue.

I would suggest to write a small script that generates code for C++ and JavaScript, with the configuration you want to have common. You can also use two templates in a template library, that you will run as part of your build process that will generate the appropriate files for each language.

Using a freemarker-like syntax (although I do not suggest including another language!), there could be something like this for JavaScript:

commonStuff = { config1: ${value1},
config2: ${value2}
}

and another template for the C++ implementation.

As suggested in the comments below, one good candidate for that, is to use sed, so that you would not have to introduce another language since your project has two already.

Related Topic