Hi,
I am trying to get basic C and C++ development working on NixOS 24.05. I’m using Neovim for development, but I’m running into some issues compiling anything and with getting clangd working in
I think I have two separate but related problems right now.
First, I can’t reliably compile a source file using clang
or clang++
. For any source file, compilation fails when the linker can’t find stdlib
headers, like <stdio.h>
using clang
or <cstdio>
using clang++
. However, on the C++, side, using c++
instead of clang++
works. I don’t understand why this is, because I was under the impression that c++
is aliased to clang++
but I guess not. I know that I have a working standard library, because compilation with gcc
and g++
both work.
Second, clangd cannot resolve any standard library headers.
I think this might be related to missing a compile_commands.json
file at the project root. I tried generating it using bear
, like
$ bear -- c++ main.cpp
which didn’t fail, but also didn’t fix the problem. As an additional note, VSCode also cannot find the headers.
Here’s the list of packages I have installed relevant to C/C++:
- clang
- bear
- clang-tools
- libclang
- libgcc
- gdb
- llvmPackages_latest.lldb
- llvmPackages_latest.libllvm
- llvmPackages_latest.libcxx
I apologize if these are dumb issues, but I have a feeling that these are NixOS specific, since I have a nearly identical setup working on a Debian machine.
Thanks in advance
Update:
Ok, I’m able to build using this flake and nix develop
:
{
description = "C and C++";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils, ... }:
flake-utils.lib.eachDefaultSystem (
system:
let
pkgs = import nixpkgs { inherit system; };
llvm = pkgs.llvmPackages_latest;
lib = nixpkgs.lib;
in
{
devShell = pkgs.mkShell {
nativeBuildInputs = [
pkgs.cmake
llvm.lldb
pkgs.clang-tools
llvm.clang
pkgs.gtest
];
buildInputs = [
llvm.libcxx
];
CPATH = builtins.concatStringsSep ":" [
(lib.makeSearchPathOutput "dev" "include" [ llvm.libcxx ])
(lib.makeSearchPath "resource-root/include" [ llvm.clang ])
];
};
}
);
}
I’m seeing that this flake explicitly sets my CPATH variable to where all of my headers are. However, this doesn’t fix my original problem. There has to be some way to build C/C++ with clang without flakes. It also doesn’t fix clangd.