gpt4 book ai didi

c++ - 使用 Google Test 编译程序时出现 "g++ is not a full path"

转载 作者:行者123 更新时间:2023-11-30 05:03:18 25 4
gpt4 key购买 nike

我正在尝试使用 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 ..

我得到了以下 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.

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

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

我的问题是:

  • 为什么会出现这些错误?
  • 是否存在解决方案,或者我真的需要忘掉 CMAKE_CXX_COMPILER 而改用 CXX?

最佳答案

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

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

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

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

关于c++ - 使用 Google Test 编译程序时出现 "g++ is not a full path",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49463212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com