【问题标题】:cmake CMAKE_C_COMPILER "is not a full path to an existing compiler tool"cmake CMAKE_C_COMPILER“不是现有编译器工具的完整路径”
【发布时间】:2017-09-01 16:59:38
【问题描述】:

这是 cmake 相关问题 尝试构建项目,并且在构建机器上默认 clang-3.5 存在一些问题,因此在那里安装了 clang-3.7。不幸的是它没有 clang 符号链接,所以我不得不找到它。

CMakeLists.txt 文件中包含这些行来检测并设置它(我知道这不是很好看的查找代码)

# Complilers, NOTE: this section should be before the Project section
find_program( CLANG_PATH clang )
find_program( CLANGCXX_PATH clang++ )
if(NOT CLANG_PATH AND NOT CLANGCXX_PATH)
    set (CLANG_SEARCH_PATH  "/usr/bin/")
    execute_process(COMMAND bash "-c" "ls ${CLANG_SEARCH_PATH} | grep -v clang++ | grep clang | head -1"
        OUTPUT_VARIABLE CLANG_FILE )
    execute_process(COMMAND bash "-c" "ls ${CLANG_SEARCH_PATH} | grep clang++ | head -1"
        OUTPUT_VARIABLE CLANGCXX_FILE )
    if(CLANG_FILE AND CLANGCXX_FILE)
        set(CLANG_PATH          "${CLANG_SEARCH_PATH}${CLANG_FILE}")
        set(CLANGCXX_PATH       "${CLANG_SEARCH_PATH}${CLANGCXX_FILE}")
        set(CMAKE_C_COMPILER    "${CLANG_PATH}")
        message(STATUS "The clang compiler discovered... ${CLANG_PATH}")
        set(CMAKE_CXX_COMPILER  "${CLANGCXX_PATH}")
        message(STATUS "The clang++ compiler discovered... ${CLANGCXX_PATH}")
    else()
        message(FATAL_ERROR "clang and clang++ were not found! Aborting...")
    endif()
endif()

编译结果是(clang++也是一样)

-- The clang compiler discovered... /usr/bin/clang-3.7
-- The clang++ compiler discovered... /usr/bin/clang++-3.7

-- The C compiler identification is unknown
-- The CXX compiler identification is unknown
CMake Error at CMakeLists.txt:24 (project):
  The CMAKE_C_COMPILER:

    /usr/bin/clang-3.7

  is not a full path to an existing compiler tool.

  Tell CMake where to find the compiler by setting either the environment
  variable "CC" or the CMake cache entry CMAKE_C_COMPILER to the full path to
  the compiler, or to the compiler name if it is in the PATH.

但路径似乎是正确的。 如果我只是以一种虚拟的方式设置它,比如

set(CMAKE_C_COMPILER    "/usr/bin/clang-3.7")
set(CMAKE_CXX_COMPILER  "/usr/bin/clang++-3.7")

有效

-- The C compiler identification is Clang 3.7.0
-- The CXX compiler identification is Clang 3.7.0
-- Check for working C compiler: /usr/bin/clang-3.7
-- Check for working C compiler: /usr/bin/clang-3.7 -- works

PS:我看到了这个CMAKE_C_COMPILER is not a full path to an existing compiler tool 但是它不是很有帮助。不过保留了相同的主题名称。

更新:

$cmake --version
cmake version 3.6.2

【问题讨论】:

  • 编译器路径末尾是否没有超出字符,例如空格或换行符?尝试将结果变量与预期值进行比较,例如 if(CMAKE_C_COMPILER STREQUAL "/usr/bin/clang-3.7")
  • @Tsyvarev 让我感到羞耻(。很好,这种行为的原因是 new line。恰好是我的 execute_process 命令的产物。刚刚使用了 string(STRIP ${CLANG_FILE} CLANG_FILE)。希望 cmake 自己做。如果你愿意写答案,我很乐意接受它是正确的。谢谢

标签: c++ cmake


【解决方案1】:

当根据execute_process 的输出形成某些变量的内容时,请注意大多数shell 实用程序和程序在其输出中添加换行符(这样做是为了在终端中查看漂亮的视图)。

要删除这个换行符,可以使用 CMake 命令string(STRIP)

【讨论】:

    【解决方案2】:

    如果您导出编译器路径,您可以解决这些错误。 例如:导出 PATH=$PATH:/

    请注意,大多数人在编译应用程序时都犯了错误。 在用户模式下设置的环境变量,但是,在编译应用程序时,用户可能会保留 sudo(它不起作用,因为当您传递 sudo 时,它会将用户终端更改为 sudo 用户。所以无论您在用户模式下设置的环境变量是范围。因此,如果您想在 sudo 中编译应用程序,则直接进入 sudo 模式,然后导出环境变量它应该可以工作)。

    【讨论】:

      猜你喜欢
      • 2017-12-21
      • 1970-01-01
      • 1970-01-01
      • 2018-01-20
      • 1970-01-01
      • 2011-09-09
      • 1970-01-01
      • 2014-09-26
      • 2013-07-17
      相关资源
      最近更新 更多