简体   繁体   English

CMAKE_COMPILER_IS_GNUCXX和CMAKE_CXX_COMPILER_ID为空

[英]CMAKE_COMPILER_IS_GNUCXX and CMAKE_CXX_COMPILER_ID are empty

I am currently playing around with CMake and want to detect the compiler and the compiler version. 我目前正在玩CMake并希望检测编译器和编译器版本。 My current CMakeLists.txt looks as follows: 我目前的CMakeLists.txt如下所示:

cmake_minimum_required (VERSION 2.6)

set (PROJECT "a_tour_of_c++")
set (GNUCXX_MINIMUM_VERSION "4.8")
set (CXX_STANDARD "c++11")

message ("${CMAKE_CXX_COMPILER}")       # C:/dev/MinGW/bin/g++.exe
message ("${CMAKE_CXX_COMPILER_ID}")    # EMPTY
message ("${CMAKE_COMPILER_IS_GNUCXX}") # EMPTY

if (CMAKE_COMPILER_IS_GNUCXX)
  if (CMAKE_CXX_COMPILER_VERSION VERSION_LESS GNUCXX_MINIMUM_VERSION)
    message (FATAL_ERROR "GCC version must be at least ${GNUCXX_MINIMUM_VERSION}!")
  endif()
else()
  message (FATAL_ERROR "Invalid compiler!")
endif()

set (BUILD_DIR build)
set (HEADER_DIR include)
set (SOURCE_DIR src)
set (MAIN_FILE ${SOURCE_DIR}/main.cc)

set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=${CXX_STANDARD}")
project ($(PROJECT))
include_directories (${HEADER_DIR})
add_executable (${BUILD_DIR}/${PROJECT} ${MAIN_FILE})

The file is fine and I can sucessfully clean, build and run my cli application with this file (if I comment out the else -branch of the compiler check). 文件很好,我可以用这个文件成功地清理,构建和运行我的cli应用程序(如果我注释掉编译器检查的else -branch)。

The problem is, that both CMAKE_CXX_COMPILER_ID and CMAKE_COMPILER_IS_GNUCXX are empty ( CMAKE_CXX_COMPILER is not). 问题是, CMAKE_CXX_COMPILER_IDCMAKE_COMPILER_IS_GNUCXX都是空的( CMAKE_CXX_COMPILER不是)。

I am using a Microsoft Windows 8 (6.3.9600) operating system and MinGW , more detailed the following tools. 我使用的是Microsoft Windows 8(6.3.9600)操作系统和MinGW ,更详细的以下工具。

使用的工具版本

As you can in the screenshot, I use NetBeans IDE to simplify the whole build progress. 正如您在屏幕截图中所做的那样,我使用NetBeans IDE来简化整个构建过程。

So my questions are: 所以我的问题是:

  1. What are possible reasons, that the mentioned variables are empty? 可能的原因是,上述变量是空的?
  2. Does any workaround exist for my problem? 我的问题是否存在任何解决方法?

And please let me know, if you see any other problems with my (simple) CMakeLists.txt file. 如果您发现我的(简单) CMakeLists.txt文件有任何其他问题,请告诉我。

Place it after project command: project命令之后放置它:

> cat CMakeLists.txt 
cmake_minimum_required(VERSION 2.8)

message("before:")
message(">    ${CMAKE_CXX_COMPILER}")
message(">    ${CMAKE_CXX_COMPILER_ID}")
message(">    ${CMAKE_COMPILER_IS_GNUCXX}")
message("-----------------")

project(Foo)

message("after:")
message(">    ${CMAKE_CXX_COMPILER}")
message(">    ${CMAKE_CXX_COMPILER_ID}")
message(">    ${CMAKE_COMPILER_IS_GNUCXX}")
message("-----------------")

> cmake -H. -B_builds
before:
>    
>    
>    
-----------------
-- The C compiler identification is GNU 4.8.1
-- The CXX compiler identification is GNU 4.8.1
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
after:
>    /usr/bin/c++
>    GNU
>    1
-----------------
-- Configuring done
-- Generating done
-- Build files have been written to: /.../_builds

It might help you tell CMake that this is a C++ project. 可能会帮助你告诉CMake这是一个C ++项目。 To do it you say it in the project command: 要做到这一点,你在project命令中说:

project ($(PROJECT) CXX)
#                   ^^^
# Note project type

You might also want to do it before checking the variable. 您可能还想在检查变量之前执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM