You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NixOS 24.05下CMake无法找到gtkmm3的问题求助

问题:CMake无法找到gtkmm3,但g++编译正常(Nix环境)

在./build目录执行cmake ..时输出如下错误:

-- Checking for module 'gtkmm3'
--   Package 'gtkmm3', required by 'virtual:world', not found
CMake Error at /nix/store/i78xz11da295kd8ynsjmxvi2r56rbhzw-clion-2024.1.3/clion/bin/cmake/linux/x64/share/cmake-3.28/Modules/FindPkgConfig.cmake:619 (message):
  The following required packages were not found:

   - gtkmm3

Call Stack (most recent call first):
  /nix/store/i78xz11da295kd8ynsjmxvi2r56rbhzw-clion-2024.1.3/clion/bin/cmake/linux/x64/share/cmake-3.28/Modules/FindPkgConfig.cmake:841 (_pkg_check_modules_internal)
  CMakeLists.txt:20 (pkg_check_modules)


-- Configuring incomplete, errors occurred!

但使用g++直接编译无错误:

g++ -o ./build/bin/button_example ./src/main.cpp `pkg-config --cflags --libs gtkmm-3.0`

现有配置文件

Nix Flake配置

{
  description = "A Nix-flake-based C/C++ development environment with GTK support";

  inputs.nixpkgs.url = "https://flakehub.com/f/NixOS/nixpkgs/0.1.*.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
          {
            # Optionally override stdenv for specific compiler settings:
            # stdenv = pkgs.clangStdenv;
          }
          {
            packages = with pkgs; [
              clang-tools
              cmake
              codespell
              conan
              cppcheck
              doxygen
              gtest
              lcov
              vcpkg
              vcpkg-tool
              gtkmm3
              pkg-config
            ] ++ (if system == "aarch64-darwin" then [ ] else [ gdb ]);
          };
      });
    };
}

CMakeLists.txt

# Minimum CMake version
cmake_minimum_required(VERSION 3.21)

# Define the project
project(calculator VERSION 1.0 LANGUAGES CXX)

# Enable C++20 standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)


# Set the output directory for binaries
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

# Include directories
include_directories(${CMAKE_SOURCE_DIR}/src)

# Find gtkmm-3.0 package using pkg-config
find_package(PkgConfig REQUIRED)
pkg_check_modules(GTKMM REQUIRED gtkmm3)

# Include GTKMM directories and link libraries
include_directories(${GTKMM_INCLUDE_DIRS})
link_directories(${GTKMM_LIBRARY_DIRS})
add_definitions(${GTKMM_CFLAGS_OTHER})

# Add the source files
file(GLOB_RECURSE SOURCES "src/*.cpp")

# Create the executable
add_executable(calculator ${SOURCES})

# Link GTKMM libraries
target_link_libraries(calculator ${GTKMM_LIBRARIES})

解决办法

1. 修正CMake中pkg-config模块名称

g++命令使用的是gtkmm-3.0,但CMake里写的是gtkmm3——Nix环境下gtkmm3的pkg-config模块实际名称是gtkmm-3.0,修改CMakeLists.txt对应行:

pkg_check_modules(GTKMM REQUIRED gtkmm-3.0)

2. 确认Nix devShell环境正确性

执行nix develop进入flake定义的开发环境后,运行pkg-config --list-all | grep gtkmm,确认能输出gtkmm-3.0相关信息。如果没有输出,检查是否正确进入了devShell。

3. 替换CLion自带CMake(可选)

报错显示使用的是CLion自带的CMake,在Nix环境下可能存在路径兼容问题。可以在CLion设置中(Build, Execution, Deployment > CMake),将CMake路径改为devShell中提供的版本(执行which cmake获取路径)。

4. 改用现代CMake写法(可选)

替换旧的全局路径设置为目标级配置,更符合现代CMake规范:

# 替换原有的include_directories、link_directories、add_definitions
target_include_directories(calculator PRIVATE ${GTKMM_INCLUDE_DIRS})
target_link_options(calculator PRIVATE ${GTKMM_LDFLAGS})
target_compile_options(calculator PRIVATE ${GTKMM_CFLAGS_OTHER})

内容的提问来源于stack exchange,提问作者Eris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 11:45:02