VSCode cpptools setup and intellisense

I have followed the instructions in this thread to install CppTools.
Now I want to develop a opengl application. To do that, I drop into the nix-shell:

nix-shell -p glm glfw3 glew mesa_glu mesa_drivers libGL_driver clang

which is supposed to setup all required libraries. This works and I can use Make to create an executable.
Can I configure the intellisense include path to find the sources and documentation to these libraries?
Also, how can I configure the run tasks to execute the program within a nix-shell? i have the following:

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "build and run",
            "type": "shell",
            "command": "nix-shell -p glm glfw3 glew mesa_glu mesa_drivers libGL_driver clang --run 'make clean && make && ./bin/program'"
        }
    ]
}

This works, but seems clunky.

Maybe it is possible to define an appropriate shell.nix installs a fitting vscode and configures the include path?

For both ccls and cquery we have wrappers that will read the environment variables that also our c/c++ compiler wrapper use to find include paths. Something similar would be required for cpptools. There is also a writeup how C/C++ is handled in nixpkgs: C - NixOS Wiki
An alternative might be to use ccls right-away: GitHub - MaskRay/vscode-ccls: ccls plugin for Visual Studio Code

How can I install ccls?
Also, will this find includes that are only available in a nix-shell?

It can be installed like any other package via nix-env, home-manager or configuration.nix depending on your preference. The package is called ccls. It will only packages in your nix-shell. The reason for that is also explained in the wiki article. If you don’t like nix-shell then you can have a look at direnv’s nix integration: Nix · direnv/direnv Wiki · GitHub

Update at the time ccls is only available in unstable or the upcoming release 19.03. You can get it for 18.09 following this guide

How could I integrate it into my VSCode workflow?
Installation worked, but it looks like it cant pick up the libraries.
I did the following:

nix-shell -p glm glfw3 glew mesa_glu mesa_drivers libGL_driver clang --run "code ."

But it cant find the library glm, for example.

I have not used it with vscode but with vim/emacs. At minimum you need to generate a compilation database as described in their wiki: Project Setup · MaskRay/ccls Wiki · GitHub

1 Like

Instructions for clang actually did the trick!
Now the language server works pretty well! I dont know yet, if I have to manually regenerate these files if I add a new *.cpp file, but for me, this is bearable. Maybe use a script to regenerate the file from time to time.

I personally use cquery in vscode for all my C/C++ development, and it works really well. All you need is a compile_commands.json file, you don’t need to run vscode inside nix-shell or anything.

manually regenerate these files if I add a new *.cpp file, but for me, this is bearable.

Depending on the build tools that a project uses, you can typically set it up so that compile_commands.json is regenerated on every build:

  • For cmake, you just need to add the -DCMAKE_EXPORT_COMPILE_COMMANDS=YES option when calling cmake. This will update the json file every time cmake is run (e.g. if you add a C++ file to the CMakeLists.txt)
  • For make-based projects, I can only recommend bear. Just run bear make instead of make and you automatically get an up-to-date compile_commands.json every time.