Visual Studio Code C/C++/Fortran with Multiple Source Files (2023)

Visual Studio Code is a free source-code editor made by Microsoft for Windows, Linux and macOS. Features include support for debugging, syntax highlighting, intelligent code completion, snippets, code refactoring, and embedded Git. C/C++ support for Visual Studio Code is provided by a Microsoft C/C++ extension to enable cross-platform C and C++ development on Windows, Linux, and macOS. The Code Runner extension allows execution of single files. For project compilation, consisting of multiple files, the C/C++ Makefile Project extension can be used (and can be adapted for Fortran). Visual Code Studio can be downloaded from the websitehttps://code.visualstudio.com/download. VS Code is first and foremost an editor, and relies on command-line tools to do much of the development workflow. There is basic support for html, css, javascript and typescript out of the baox. Support for other languages is supported via extensions. VS Code does not include a C/C++ or Fortran compiler or debugger. You will need to install these tools or use those already installed on your computer.

Table of Contents

Installing a C/C++/Fortran Compiler

On MacOS, first check if the compiler toolset is already present

gcc --version

which show the compiler options if the compiler is present, for example

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX.sdk/usr/include/c++/4.2.1Apple clang version 12.0.0 (clang-1200.0.32.28)Target: x86_64-apple-darwin20.2.0Thread model: posixInstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin

If it is not present, then it will present an install panel. The installation can also be forced using

xcode-select --install

The Apple toolchain does not include a Fortran compiler. Using home-brew, it easy to install one. Homebrew can be installed using

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

and then installing the gcc package

brew install gccbrew install python3pip3 install fortran-language-server

which include the ‘gfortran’ compiler. Optional: also install python3 for Fortran IntelliSense (python has a Fortran implementation of the Language Server Protocol).

On linux, for example Ubuntu-type systems, the compilers can be installed using

sudo apt install build-essentialsudo apt install gfortransudo apt install python3sudo apt install python3-pipsudo pip3 install fortran-language-server

On windows, a compiler needs to be installed. MSYS2 is a collection of tools and libraries providing you with an easy-to-use build system. Download MSYS2 from https://www.msys2.org. Follow the installation instruction steps, including step 7 which installs the C/C++/gfortran compiler and build utilities like ‘make’.

(download the installer)(run the installer)(enter the desired installation folder, leave as default)(when done, run MSYS2 now)pacman -Syupacman -Supacman -S --needed base-devel mingw-w64-x86_64-toolchain pacman -S mingw-w64-x86_64-python3-pip mingw-w64-x86_64-python3-setuptools/mingw64/bin/pip3 install fortran-language-serverpacman -S mingw-w64-x86_64-lapackpacman -S git unzip zsh

After step 5, the terminal will closed, and you need to reopen ‘MSYS2 MSYS’ from the Start menu. After step 7, you will have a fully functional build system and have access to ‘gcc’, ‘g++’, ‘gfortran’, and ‘make’ to build software. Step 10 and 11 show that the MSYS2 bash environment can be extended with a lot of packages. The installation of lapack allows the linking with ‘-llapack -lblas’ to solve for example eigenvalue problems. Git, unzip are useful utilities, and you could also for example change the default bash shell and use zsh (also used on mac).

Steps 8 and 9 install the fortran language server in python. There are two versions of python in MSYS2: (1)MSYS2 – POSIX applications emulated to work in Windows, and (2)MINGW – Windows native applications. We need the latter, since the IntelliSense in VS Code calls the fortran compiler (fortran.exe) and fortran language server (fortls.exe) from windows.MINGW refers to executables that are compiled using the MINGW GCC Compiler and target the Win32 API.

Install Visual Studio Code extensions

Launch Visual Studio Code. In the side bar (left), click the “Extensions” icon, and install the following extensions:

Visual Studio Code C/C++/Fortran with Multiple Source Files (1)

The C/C++ extension adds language support for C/C++ to Visual Studio Code, including features such as IntelliSense and debugging. The Makefile extension is generating a simple C/C++ project structure together with a Makefile which fits almost 99% of all small project’s needs. Code Runner adds a ‘Run Code’ button in the top-bar and in the context-menu when right-clicking on a file. The Fortran extensions for VS Code provides support for syntax highlighting, IntelliSense, and debugging.

