I create a very simple C project using LLVM. Here is my flake.nix:
{
description = "A Nix-flake-based Python development environment";
inputs.nixpkgs.url = "github:nixos/nixpkgs/nixos-unstable";
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 {
packages = with pkgs; [
llvm_18
zlib
];
};
});
};
}
I can build my project using command:
clang++ -o main main.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core`
However, I cannot build my project using CMake when using nix installed llvm_18 or libllvm package, since it cannot find zlib cmake file possibly. (I can build this project using my manual build llvm since it doesnât enable zlib â the build log shows that zlib is missing).
My CMakeLists.txt file:
cmake_minimum_required(VERSION 3.20.0)
project(SimpleProject LANGUAGES CXX)
# Find LLVM package
find_package(LLVM REQUIRED CONFIG)
# Store LLVM include directories in cache
set(LLVM_INCLUDE_DIRS ${LLVM_INCLUDE_DIRS} CACHE STRING "LLVM include directories")
include_directories(${LLVM_INCLUDE_DIRS})
separate_arguments(LLVM_DEFINITIONS_LIST NATIVE_COMMAND ${LLVM_DEFINITIONS})
add_definitions(${LLVM_DEFINITIONS_LIST})
# Now build our tools
add_executable(main main.cpp)
# Link against LLVM libraries
llvm_map_components_to_libnames(llvm_libs support core irreader transformutils)
target_link_libraries(main ${llvm_libs})
The error message is: (using command cmake -S . -B build)
-- Could NOT find FFI (missing: FFI_LIBRARIES HAVE_FFI_CALL)
-- Could NOT find Terminfo (missing: Terminfo_LIBRARIES Terminfo_LINKABLE)
-- Could NOT find ZLIB (missing: ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
-- Could NOT find LibXml2 (missing: LIBXML2_LIBRARY LIBXML2_INCLUDE_DIR)
-- Found LLVM 18.1.8
-- Using LLVMConfig.cmake in: /nix/store/wijipxmz3lnwbcwrwl5r34mw20qlqwi1-llvm-18.1.8-dev/lib/cmake/llvm
-- LLVM include directories: /nix/store/wijipxmz3lnwbcwrwl5r34mw20qlqwi1-llvm-18.1.8-dev/include
-- Configuring done (0.0s)
CMake Error at /nix/store/wijipxmz3lnwbcwrwl5r34mw20qlqwi1-llvm-18.1.8-dev/lib/cmake/llvm/LLVMExports.cmake:58 (set_target_properties):
The link interface of target "LLVMSupport" contains:
ZLIB::ZLIB
but the target was not found. Possible reasons include:
* There is a typo in the target name.
* A find_package call is missing for an IMPORTED target.
* An ALIAS target is missing.
Call Stack (most recent call first):
/nix/store/wijipxmz3lnwbcwrwl5r34mw20qlqwi1-llvm-18.1.8-dev/lib/cmake/llvm/LLVMConfig.cmake:347 (include)
CMakeLists.txt:5 (find_package)
I also tried nix-shell -p zlib, but it didât change anything.