1

I have an Qt/C++ application which uses several configuration. So in Build settings I've added my 2 custom configurations in addition to standard Release end Debug. enter image description here

Now in QtCreator I can select desired configuration to build the application using appropriate configuration.

But I don't know how to read this configuration in a C++ file. I want something like the following:

#if defined CONFIG1
...
#elif defined CONFIG2
...
#else
...
#endif

How to do that?

folibis
  • 12,048
  • 6
  • 54
  • 97
  • `#ifdef`? (5 more to go) – user202729 Feb 06 '18 at 12:12
  • The "configuration" you want to "read", it's not a C++ source or header file you build with the project? It's an actual text-file you want to read in your program at run-time? By "configuration" do you mean a macro you use in a source or header file? Can you please elaborate on what you mean by "read this configuration". – Some programmer dude Feb 06 '18 at 12:12
  • I just want to determine active configuration. in C++ files of course. – folibis Feb 06 '18 at 12:14
  • Then what is wrong with what you show us? Have you tried it? With your current attempt, what works and what doesn't work? Do you get build errors? Run-time errors? Please be as detailed and specific as possible. – Some programmer dude Feb 06 '18 at 12:15
  • CONFIG1, CONFIG2 is for example. I don't know where to set it. `#if Configuration_name` doesn't work. – folibis Feb 06 '18 at 12:18
  • are you porting from QtCreator to `qmake`? Maybe [this](https://stackoverflow.com/a/3350169/7926064) helps. – BNT Feb 06 '18 at 12:19
  • Each "configuration" should allow you to add macros specific for it. – Some programmer dude Feb 06 '18 at 12:27

1 Answers1

3

Ok, thanks to you guys I've found the solution. There is Additional arguments in the Build step tab for custom variables. I've added here:

"DEFINES += CONFIG1"

for specified configuration. Quotes are necessary. It will be converted to -DCONFIG1 command line argument for g++ and so in C++ code it could be used as following:

#ifdef CONFIG1
    // code here
#endif
folibis
  • 12,048
  • 6
  • 54
  • 97