I am currently working in an aleady developed C++ project. The existing files are the following:



In the file HydroModel.hpp there are some constants defined in the namespace config, for example

 namespace config 
     const unsigned int dim = 40; 
     const unsigned int NaN = 10101; ... 

This file is being included in main.cpp, ODESysSol.hpp and HydroModel.cpp. These constants are being used over and over in those files.

However, these constants are not always the same, as they change depending on the case study analyzed. The idea is to take out these variables into a txt file in order to avoid compiling each time that the case study changes.

I know it would be easy if the variables are being defined inside a function or the main(), but so far I didn't find a way to do it in the preprocessor.

Is it possible to load the txt file and define the variables inside the namespace or I have to rewrite everything that is related to those variables?

  • They're variables. Why can't you just set their values from a config file at run time?
    – Useless
    Sep 29, 2016 at 13:14
  • Show us please one example to make clear which syntax was used to define the constants (or variables?). Are they are just preprocessor symbols? Static "const" variables? Or what else?
    – Doc Brown
    Sep 29, 2016 at 13:18
  • @Useless, can you please provide me with a little more information about that? I think that could be what I am looking for.
    – Muthahu6
    Sep 29, 2016 at 13:25
  • @DocBrown, here is the example namespace config { const unsigned int dim = 40; const unsigned int NaN = 10101; ... }
    – Muthahu6
    Sep 29, 2016 at 13:25
  • @AndresG: thanks for the clarification. By the way, did you see the "edit" button below your question? Guess what it is for. Comments are not well suited for adding formatted code.
    – Doc Brown
    Sep 29, 2016 at 13:28

3 Answers 3


Change your header file HydroModel.hpp to

namespace config 
    extern unsigned int dim; 
    extern unsigned int NaN;

In HydroModel.cpp write

namespace config 
    unsigned int dim; 
    unsigned int NaN;

That way, you won't have to rewrite much of your existing code.

Now all what remains to you is to implement a function which initializes those variables from the text file. Afterwards make sure this function is called when the program starts, before any other code tries to access the variables.

  • And make sure that nobody modifies them. Sep 29, 2016 at 13:40
  • @NicolBolas: first, one needs to make it work, then make it nice ;-)
    – Doc Brown
    Sep 29, 2016 at 13:42
  • For more on extern see when-to-use-extern-in-c Sep 29, 2016 at 16:49
  • Thanks for your reply, I think this is the type of solution that I am looking for. I did the changes but I didn't implement the function to load the values from a file, I just assigned the values as soon as the main() begins (just for testing). However now the program became terribly slow.. any suggestions? Thanks again!
    – Muthahu6
    Sep 30, 2016 at 22:22
  • @AndresG: I suggest you measure which part exactly is slow and isolate the problem. If you can reduce it to a minimal working, reproducible code snippet, and you are still unsure why it is slow, you can ask a related question on Stackoverflow.
    – Doc Brown
    Sep 30, 2016 at 22:33

If you want to "avoid compiling", then it has to be data loaded at runtime. It can't be something from a global compile-time const variable. You can have an object with (perhaps static) functions that return these values as loaded from a file. I suggest functions rather than exposing variables directly to prevent the outside world from modifying their values.

But they can't just be global const variables.


If you want to avoid recompiling, then some config file that would be loaded at runtime needs to be used.

You can also use ifdef and then use preprocessor directives in project files to choose the value of a constant, but that requires recompiling.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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