0

I have the need to access a bash associative array element via cmake. So assume that my declaration and access was something like the following in bash:

~ declare -x someText=124 ~ echo $someText 124 ~ declare -A mymap=( [foo]=bar [baz]=quux) ~ echo ${mymap[foo]} bar 

Access in CMakeLists.txt would be:

message(STATUS "$ENV{someText}) message(STATUS "$ENV{mymap}) 

when I run cmake I get the following:

-- 124 -- 

So, the associative array cannot be accessed via the $ENV{} cmake command. Any ideas how to access it? To be specific, I don't care about the complete associative array, just one element in it.

4
  • The bash -c opens a sub-shell, the array mymap defined in the first command won't be available in the second echo Commented Apr 12, 2018 at 6:23
  • ok, but even if I get rid of bash -c and I run every command by itself, I get the same message. Commented Apr 12, 2018 at 6:36
  • @Inian: bash -c opens a chid process, its not a sub-shell. A sub-shell is where existing variables (and other resources) are copied to the child whether they are exported or not. Since the parent (cmake) is not bash then it can't create a bash sub-shell. Commented Apr 12, 2018 at 6:36
  • @TassosTzavellas: you will, you cannot import a variable value from another process (a shell), let alone another language, without some sort of inter-process communication. Commented Apr 12, 2018 at 6:39

1 Answer 1

1

CMake cannot access array variables created in a bash shell.

Process cmake doesn't see an array variable - unlike to "normal" variable, array variable isn't part of the environment passed to the process.

Even another bash subprocess doesn't see array variable, see this question.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.