简体   繁体   中英

“g++ is not a full path” when compiling a program using Google Test

I am trying to compile with g++ a program that uses Google Test as a submodule. Here is what I tried:

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

Then, I created a CMakeLists.txt containing:

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)

And a file named test.cpp containing:

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

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

Then, I ran the commands:

mkdir build
cd build
cmake ..

And I got the following fatal error:

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.

Yet, when I run which g++ , I get /usr/bin/g++ which means that the compiler name is in the PATH and that CMake should find it. I even tried the following:

  • Removing Google Test and replacing test.cpp with an empty main, which perfectly works and means that the problem is about Google Test
  • Replacing in CMakeLists.txt g++ with /usr/bin/g++ , which produces an infinitely large periodic output when running cmake ..

My questions are:

  • Why am I getting those errors?
  • Does there exist a solution or do I really need to forget about CMAKE_CXX_COMPILER and use CXX instead?

I followed your steps. We indeed get an infinite loop, and the following output:

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++

Changing the set in CMakeLists.txt file in the following way appears to fix this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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