0

Hi I have a code in which it is using few INI files for pre-defined settings. Can I use #ifdef in INI file, if yes how can I use that? If NO, How can I restrict my code compilation of INI files. for example I have one macro- "FLAG_A" I have INI file as below

setting.ini

Volume = 10    
Brightness = 30    
A_Variable  = 0    

I want to use it like

#ifdef FLAG_A    
Volume = 5    
#endif    
Brightness = 30    
A_Variable = 0    
Prav27
  • 11
  • 2
  • 1
    Are you asking if you can use the C++ preprocessor in non C++ code? If so, https://stackoverflow.com/questions/18473036/run-gcc-preprocessor-non-c-files might be helpful. – Artyer Sep 24 '19 at 00:19
  • No, you can't. But you can load a different ini file from your code, using macros if you wish. – Michaël Roy Sep 24 '19 at 00:31
  • 1
    potentially you can pipe the ini file through a c++ pre-processor whild loading. But you have to figure out how to do it in your environment. – Serge Sep 24 '19 at 00:49
  • Serge, Can you make me understand with an example? – Prav27 Sep 24 '19 at 00:55

1 Answers1

0

Ini files are not compiled by C++ compilers usually. Rather, C++ compilers compile C++ source files. In fact, ini files are not generally compiled at all, since ini files are not a compiled language.

The C (which is essentially same as C++) pre-processor is not intended for other macro processing than for purposes of the language. Some compilers do allow you to invoke the pre-processor separately without compilation. An example:

gcc -E file.ini

But since the purpose is for the compilation, this will in addition to processing and replacing your directives with the corresponding output, add implementation defined directives for purpose of the compiler. If your ini processor supports # character as a comment, then these directives would conveniently be interpreted as comments. Otherwise, this is probably not usable for you.

Regardless, there are other, independent macro languages as well as template processors than the one used by the C pre-processor. I suggest you use one of them.

eerorika
  • 232,697
  • 12
  • 197
  • 326