cmake检查编译器是否存在 [英] cmake check existence of compiler

查看:209
本文介绍了cmake检查编译器是否存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to play around with cmake to build a small C++-code.

我愿意还没有 g ++
(我正在VirtualBox操作系统上进行测试)

I do not have yet g++ (I'm testing on a virtualbox OS)

当我打电话 cmake。
我收到了讨厌的错误消息。

When I call cmake . I get the nasty error messages.

-- 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 ++ ist。请安装它。

Basically, this is OK. It says errors occurred, but it says too much than needed. I just want to get a precise and concise message saying "g++ ist not installed. INSTALL it please".

有没有办法先检查 g ++ 已安装,然后给出适当的消息?

Is there a way to first check if g++ is installed and then give an appropriate message?

推荐答案

您提供的输出表明CMake试图为您提供帮助。如果您觉得它太冗长,也许减少它的最简单方法就是将其捕获到一个变量中,然后对其进行检查。

The output you gave shows that CMake attempting to be helpful to you. If it is too verbose for your taste, perhaps the simplest way to reduce it would be to capture it into a variable, then examine it.

您可以保存示例CMake下面的脚本为 detect_cxx_compiler.cmake ,然后使用 cmake -P detect_cxx_compiler.cmake 调用脚本。编写代码的目的是为了对CMake初学者有所帮助,而不是为了小尺寸或提高处理效率。

You can save the sample CMake script below as detect_cxx_compiler.cmake, and invoke the script using cmake -P detect_cxx_compiler.cmake. The code is written in a manner intended to be helpful to CMake beginners, not for small size or processing efficiency.

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.")

这篇关于cmake检查编译器是否存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