This afternoon I unknowingly entered into the rabbit hole of creating a C++ development environment that is based in LLVM.
I quickly realized that this sounds much easier than it is… The first hint was this Github issue:
- Package request:
llvmPackages.stdenv
should be an actual Clang/LLVM toolchain #277564 - Using the
llmvPkgs_15.stdenv
toolchain #213144 - LLVM stdenvs with LLD outside of pkgsLLVM #142901
I also made my way through this forum, and dug this out:
Endgoal
I would like:
- A reproducible, isolated environment, where I can compile C++ applications using most of the LLVM tools (i.e. ofc.
glibc
is the exception, this will have to be the one that is provided by the system) - Be able to choose between
libc++
andlibstdc++
, i.e. I want to really be able to switch this. If this would require two shells, that is ofc. fine as well.
Attempt
{
description = "A LLVM-based C/C++ development environment";
inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.2411.715519.tar.gz";
outputs = { self, nixpkgs }:
let
supportedSystems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
pkgs = import nixpkgs { inherit system; };
});
in
{
devShells = forEachSupportedSystem ({ pkgs }: {
default = pkgs.mkShell.override {
stdenv = pkgs.pkgsLLVM.stdenv;
} {
packages = with pkgs; [
clang-tools
clang
libcxx
libunwind
cmake
codespell
conan
ninja
doxygen
] ++ (if system == "aarch64-darwin" then [ ] else [ gdb ]);
};
});
};
}
What is wrong with this attempt?
(some of the below issues are fixed in the above flake, but they were problems along the way)
- missing headers, i.e. using
gcc
headers instead ofllvm
headers - uses
ld
instead oflld
- cannot link
libc++
(orlibc++abi
andlibunwind
), e.g. library not around or not in the linkers path - also, I was missing
llvm-ar
,llvm-nm
,llvm-ranlib
, etc. or the wrong ones were used during a build, i.e.ar
,nm
, instead of thellvm
version. - undefined references to some symbols (I was compiling a reference library, so this should not have been a configuration error, but might be related to the previous point)
Notes
- Building LLVM, see here.