Timothy Riley
Timothy Riley

Reputation: 1

Environment variables set in CMakeLists.txt or exported not propagated to build tools

I am trying to make the Qt build folder portable so that it can be built on one machine and shared with others. I'm running into an issue on macOS actually using the Qt uic tool in CMake with an error like this:

dyld[74739]: Library not loaded: @rpath/QtCore.framework/Versions/5/QtCore
Referenced from: /path_to_moved_Qt_build_folder/qtbase/bin/uic
Reason: tried: '/path_to_moved_Qt_build_folder/qtbase/bin/../Frameworks/QtCore.framework/Versions/5/QtCore' (no such file), '/original_Qt_build_folder/qtbase/lib/QtCore.framework/Versions/5/QtCore' (no such file)
... (some other paths)

otool -l /path_to_moved_Qt_build_folder/qtbase/bin/uic shows me that the path to the framework in the original Qt build folder is hardcoded. I can get uic to run correctly if I set DYLD_FRAMEWORK_PATH=/path_to_moved_Qt_build_folder/qtbase/lib but I can't get this to work in the actual CMake build. I've tried both of the following:

export DYLD_FRAMEWORK_PATH=/path_to_moved_Qt_build_folder/qtbase/lib cmake --build BUILD --target app

as well as setting the environment in the CMakeLists.txt file:

set(ENV{DYLD_FRAMEWORK_PATH} /path_to_moved_Qt_build_folder/qtbase/lib)

I've also tried setting the Libraries path in qt.conf but this also does not seem to affect how uic runs.

But in both cases when running my build I am getting the original error again. How can I get this variable exported so uic will use it?

Upvotes: -1

Views: 1215

Answers (2)

starball
starball

Reputation: 52080

You tried to do this with set(ENV{...} ...), but that won't affect the build. See the docs, which state:

This command affects only the current CMake process, not the process from which CMake was called, nor the system environment at large, nor the environment of subsequent build or test processes.

If you want to set an environment variable for a build command, you can use the CMake env commandline command, like cmake -E env VAR_NAME=VALUE -- cmake --build .... Or if the build tool accepts other forms of setting the variable you want to set (such as Make), you can pass them to the underlying build tool at the end of the cmake --build command after a -- (docs). Ex. cmake --build ... -- VAR_NAME=VALUE.

If you are using CMake presets, configure, build, test, and package presets all have a environment property that you can use, and build, test, and package presets also have a inheritConfigureEnvironment property. See also the preset macro docs for $env{<variable-name>} and $penv{<variable-name>}.

If you are using the VS Code CMake Tools extension without CMake presets, see also these settings: cmake.environment, cmake.configureEnvironment, cmake.buildEnvironment, cmake.testEnvironment, and cmake.buildToolArgs.

Note: your attempt to export an environment variable looks wrong. I think you either meant to use a shell syntax like VAR_NAME=VALUE <command>, or put a semicolon or && between the export command and the command you want to run with the exported variable.

Upvotes: 1

Timothy Riley
Timothy Riley

Reputation: 1

Found a solution - for me, CMake is generating a Makefile, so I found that you can pass environment variables to it, for example:

make target FOO=BAR

If using CMake itself to build, then do this:

cmake --build BUILD --target target -- FOO=BAR

Upvotes: 0

Related Questions