I am trying to make some basic nix-shell
for my C/C++ development. I am a big fan of LLVM and their tools. I managed to get the clang++
working with llvmPackages_latest.llvm
(because it could not find and include anything from libc++
.
But when it comes to clang-tidy
or cppcheck
I just cannot make it work properly. I looked into the older threads like: clang-tooling-woes-on-nixos. But none of it really helped me.
Using this shell.nix
file:
{
pkgs ? import <nixpkgs> {},
stdenv ? pkgs.clangStdenv
}:
pkgs.mkShell {
name = "C/C++ Study Environment";
buildInputs = with pkgs; [
pkg-config
gnumake
ninja
cmake
meson
bashInteractive
llvmPackages_latest.llvm
llvmPackages_latest.libcxxClang
llvmPackages_latest.libcxxStdenv
lld
clang
clang-tools
cppcheck
];
shellHook = with pkgs; ''
export CC=clang
export CXX=clang++
'';
}
and simple hello world in C:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
when trying to run clang-tidy hello.c
I get this error:
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "test.c"
No compilation database found in /home/dan/Documents/Code/Cpp or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
1 error generated.
Error while processing /home/dan/Documents/Code/Cpp/test.c.
/home/dan/Documents/Code/Cpp/test.c:1:10: error: 'stdio.h' file not found [clang-diagnostic-error]
#include <stdio.h>
^~~~~~~~~
Found compiler error(s).
and if I manually set CPATH
variable in the shellHook
to something like this (I realize that it is not a good idea to override this variable without $CPATH:
, this is only for testing if it works):
export CPATH=/nix/store/k3d8wqlsnmm5270zd19cbs26g7wifxj6-glibc-2.34-210-dev/include
Then I get a new error, I cannot find stddef.h
:
Error while trying to load a compilation database:
Could not auto-detect compilation database for file "test.c"
No compilation database found in /home/dan/Documents/Code/Cpp or any parent directory
fixed-compilation-database: Error while opening fixed database: No such file or directory
json-compilation-database: Error while opening JSON database: No such file or directory
Running without flags.
1 error generated.
Error while processing /home/dan/Documents/Code/Cpp/test.c.
/nix/store/k3d8wqlsnmm5270zd19cbs26g7wifxj6-glibc-2.34-210-dev/include/stdio.h:33:10: error: 'stddef.h' file not found [clang-diagnostic-error]
#include <stddef.h>
^~~~~~~~~~
Found compiler error(s).
And the CppCheck gives me this error, every time:
Checking hello.c ...
test.c:1:0: information: Include file: <stdio.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem]
^
LLVM and clang is not installed on the system level.
With my current knowledge I simply cannot get this to work. I only want to lint my code with clang-tidy and cppcheck is here some easy fix?