0

I use execute_process to run a shell script,

set(CMAKE_C_FLAGS ...) # here ... means many options
execute_process(
    COMMAND ./my_script.sh)

In my_script.sh I want to use the value of CMAKE_C_FLAGS and how to do it? When echo ${CMAKE_C_FLAGS} in script, I get nothing.

1
  • Inside your Make file, you write the value of the variable into a file, and afterwards in your shell script, you read this file. However, in this case I would consider it the other way around: Set the variable in your script before invoking cmake, and pass it to the Makefile. No need of a temporary file then. Commented Dec 14, 2021 at 9:40

1 Answer 1

2

CMake variables are not environment variables.

You can set an environment variable like so:

set(CMAKE_C_FLAGS ...)
set(ENV{CMAKE_C_FLAGS} "${CMAKE_C_FLAGS}")
execute_process(COMMAND "${CMAKE_CURRENT_SOURCE_DIR}/my_script.sh")

Note that this lasts only for the duration of the CMake configure step. This means it will work with execute_process, but not add_custom_command.

Also note that calling a shell script from CMake is a code smell (hinders portability, is weird and probably unnecessary).

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

2 Comments

So some processing is needed to be done with shell before building the target, what's the correct way to do that if not calling a shell script?
"needed to be done with shell" -- this is, let's say, rare.

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.