使用Google Test编译程序时,“g ++不是一个完整的路径”

时间:2018-03-24 09:34:17

标签: c++ cmake googletest

我正在尝试用g ++编译一个使用Google Test作为子模块的程序。这是我试过的:

git init testgoogletest
cd testgoogletest
git submodule add https://github.com/google/googletest.git

然后,我创建了一个包含:

的CMakeLists.txt
cmake_minimum_required (VERSION 3.5.1)
project(try_googletest)

set(CMAKE_CXX_COMPILER g++)

set(VARIABLE_INCLUDE_DIR "${gtest_SOURCE_DIR}/include" "${gmock_SOURCE_DIR}/include")
add_subdirectory(../googletest gtest)

target_include_directories(gmock_main SYSTEM BEFORE INTERFACE
    "${gtest_SOURCE_DIR}/include" "${gmock_SOURCE_DIR}/include")

add_executable(my_executable test.cpp)
target_link_libraries(my_executable PRIVATE gmock_main)

一个名为test.cpp的文件包含:

#include "gtest/gtest.h"
#include "gmock/gmock.h"

TEST(SimpleTest, works) {
    EXPECT_TRUE(true);
}

然后,我运行了命令:

mkdir build
cd build
cmake ..

我收到了以下致命错误:

CMake Error at (personal path)/googletest/CMakeLists.txt:7 (project):
  The CMAKE_CXX_COMPILER:

    g++

  is not a full path and was not found in the PATH.

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

然而,当我运行which g++时,我得到/usr/bin/g++这意味着编译器名称在PATH中并且CMake应该找到它。我甚至尝试了以下方法:

  • 删除Google Test并将test.cpp替换为空的main,这完全有效并且意味着问题是关于Google Test
  • 使用g++替换CMakeLists.txt /usr/bin/g++,这会在运行cmake ..
  • 时产生无限大的周期性输出

我的问题是:

  • 为什么我会收到这些错误?
  • 是否存在解决方案或者我是否真的需要忘记CMAKE_CXX_COMPILER而是使用CXX?

1 个答案:

答案 0 :(得分:0)

我按照你的步骤。我们确实得到了一个无限循环,以及以下输出:

You have changed variables that require your cache to be deleted.
Configure will be re-run and you may have to reset some variables.
The following variables have changed:
CMAKE_CXX_COMPILER= /usr/bin/c++

以下列方式更改set文件中的CMakeLists.txt似乎可以解决此问题:

set(CMAKE_CXX_COMPILER /usr/bin/c++)