7

Good day! I have a qt project and I want to customize it using .pro-file conditions. Notably, I want to use one .pro-file to get several outputs, something like that:

DEFINES += APP1=0 APP2=1
DEFINES += TYPE=APP1
if(TYPE == APP1) {
LIBS += <LIB1>
DESTDIR = <DIR1>
}
else {
LIBS += <LIB2>
DESTDIR = <DIR2>
}

But when I try to build my project I get the following error when running qmake:

Parse Error('else')

How to do it correctly?

Mikhail Zimka
  • 694
  • 1
  • 9
  • 20
  • 1
    Try putting `} else {` in one line. I can build a test project with your example but older versions of qmake had that issue I think – Tim Meyer Jan 22 '13 at 11:55

2 Answers2

13

The values stored in the CONFIG variable are treated specially by qmake. Each of the possible values can be used as the condition for a scope. So, your project file can be wrote simply as:

CONFIG += APP1

APP1 {
  LIBS += <LIB1>
  DESTDIR = <DIR1>
} else {
  LIBS += <LIB2>
  DESTDIR = <DIR2>
}
liuyi.luo
  • 1,219
  • 7
  • 11
  • Thanks! That realy works! But now I have one more question: is there any way to pass a CONFIG value from ,pro-file to C++ code? I'd like to call something like: if(CONFIG.contains(APP1) {do something}) – Mikhail Zimka Jan 22 '13 at 13:23
  • Take a look at this: [Add a define to QMake with a value](http://stackoverflow.com/questions/3348711/add-a-define-to-qmake-with-a-value). I think that way you can use the `#if` / `#ifdef` syntax – Tim Meyer Jan 22 '13 at 14:42
5

I just want to note 1 thing about the conditions Make sure the curly bracket is not the same line. Otherwise it will fail

Good

CONFIG += opencv_32_bit

opencv_32_bit {

} else {

}

Will fail

CONFIG += opencv_32_bit

opencv_32_bit 
{

}
else
{

}

I'm not sure why, but I had this problem since I prefer braces on the next line

Daniel Georgiev
  • 1,292
  • 16
  • 14