HowTo Use OpenFOAM with Visual Studio Code
The C++ language server (ccls) can read and analyze C++ source code to provide features like "Go to Definition" in many Editors. Because ccls needs to know how the files are compiled, you need to do a bit of configuration. This guide shows how to set this up for OpenFOAM in Visual Studio Code.
- Install VS Code
- Install the ccls vs code extension
- Install bear
- Rebuild OpenFOAM and all your custom libraries that use wmake and pass the "-with-bear" option to wmake / Allwmake . If you get a "unknown option: '-with-bear' " error, it is because your OpenFOAM version is too old. But don't worry, I wrote a backport for OpenFOAM8 and a backport for OpenFOAM7. If you use an even older version of OpenFOAM, you can easily adopt those backports. Note that this backported version does not require you to pass "-with-bear" and might be slower.
You can now either setup a folder or a workspace to work with OpenFOAM.
Setting up a workspace
- Run the command
openfoam-root-dir/bin/tools/vscode-settings > openfoam.code-workspace(you need to set the environment for that)
- In VS Code, select File -> Open Workspace and select the openfoam.code-workspace file generated by the previous step.
Setting up a folder:
- Run the command
openfoam-root-dir/bin/tools/vscode-settings > .vscode/settings.json(you need to set the environment for that)
- In VS Code, open the folder where you ran the command. Note that it does not matter whether the source files are in this folder or not.
Note that after doing the steps above, "Go to definition" or similar features will only work after ccls finished generating its cache. This process can take a while (see if there is a "ccls" process that heavily uses the cpu).
For questions about this setup ask Volker Weißmann.
If you find that "Go to Definition" or some other features do not work on some specific function, please file a bug report for the ccls plugin.