WSL看不到环境变量 [英] WSL can't see environment variable
问题描述
我目前正在寻求让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
获取外壳并执行脚本时,输出如下:
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 <command>
在WSL中执行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
这是怎么回事?
〜/.bashrc 文件仅由交互式shell加载.当您以这种方式运行脚本时,它是由非交互式Shell处理的.您可以使用-i
开关强制它是交互式的(尽管我不知道它是否可以与wsl
命令一起使用).或者,在运行脚本之前将BASH_ENV=/home/max/.bashrc
放入您的环境中.
I'm currently on the quest to getting a MIPS cross-compiler toolchain running within WSL, accessed by CLion.
There, I'm having a strange issue with WSL. I have added the following to the .bashrc
:
STAGING_DIR="/home/max/source/staging_dir/toolchain-mipsel_24kc_gcc-5.4.0_musl-1.1.16"
export STAGING_DIR
Moreover I have the script test.sh
#!/bin/sh
echo "Staging dir is: " $STAGING_DIR
When using wsl
to get a shell and execute the script, the output is as follows:
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
When using wsl <command>
to execute a bash command within WSL, the output differs. The environment variable has simply dissappeared.
C:\Users\Maxi>wsl ./test.sh
Staging dir is:
It seems like the .bashrc
(I've also tried .profile
) is not executed when executing it via wsl?`
This is a problem because when CLion invokes the WSL subsystem to build my C++ project using CMake, the compiler complains that this environment variable isn't there.
When I manually get a shell and execute the exact same cmake command, compilation works without warnings since the environment variable is there.
Inside 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
On a WSL shell:
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
What's going on here?
The ~/.bashrc file is only loaded by interactive shells. When you run your script in that fashion it is handled by a non-interactive shell. You can force it to be interactive by using the -i
switch (although I don't know if that works with the wsl
command). Alternatively, put BASH_ENV=/home/max/.bashrc
in your environment before running the script.
这篇关于WSL看不到环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!