Settings

In the side bar (left), click the “Manage” icon, and select “Settings”.In the settings, search for “code runner”, the relevant items are the three items shown in the picture below. Make sure these three options are “on”. The “Run in Terminal” option is needed to also allow keyboard-input into a C/C++ program. If this option is off, the output will be immutably printed to the output-console. The terminal is more powerful and allows interactive use. The other two options are convenient to ensure that the files are always saved before compilation. Lastly, to avoid accidentally running selected code snippets, set ‘ignoreSelection’ to ‘true’.

Visual Studio Code C/C++/Fortran with Multiple Source Files (2)

In the settings, search for “executor map”, the relevant item is the “Code-runner: Executor Map”. Click “Edit in settings.json”.

Visual Studio Code C/C++/Fortran with Multiple Source Files (3)

The settings file lists the rule “Code runner” is using to compile source files. Line 1 is for C, and line 2 is the rule for C++. The default for C/C++ and fortran reads

"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt","cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt","FortranFreeForm": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt","fortran-modern": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt","fortran_fixed-form": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt","fortran": "cd $dir && gfortran $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
"c": "cd \"$workspaceRoot\" ; make -f Makefile && ./myapp;","cpp": "cd \"$workspaceRoot\"; make -f Makefile && ./myapp;","FortranFreeForm": "cd \"$workspaceRoot\"; make -f Makefile && ./myapp;","fortran-modern": "cd \"$workspaceRoot\"; make -f Makefile && ./myapp;","fortran_fixed-form": "cd \"$workspaceRoot\"; make -f Makefile && ./myapp;","fortran": "cd \"$workspaceRoot\"; make -f Makefile && ./myapp;","makefile": "cd \"$workspaceRoot\"; make -f Makefile && ./myapp;",

Save the file. The last line adds the rule for the Makefile. For all of these type of files, the directory is switched to where the makefile is, make is run, and if successful, then the ‘./myapp’ program is executed. One of the Supported customized parameters of Code Runner is $workspaceRoot: The path of the folder opened in VS Code. This is the location of the Makefile. The quotes around the$workspaceRoot are because a directory-name can contain spaces.

For Mac and linux, we are done. Windows requires an additional step, we need to set the internal terminal to the bash-shell of MSYS2. In settings, search for the integrated terminal shell for windows and edit the settings.json.

Visual Studio Code C/C++/Fortran with Multiple Source Files (4)
Visual Studio Code C/C++/Fortran with Multiple Source Files (5)

Modify the ‘settings.json’ file, add the following lines

"terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe","terminal.integrated.shellArgs.windows": ["-i"],"terminal.integrated.env.windows": { "PATH" : "/mingw64/bin:/usr/local/bin:/usr/bin:/bin:/c/Windows/System32:/c/Windows:/c/Windows/System32/Wbem:/c/Windows/System32/WindowsPowerShell/v1.0/" },"code-runner.saveFileBeforeRun": true,"code-runner.saveAllFilesBeforeRun": true,"code-runner.runInTerminal": true,"code-runner.ignoreSelection": true,"fortran.gfortranExecutable": "C:\\msys64\\mingw64\\bin\\gfortran.exe","fortran-ls.executablePath": "C:\\msys64\\mingw64\\bin\\fortls.exe"

and save the file. Also the fortran executable and the fortran language server are set to the ones of MSYS2. Note that the “code-runner” lines might already be present. The setting of options via the GUI is identical to setting the options in this json file.

Creating and Compiling a Small C++ project

The first step is to create an empty directory, for example named ‘Project’, and opening that folder as a new project.

Visual Studio Code C/C++/Fortran with Multiple Source Files (6)

Next, initialize the project in the current working directory withCtrl+Shift+Pon a Windows/Linux orCMD+Shift+Pon a Mac and chooseC/C++ Make: INIT Projectand C++ Project.

Visual Studio Code C/C++/Fortran with Multiple Source Files (7)
Visual Studio Code C/C++/Fortran with Multiple Source Files (8)

