A Fortran example project using Visual Studio and Windows Subsystem for Linux (WSL).
- Visual Studio 2019
- Windows Subsystem for Linux (WSL)
Note: This was tested on a system with Windows 10 2004, Visual Studio 16.6 and WSL 2.
Download and install a Linux distribution for WSL. This example was tested with Debian Buster, but other distributions should work just as well.
Install the packages needed for your WSL installation to work with Visual Studio:
sudo apt install g++ gdb make ninja-build rsync zip
For more detailed instructions see the Linux development with C++ documentation.
In addition we need to install the gfortran
compiler:
sudo apt install gfortran
Start Visual Studio and open the vs-wsl-fortran folder. Select the WSL-GCC-Debug target, then build it.
Select vs-wsl-fortran (src\vs-wsl-fortran) as the startup item and start it (F5). To see the output you might need to open the Linux Console Window view.
The mingw compiler can be used to create Windows binaries. Install an appropriate mingw package in the WSL installation, for example:
sudo aptitude install gfortran-mingw-w64-x86-64
In Visual Studio select the WSL-MINGW-Release target and build it.
After building the executable you still need to copy some DLLs into that folder. In the case of Debian these DLLs are provided by the mingw package:
cd <vs-wsl-fortran_folder>/out/build/WSL-MINGW-Release/src
cp /usr/lib/gcc/x86_64-w64-mingw32/8.3-win32/*.dll .
Now you can run the executable in a Windows PowerShell or CMD:
.\vs-wsl-fortran.exe
Ninja is not yet supported for Fortran, so generator has to be set to "Unix Makefiles" instead.
When cross compiling the compiler has to be specified with "-DCMAKE_Fortran_COMPILER=/usr/bin/x86_64-w64-mingw32-gfortran" in the CMake Command Arguments.
This is not necessary when targeting Linux, in that case the default gfortran
compiler is detected automatically.
By default Visual Studio does not provide any syntax highlighting for Fortran code, but it can be added as described here.
First create the following folder:
%userprofile%\.vs\Extensions\Syntaxes\
Then download an appropriate grammar.json file for Fortran and store it in this folder. You can take for example the grammar files used by the Modern Fortran plugin for VSCode, you just have to copy the .json files from
https://github.com/krvajal/vscode-fortran-support/tree/master/syntaxes
into your Syntaxes folder.
You may need to restart Visual Studio before the new highlighting becomes visible.