Home > Software engineering >  How to reference CMake variable in shell script
How to reference CMake variable in shell script

Time:12-16

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.

CodePudding user response:

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).

  • Related