Now the Makefile is created for a C++ project consisting of cpp and header files. The Makefile settings are fine as they are. The executable program will be named ‘myapp’. The compiler settings can be modified, for example to change the C++ version and warning flags, or to add debug compiler flags.

Visual Studio Code C/C++/Fortran with Multiple Source Files (9)

To start coding on a new project, set the compiler flags to ‘debug’ settings to get the most help out of the compiler (and debugger). Later these compiler flags can be modified for ‘release’ settings to optimize the code for speed.

CXXFLAGS = -g -O0 -Wall -Wextra -Wshadow -Wnon-virtual-dtor -pedantic

New files need to be created in the ‘src’ directory. Make sure to select the ‘src’ directory (and not the ‘src/include’ directory), and create a new file.

Visual Studio Code C/C++/Fortran with Multiple Source Files (10)

Name the file “main.cpp” and let’s add a prototypical Hello World C++ program.

Visual Studio Code C/C++/Fortran with Multiple Source Files (11)

The Makefile extension performs the same steps as we would have to do manually to compile the source. But the Makefile extension becomes very convenient once the codes grows in size using multiple files. The compile steps and the result of running the program can be seen below.

Visual Studio Code C/C++/Fortran with Multiple Source Files (13)

Modification for Fortran

The same Makefile framework can be used for Fortran. The first difference is that files have the ‘.f90’ extension in Fortran. Files with this extension will automatically be in ‘free format’ (this new form uses a much wider character set and it allows much greater freedom in laying out statements on a line.). The second difference is the Makefile is changed to reflect the settings needed for compilation of Fortran files.

Visual Studio Code C/C++/Fortran with Multiple Source Files (14)
# Compiler settings - Can be customized.CC = gfortranCPP = gfortran -cppCXXFLAGS = -g -WallLDFLAGS = # Makefile settings - Can be customized.APPNAME = myappEXT = .f90SRCDIR = srcOBJDIR = obj

IntelliSense

But hovering over a defined subroutine, you get information on the arguments and the types. Also, right-click and “Go to Definition” will take you to the implementation.

Visual Studio Code C/C++/Fortran with Multiple Source Files (15)
Visual Studio Code C/C++/Fortran with Multiple Source Files (16)

Debugging

The setup of debugging is straightforward. The first time the Debug-Icon is clicked a setup will appear.

Visual Studio Code C/C++/Fortran with Multiple Source Files (17)

A file ‘launch.json’ will be created in the project. The only change needed on MacOS is to set the ‘program’. After saving, using debug breakpoints should be fully functional.

Visual Studio Code C/C++/Fortran with Multiple Source Files (18)

On windows, an additional modification is needed: the path to the gdb debugger from MSYS2 needs to be set.

Visual Studio Code C/C++/Fortran with Multiple Source Files (19)
"program": "${workspaceFolder}/myapp","MIMode": "gdb","miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe"

With these modifications, debugging and breakpoints work. You can “step over”, “step into”, “step out”, and have options to “continue”, “pause”, and “stop”.

Visual Studio Code C/C++/Fortran with Multiple Source Files (20)

Automatically Compile Project before Debugging

First add the ‘preLaunchTask’ called ‘build’ to the launch task. Next, select “Terminal” in the menu, and “Run Build Task”. Since no task is configured yet, a popup appears. Create ‘tasks.json’ from a template. Choose ‘others’ as template, and configure the build task.

Visual Studio Code C/C++/Fortran with Multiple Source Files (21)
Visual Studio Code C/C++/Fortran with Multiple Source Files (22)
Visual Studio Code C/C++/Fortran with Multiple Source Files (23)
Visual Studio Code C/C++/Fortran with Multiple Source Files (24)

Next the scripts needs to edited to look like below.

