VScode 无法正确连接到 WSL

我正在尝试使用 vscode 连接到 wsl 以使用 cmake 编译我的 Fortran 代码。当我尝试配置我的项目时,cmake 报告错误说 ifort 无法编译一个简单的测试程序。完整的 cmake 输出在这里:

[main] Building folder: program 
[main] Configuring folder: program 
[proc] Executing command: /usr/local/bin/cmake --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_Fortran_COMPILER:FILEPATH=/opt/intel/oneapi/compiler/2021.3.0/linux/bin/intel64/ifort -DCMAKE_C_COMPILER:FILEPATH=/opt/intel/oneapi/compiler/2021.3.0/linux/bin/intel64/icc -H/home/user/program -B/home/user/program/build -G "Unix Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The Fortran compiler identification is Intel 2021.3.0.20210609
[cmake] -- Detecting Fortran compiler ABI info
[cmake] -- Detecting Fortran compiler ABI info - failed
[cmake] -- Check for working Fortran compiler: /opt/intel/oneapi/compiler/2021.3.0/linux/bin/intel64/ifort
[cmake] -- Check for working Fortran compiler: /opt/intel/oneapi/compiler/2021.3.0/linux/bin/intel64/ifort - broken
[cmake] CMake Error at /usr/local/lib/python3.8/dist-packages/cmake/data/share/cmake-3.21/Modules/CMakeTestFortranCompiler.cmake:54 (message):
[cmake]   The Fortran compiler
[cmake] 
[cmake]     "/opt/intel/oneapi/compiler/2021.3.0/linux/bin/intel64/ifort"
[cmake] 
[cmake]   is not able to compile a simple test program.
[cmake] 
[cmake]   It fails with the following output:
[cmake] 
[cmake]     Change Dir: /home/user/program/build/CMakeFiles/CMakeTmp
[cmake]     
[cmake]     Run Build Command(s):/usr/bin/make -f Makefile cmTC_36220/fast && /usr/bin/make  -f CMakeFiles/cmTC_36220.dir/build.make CMakeFiles/cmTC_36220.dir/build
[cmake]     make[1]: Entering directory '/home/user/program/build/CMakeFiles/CMakeTmp'
[cmake]     Building Fortran object CMakeFiles/cmTC_36220.dir/testFortranCompiler.f.o
[cmake]     /opt/intel/oneapi/compiler/2021.3.0/linux/bin/intel64/ifort    -c /home/user/program/build/CMakeFiles/CMakeTmp/testFortranCompiler.f -o CMakeFiles/cmTC_36220.dir/testFortranCompiler.f.o
[cmake]     Linking Fortran executable cmTC_36220
[cmake]     /usr/local/lib/python3.8/dist-packages/cmake/data/bin/cmake -E cmake_link_script CMakeFiles/cmTC_36220.dir/link.txt --verbose=1
[cmake]     /opt/intel/oneapi/compiler/2021.3.0/linux/bin/intel64/ifort CMakeFiles/cmTC_36220.dir/testFortranCompiler.f.o -o cmTC_36220 
[cmake]     ld: /opt/intel/oneapi/compiler/2021.3.0/linux/bin/intel64/../../bin/intel64/../../lib/icx-lto.so: error loading plugin: libimf.so: cannot open shared object file: No such file or directory
[cmake]     make[1]: *** [CMakeFiles/cmTC_36220.dir/build.make:99: cmTC_36220] Error 1
[cmake]     make[1]: Leaving directory '/home/user/program/build/CMakeFiles/CMakeTmp'
[cmake]     make: *** [Makefile:127: cmTC_36220/fast] Error 2
[cmake]     
[cmake]     
[cmake] 
[cmake]   
[cmake] 
[cmake]   CMake will not be able to correctly generate this project.
[cmake] Call Stack (most recent call first):
[cmake]   CMakeLists.txt:2 (project)
[cmake] 
[cmake] -- Configuring incomplete,errors occurred!
[cmake] See also "/home/user/program/build/CMakeFiles/CMakeOutput.log".
[cmake] See also "/home/user/program/build/CMakeFiles/CMakeError.log".
[cmake] 

我的编译器是 ifort 版本 2021.3.0.20210609,cmake 是 3.21.1,wsl1 是 Ubuntu20.04。

这个问题看起来与之前的一些问题如 CMake configuring fails. “cl.exe is not able to compile a simple test program”CMake: The C Compiler is not able to compile a simple test program 完全一样,但我发现了一些奇怪的东西:

我尝试不使用vscode的cmake插件,而是直接在vscode内置终端和wsl终端中使用cmake和make命令,一切正常。并且该错误仅在我通过 windows 启动 vscode 时发生,当通过 wsl 启动 vscode 时,一切正常

code .

还有另外两条线索让我非常担心。一、oneAPI需要用户添加

source /opt/intel/oneapi/setvars.sh

在 .bashrc 中初始化 oneAPI 环境。所以每次打开wsl终端,都会先显示:

:: initializing oneAPI environment ...
   -bash: BASH_VERSION = 5.0.17(1)-release
:: advisor -- latest
:: ccl -- latest
:: clck -- latest
:: compiler -- latest
:: dal -- latest
:: debugger -- latest
:: dev-utilities -- latest
:: dnnl -- latest
:: dpcpp-ct -- latest
:: dpl -- latest
:: inspector -- latest
:: intelpython -- latest
:: ipp -- latest
:: ippcp -- latest
:: itac -- latest
:: mkl -- latest
:: mpi -- latest
:: tbb -- latest
:: vpl -- latest
:: vtune -- latest
:: oneAPI environment initialized ::

在 user@host 之前。在vscode和wsl正常工作的电脑上,当我打开vscode并启动一个新终端时,它显示

:: WARNING: setvars.sh has already been run. Skipping re-execution.
   To force a re-execution of setvars.sh,use the '--force' option.
   Using '--force' can result in excessive use of your environment variables.

这似乎意味着当打开 vscode 并连接到 wsl 但没有显式启动 vscode 终端时,.bashrc 已经被正确读取。但是,在出现问题的计算机上,启动新的vscode终端显示“正在初始化oneAPI环境...”,如前所述,似乎即使连接到wsl,也不会自动加载.bashrc。

第二条线索是错误消息中的一行内容

ld: /opt/intel/oneapi/compiler/2021.3.0/linux/bin/intel64/../../bin/intel64/../../lib/icx-lto.so: error loading plugin: libimf.so: cannot open shared object file: No such file or directory

根据我查到的:icc compiled code unable to load shared object (libimf.so),表示有些关键路径没有添加到环境变量中,通常是因为“source /opt/intel/oneapi/setvars.sh”这一行是没有在.bashrc中添加启动oneapi环境,我明明已经添加了。

总之,这一切都让我怀疑vscode和wsl的连接没有正确设置,但不知道这个猜想是否正确,如何解决。我尝试了 here 的解决方案,但没有奏效。我也很困惑,同样的配置过程不会在其他电脑上出现任何问题。

对于我的描述如此之长,我深表歉意,任何帮助将不胜感激。

zhangyeah19887 回答:VScode 无法正确连接到 WSL

您可以尝试将以下 shell 脚本行添加到您的 WSL Linux 目标 /etc/profile 脚本中吗?此位置(或在 /etc/profile.d/ 脚本中)将确保 WSL 目标开发系统的所有用户在使用 Visual Studio Code 连接时都将运行 setvars.sh 环境脚本:

. /opt/intel/oneapi/setvars.sh &>/dev/null
本文链接:https://www.f2er.com/2309.html

大家都在问