cmake检查编译器是否存在

cmake检查编译器是否存在,cmake,Cmake,我正在尝试使用cmake构建一个小的C++代码 我还没有g++ (我正在virtualbox操作系统上测试) 当我调用cmake. 我收到了令人讨厌的错误信息 -- The C compiler identification is GNU 4.7.2 **-- The CXX compiler identification is unknown** -- Check for working C compiler: /usr/bin/gcc -- Check for working C co

我正在尝试使用
cmake
构建一个小的C++代码

我还没有
g++
(我正在virtualbox操作系统上测试)

当我调用
cmake.
我收到了令人讨厌的错误信息

-- The C compiler identification is GNU 4.7.2

**-- The CXX compiler identification is unknown**

-- Check for working C compiler: /usr/bin/gcc

-- Check for working C compiler: /usr/bin/gcc -- works

-- Detecting C compiler ABI info

-- Detecting C compiler ABI info - done

**CMake Error: your CXX compiler: "CMAKE_CXX_COMPILER-NOTFOUND" was not found.   Please set CMAKE_CXX_COMPILER to a valid compiler path or name.

-- Configuring incomplete, errors occurred!**
基本上,这是可以的。它说发生了错误,但说的太多了。我只想得到一条精确而简洁的消息,说“g++未安装,请安装”


有没有办法先检查是否安装了
g++
,然后给出相应的消息

您应该使用GCC(Gnu编译器集合)前端。您应该安装gcc-c++或类似的软件包。

您提供的输出表明CMake试图对您有所帮助。如果它对于您的口味来说过于冗长,那么减少它的最简单方法可能是将它捕获到一个变量中,然后检查它

您可以将下面的示例CMake脚本保存为
detect\u cxx\u compiler.CMake
,并使用
CMake-p detect\u cxx\u compiler.CMake
调用该脚本。代码的编写方式旨在帮助CMake初学者,而不是为了小规模或处理效率

cmake_minimum_required(VERSION 2.8.5 FATAL_ERROR)
cmake_policy(VERSION 2.8.5)

# This cmake script (when saved as detect_cxx_compiler.cmake) is invoked by:
#
#     cmake -P detect_cxx_compiler.cmake
#
# It is written for clarity, not brevity.

# First make a new directory, so that we don't mess up the current one.
execute_process(
    COMMAND ${CMAKE_COMMAND} -E make_directory detection_area
    WORKING_DIRECTORY .
)

# Here, we generate a key file that CMake needs.
execute_process(
    COMMAND ${CMAKE_COMMAND} -E touch CMakeLists.txt
    WORKING_DIRECTORY detection_area
)

# Have CMake check the basic configuration.  The output is
# actually in the form that you posted in your question, but
# instead of displaying it onscreen, we save it to a variable
# so that we can select only parts of it to print later.
execute_process(
    COMMAND ${CMAKE_COMMAND} --check-system-vars
    OUTPUT_VARIABLE the_output
    OUTPUT_STRIP_TRAILING_WHITESPACE
    WORKING_DIRECTORY detection_area
)

# Eliminate the directory, including all of the files within it that
# CMake created.
execute_process(
    COMMAND ${CMAKE_COMMAND} -E remove_directory detection_area
    WORKING_DIRECTORY .
)

# Here, you have the entire message captured as a variable.
# Uncomment this next line to convince yourself of this.
#message(STATUS "the_output = |${the_output}|.")

# Here, we search the message to see if the C++ compiler was found or not,
# and print an arbitrary message accordingly.
string(FIND "${the_output}" "CMAKE_CXX_COMPILER-NOTFOUND" scan_result)
#message(STATUS "scan_result = |${scan_result}|.")
if(NOT(-1 EQUAL "${scan_result}"))
    message(FATAL_ERROR "A C++ compiler was not detected.")
endif()

message(STATUS "A C++ compiler was detected.")

请显示您得到的错误,以便我们可以帮助,所以您不想修复这个错误,不想删除项目中的C++支持,您只想更改错误消息?我认为错误信息非常准确(:是的,但在这个阶段我不想。目标是首先检查它的存在。