How to check for the presence of an environment variable when building a project from qmake An environment variable may or may not be set. in qmake, it is necessary to analyze the availability and consequently build the project in different ways. The project is built under Windows and under Linux. I tried it even if the environment variable VAR_ENV is not declared anyway, says that it is not empty (it displays an empty string):

VAR=$$(VAR_ENV) !isEmpty (VAR) { message($$VAR) } 

    1 answer 1

    Not very nice, but it works. In the pro or pri file.

     linux-g++-64 { !system ("./test.sh") { VAR = 0 # действие если объявления нет } else { VAR = $$(VAR_ENV) # действие если объявление есть } } win32 { !system(test.bat) { VAR = 0 # действие если объявления нет } else { VAR = $$(VAR_ENV) # действие если объявление есть } } 

    And two scripts for Windows and Linux. File test.bat.

     @echo off set VAR_ENV%1 | findstr "." if ERRORLEVEL 1 (exit 9) else (exit 0) 

    File test.sh.

     if [ -z ${VAR_ENV+x} ]; then exit 9; else exit 0; fi