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

CMake无法找到外部可执行文件Emacs的问题求助

问题描述

我的系统中已安装Emacs:

$ which emacs
/usr/bin/emacs
$ emacs --version
GNU Emacs 29.4

但我无法让CMake找到它,以下是CMakeLists.txt中的对应代码片段:

find_program(EMACS emacs)
message(STATUS "EMACS=${EMACS}")
if(EMACS_FOUND)
    execute_process(COMMAND ${EMACS_EXECUTABLE} ./text.org --batch -f org-latex-export-to-pdf --kill
                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                    RESULT_VARIABLE EMACS_RESULT)
    if(NOT EMACS_RESULT EQUAL "0")
        message(WARNING "Emacs error: ${EMACS_RESULT} - cannot generate documentation.")
    endif()
else()
    message(WARNING "Emacs not found: cannot generate documentation.")
endif()

在IDE(KDevelop)中运行的输出如下:

-- EMACS=EMACS-NOTFOUND
CMake Warning at CMakeLists.txt:65 (message):
  Emacs not found: cannot generate documentation.

在命令行(Bash)中运行的输出如下:

-- EMACS=/usr/bin/emacs
CMake Warning at CMakeLists.txt:65 (message):
  Emacs not found: cannot generate documentation.

请问我哪里操作出错了?


问题分析与解决

你的CMake代码存在两处关键错误,另外IDE环境还有PATH配置问题:

  • 存在性判断变量错误
    find_program(EMACS emacs)执行后,CMake会将找到的程序路径存入EMACS变量,未找到时则为EMACS-NOTFOUND。你用EMACS_FOUND判断是否找到,虽然理论上find_program会生成该布尔变量,但实际场景中直接判断EMACS变量是否有效更可靠(EMACS-NOTFOUND在CMake条件判断中会被视为假)。命令行中明明已找到Emacs却进入“未找到”分支,就是因为EMACS_FOUND变量未正确生效,换成if(EMACS)即可解决。

  • 执行命令时用错变量
    EMACS_EXECUTABLE是find_package(Emacs)才会生成的变量,而你用的是find_program(EMACS emacs),所以执行命令时应该用你自己定义的${EMACS}变量,而非${EMACS_EXECUTABLE}。

修正后的CMake代码片段:

find_program(EMACS emacs)
message(STATUS "EMACS=${EMACS}")
if(EMACS)
    execute_process(COMMAND ${EMACS} ./text.org --batch -f org-latex-export-to-pdf --kill
                    WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
                    RESULT_VARIABLE EMACS_RESULT)
    if(NOT EMACS_RESULT EQUAL 0)
        message(WARNING "Emacs error: ${EMACS_RESULT} - cannot generate documentation.")
    endif()
else()
    message(WARNING "Emacs not found: cannot generate documentation.")
endif()
  • IDE(KDevelop)找不到Emacs的问题
    KDevelop的运行环境PATH通常和命令行Bash的PATH不同,可能/usr/bin不在IDE的搜索路径中。解决方法二选一:
    1. 在KDevelop的CMake配置界面,手动指定EMACS路径,添加参数-DEMACS=/usr/bin/emacs
    2. 调整KDevelop的环境变量配置,确保/usr/bin被包含在PATH中

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 01:14:55