WSL无法看到环境变量

时间:2018-09-25 18:46:08

标签: c++ environment-variables clion windows-subsystem-for-linux

我目前正在寻求让CLion访问WSL中运行的MIPS交叉编译器工具链的方法。

在那儿,WSL有一个奇怪的问题。我已将以下内容添加到.bashrc

STAGING_DIR="/home/max/source/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16"
export STAGING_DIR

此外,我有脚本test.sh

#!/bin/sh

echo "Staging dir is: " $STAGING_DIR

使用wsl获取shell并执行脚本时,输出如下:

max@DESKTOP-ISH7UJQ:/mnt/c/Users/Maxi$ ./test.sh
Staging dir is:  /home/max/source/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16

在WSL中使用wsl <command>执行bash命令时,输出会有所不同。环境变量只是消失了。

C:\Users\Maxi>wsl ./test.sh
Staging dir is:

通过wsl执行.bashrc(我也尝试过.profile)时似乎没有执行?

这是一个问题,因为当CLion调用WSL子系统使用CMake构建我的C ++项目时,编译器会抱怨此环境变量不存在。

当我手动获得一个shell并执行 exact same cmake命令时,由于环境变量存在,因此编译工作不会发出警告。

内部CLion:

/usr/bin/cmake --build /mnt/c/Users/Maxi/CLionProjects/linux_wsl_test/cmake-build-debug-wsl_omega --target all -- -j 2
Scanning dependencies of target linux_wsl_test
[ 50%] Building CXX object CMakeFiles/linux_wsl_test.dir/main.cpp.o
mipsel-openwrt-linux-g++: warning: environment variable 'STAGING_DIR' not defined
[100%] Linking CXX executable linux_wsl_test
mipsel-openwrt-linux-g++: warning: environment variable 'STAGING_DIR' not defined
mipsel-openwrt-linux-g++: warning: environment variable 'STAGING_DIR' not defined
[100%] Built target linux_wsl_test

在WSL外壳上:

max@DESKTOP-ISH7UJQ:~/build_dir$ /usr/bin/cmake --build /mnt/c/Users/Maxi/CLionProjects/linux_wsl_test/cmake-build-debug-wsl_omega --target all -- -j 2
[ 50%] Building CXX object CMakeFiles/linux_wsl_test.dir/main.cpp.o
[100%] Linking CXX executable linux_wsl_test
[100%] Built target linux_wsl_test

这是怎么回事?

1 个答案:

答案 0 :(得分:1)

〜/ .bashrc 文件仅由交互式外壳程序加载。当您以这种方式运行脚本时,它是由非交互式shell处理的。您可以使用-i开关强制它是交互式的(尽管我不知道wsl命令是否适用)。或者,在运行脚本之前将BASH_ENV=/home/max/.bashrc放入您的环境中。