Search code examples
linuxbashqtmakefileqmake

How to setup environmental variable from inside qmake .pro file on linux?


I need to setup environmental variable with path from inside the .pro file which of course builds tons of stuff. The path is known. The behavior I expect is:

# my_project.pro
DESTDIR=~/cool/path/to/a/file/

# exporting DESTDIR to environmental variable called ENV_VAR
# terminal
$ qmake my_project.pro
$ make 
# messages from make building stuff
$ echo $ENV_VAR
~/cool/path/to/a/file/

Doesn't matter which one of them (qmake or make) will export the variable. I tried all sort of things, to no success, e.g.

# inside 
# attempt 1
QMAKE_POST_LINK += ENV_VAR=$$DESTDIR \
export ENV_VAR
# attempt 2
system(ENV_VAR=$$DESTDIR && export ENV_VAR)
# attempt 91345
system(ENV_VAR=$$DESTDIR)
system(export ENV_VAR)

Every attempt ended in:

# terminal
$ echo $ENV_VAR

$

If there is such possibility, I, apparently, can't manage to random-guess the correct syntax. How should I do this?

I'm using latest Qt5 and Ubuntu 22.04 jammy


Solution

  • You cannot set the environment variables of a parent process.

    When you call qmake, bash does a fork. That is basically copying the complete environment. In the forked process, qmake is executed, so in the copied environment, not in the original environment.