【发布时间】:2018-03-24 09:34:17
【问题描述】:
我正在尝试用 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 上
- 将 CMakeLists.txt 中的
g++替换为/usr/bin/g++,在运行cmake ..时会产生无限大的周期性输出
我的问题是:
- 为什么会出现这些错误?
- 是否存在解决方案,还是我真的需要忘记 CMAKE_CXX_COMPILER 并改用 CXX?
【问题讨论】:
-
为我工作。
set(CMAKE_CXX_COMPILER g++)是真实的还是您在问题中更新了它? -
@S.M.所有的选择都适合你吗?我确认我在这里写的正是我所做的。
-
我没有检查其他选择。如果“g++”将从另一个代码或函数返回并且包含行尾,则在大多数情况下会出现类似的错误。这将通过使用
string(STRIP, value, variable)来解决。
标签: c++ cmake googletest