Chris Watts
Chris Watts

Reputation: 6725

Permanently storing an environment variable when using CMake install

I'm trying to store a path environment variable to the location of some configuration files my program needs at runtime, but I do not know the location of until the compiled program is installed.

My idea was to use the following:

install(CODE "set(ENV{MY_CONFIG_PATH} \"${CMAKE_INSTALL_PREFIX}/MyConfig\"")

However, I quickly found out that this does not permanently set that environment variable so as soon as I run the program and check the contents of MY_CONFIG_PATH with std::getenv(), I get a null pointer.

I thought about maybe setting a preprocessor define at compile time, but that won't work either because it seems CMAKE_INSTALL_PREFIX is only populated when the installation process is executing.

Can anyone suggest a neat workaround that works for both Windows and Unix?

Upvotes: 1

Views: 763

Answers (1)

tambre
tambre

Reputation: 4853

Since CMAKE_INSTALL_PREFIX is known at configure-time you can use the configure_file command to configure a file and insert the value of CMAKE_INSTALL_PREFIX into the specified location.

Upvotes: 1

Related Questions