0

I have a set of an environment variable which is in bash, I want to add those all environment variable in my executable in CMakeLists.txt.

I have an exec-cluster.bash file having

 EXPORTS+=" export DICE_USE_ALT_LICENSE=${DICE_USE_ALT_LICENSE};"
 EXPORTS+=" export DICE_VENDOR_KEY=\"${DICE_VENDOR_KEY}\";"

Now I want to add these all variable in my executable in CMakeLists.txt.

6
  • You can change the environment of a process (your running executable) but AFAIK you cannot change the enviroment of a parent process (the bash in your case). The only exception might be to change one of the start scripts (e.g. ~/.bashrc) but even then the changes won't become effective before bash is started next time. Btw. how do you know that user of your executable runs this from bash? Although it's probably very common - it's not the only existing shell. tcsh and zsh might be in use as well and there are (much?) more... Commented Jan 10, 2019 at 6:33
  • what do u mean by "adding env vars to the executable"? Commented Jan 10, 2019 at 10:13
  • I am using get_env("DICE_USE_ALT_LICENSE") in my source code and that environment variable DICE_USE_ALT_LICENSE defined in exec-cluster.bash. so I want to add exec-cluster.bash in CMakeLists.txt while building my source so that I can use. Commented Jan 10, 2019 at 10:17
  • 2
    but get_env only applies when your program runs, not when is being built? How would CMake come into the picture here? Do you want to hard-code these variables into your program somehow? Commented Jan 10, 2019 at 10:43
  • Then how can I make the environment ready for my executable so that I can use while environment variables are defined in bash? Commented Jan 10, 2019 at 11:51

1 Answer 1

1

You can get the environment variable inside cmake with $ENV{} and then add them to the compile environment as C/C++ defines:

...
set(VARIABLE_VAL $ENV{VARIABLE_NAME})

add_compile_definitions(VARIABLE_NAME=${VARIABLE_VAL})
...

If I understood well what you want...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.