Visual Studio Code C/C++/Fortran with Multiple Source Files (25)
{ // See https://go.microsoft.com/fwlink/?LinkId=733558 // for the documentation about the tasks.json format "version": "2.0.0", "tasks": [ { "label": "build", "type": "shell", "command": "make -f Makefile", "group": { "kind": "build", "isDefault": true }, "presentation": { "reveal": "silent", "panel": "shared" }, "problemMatcher": [] } ]}

The type is a shell-script. The ‘presentation’ is set to ‘silent’ and ‘shared’ which means the terminal will be brought to the front only in case of an error, and that the output of this and other tasks will be shown in the same terminal, respectively. The ‘group’ is set to ‘build’ which means it can be executed with⇧⌘B.

When starting the debugger, the preLaunchTask will now automatically be run first (compiling the project).

Using WSL instead of MSYS2

Instead of using MSYS2, it is also possible to make use of the WSL system, if that is installed. This is a more appropriate setup if your files and projects are located in the WSL environment. The ‘Remote WSL’ extension is required to make this work. After installing it, a green button appear at the lower left bottom. Choose ‘remote-WSL Reopen Folder in WSL’ to open a folder in your WSL system. After a while, in your VS code interface, it will ask you whether you would like to install the C/C++ and Fortran extensions also in the WSL-Ubuntu.

Visual Studio Code C/C++/Fortran with Multiple Source Files (26)

Inside WSL, the C/C++/Fortran compilers needs to be installed, as well as the fortran language server. Run inside WSL

sudo apt install make gfortran build-essential liblapack-dev libblas-dev python3 python3-pipsudo pip3 install fortran-language-server

To make use of the WSL compilers, the setting ‘settings.json’ can be adjusted and modified for WSL.

"fortran.gfortranExecutable": "/usr/bin/gfortran","fortran-ls.executablePath": "/usr/local/bin/fortls"

Uninstalling

To uninstall VS Code on macOS, delete the directories

~/.vscode~/Library/Application\ Support/Code

To uninstall MSYS2 and VS Code on windows run

C:\msys64\maintenancetool.exeC:\Program Files\Microsoft VS Code\unins000.exe

To remove the VS Code settings and to remove the extension, delete the two following user folders

%UserFolder%\AppData\Roaming\Code%UserFolder%\.vscode

Sources

Visual Studio Code C/C++/Fortran with Multiple Source Files (27)

GCC & clang on windows with Visual Studio Code + bash terminal + debugging!Daniel Elliott Jones

Read More

Daniel Elliott Jones explains how to setup MSYS2 for windows and set the internal terminal to the bash terminal.

Visual Studio Code C/C++/Fortran with Multiple Source Files (28)

C++ Tutorial for Beginners #2: Visual Studio Code - Makefile & Multi File Extension | 2021 | (Linux)SavvyNik

Read More

SavvyNik explains the Makefile Project Extension for Visual Studio Code. This C++ Tutorial is intended for C/C++ Beginners with Programming in 2021 and will help you use Visual Studio Code with Programming a C/C++ project.Since Visual Studio Code doesn’t have an easy way of compiling multiple files together we will install a new extension called C/C++ Makefile Project. This extension will help generate a makefile for our project and allow us to compile multiple source files together very easily no matter home many we have. This will be useful down the road.

Visual Studio Code C/C++/Fortran with Multiple Source Files (29)

VS Code for Fortran Ep.2: Extensions & RulersLukas Lamm

Read More

The second episode of this series shows you how to set up two important extensions for Fortran in VS Code. You will also learn how to set up rulers in VS Code to avoid line truncation errors. Duration: 8 minutes.

Visual Studio Code C/C++/Fortran with Multiple Source Files (30)

Debugging C++ on VSCode w/ WSLEECS 281

Read More

Rushil covers how to set up debugging on VSCode with WSL, along with some common errors. Duration: 8 minutes.

Top Articles
Latest Posts
Article information

Author: Merrill Bechtelar CPA

Last Updated: 01/12/2023

Views: 6364

Rating: 5 / 5 (70 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Merrill Bechtelar CPA

Birthday: 1996-05-19

Address: Apt. 114 873 White Lodge, Libbyfurt, CA 93006

Phone: +5983010455207

Job: Legacy Representative

Hobby: Blacksmithing, Urban exploration, Sudoku, Slacklining, Creative writing, Community, Letterboxing

Introduction: My name is Merrill Bechtelar CPA, I am a clean, agreeable, glorious, magnificent, witty, enchanting, comfortable person who loves writing and wants to share my knowledge and understanding with you.