Cannot build my LLVM related project using CMake (ZLIB::ZLIB not found)

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.

Does it work when you add pkg-config to the packages?

No, it doesn’t work.

What about adding cmake?

Amazing. It works! Thank you :slight_smile:
Though I cannot find a single cmake keyword in zlib’s nix package file. Don’t know how this magic work.

1 Like

Well, it was kinda confusing because cmake was not in the devshell, but I could execute it somehow and it gave the exact same error that you got. However, I tried to reproduce that after the first time but I couldn’t and it said cmake was not found. Added it to the packages and it worked.

1 Like

It turns out zlib is not necessary.

          packages = with pkgs; [
            cmake
            llvm_18
          ];
1 Like