Is it possible to set Environment variables in Makefile - to be used after

后端 未结 2 419
一整个雨季
一整个雨季 2021-01-06 10:43

I am trying to set an Environment variable in a Makefile, so it can be used in another program running in the sam shell as make, but after make ha

2条回答
  •  南笙
    南笙 (楼主)
    2021-01-06 11:12

    Your export TEST_ENV_ONE=OneString above is running in a dedicated shell. The subsequent commands run in other shell instances. Therefore, they don't inherit the environment variable TEST_ENV_ONE.


    You could use a top-level (i.e., not in a target's recipe) export directive in the makefile:

    export env_var := MyEnvVariable
    
    .PHONY: all
    all:
        echo env_var: $$env_var
    

    This way, the variable env_var is exported to the shells that will execute the recipes.

    If you run make with the makefile above:

    $ make
    echo env_var: $env_var
    env_var: MyEnvVariable
    

    As you can see from the output, the shell that run echo env_var: $env_var had the variable env_var in its environment.

提交回复
热议问题