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.

  • 9
    Ah, I see we have a masochist here with us today.
    – Neil
    Sep 28, 2012 at 16:05
  • Actually even more masochistic: I need to ensure same ids/classes in CSS too.
    – paperjam
    Sep 28, 2012 at 16:29

4 Answers 4


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.

  • Writing a small templating step in your toolchain is a good answer. Common definitions go in the file that feeds the template, language/feature specific code goes in template files. sed, perl, python... any could be used to implement this pretty quickly.
    – seggy
    Sep 28, 2012 at 16:39
  • @seggy Yep, actually I wanted to suggest sed as an alternative to using a scripting language to minimize the number of "extra" languages. Sep 28, 2012 at 16:48
  • In general, I agree. sed should be on almost any system (and it's a really handy trick to have in your kit), but if he's on windows without Cygwin, he may have other options available.
    – seggy
    Sep 28, 2012 at 17:00

What you're describing is what is commonly done with languages like JSP and PHP, injecting names defined on the server. If you cannot use these languages, then I don't think it is worth the difficulty you're attempting simply to avoid having to change class names in more than one spot. Granted, that is generally a best practice, but if it makes your code unnecessarily unreadable, you should avoid it like the plague.

C# is similar enough to C++ that the transition should be smooth, and C# has Asp support which allows you to define constants and drop them directly in the page.

However assuming that you don't want to switch language, my advice is to do the next best thing: Have a C++ header and a css file which declare the same names. If you need to change the names, you need only change in these two places, and you've lost little extra time in doing so. Don't lose sight that simplicity is your objective, not avoiding duplication of code, and in this case, I can't possibly think that what you're attempting is the simplest solution.


Generate the javascript based on the definitions in the C++ file. If you already have a template system in place for the HTML, you can probably reuse that to interpolate C++ values into the javascript.

If server load or performance is an issue, consider caching the javascript (both on the server and on the client), or precompiling it altogether (it's a small step from a C++ program that serves dynamic javascript on-the-fly to one that saves it in a file instead).


You can use macros in C/C++ to change identifiers as needed. For javascript the obvious candidate is var since it is only needed in javascript ... it can be redefined as necessary to any c type.


//constant integers
#define var const int
#include "int-consts.js"
#undef var

//constant strings
#define var const char*
#include "string-consts.js"
#undef var

//arrays of strings
#define var const char**
#define new (const char*[])
#define Array(x,...)  {x,__VA_ARGS__}
#include "string-arrays.js"
#undef new
#undef var

//and so forth for other types


var swapMagicOffset = 4086;


var swapMagic = "SWAP-SPACE";
var swapMagic2 = "SWAPSPACE2";


var cars = new Array("Ford", "Chevy", "Dodge");

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.