【问题标题】:GTest - isatty not declared in this scopeGTest - isatty 未在此范围内声明
【发布时间】:2018-03-05 17:44:22
【问题描述】:

我花了几个小时试图弄清楚为什么会出现以下编译错误:

 ~/src/example/build $ make
-- Downloading GMock / GTest version 1.8.0 from git
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/src/example/build
[  5%] Performing update step for 'gmock'
[ 10%] Performing configure step for 'gmock'
-- gmock configure command succeeded.  See also /home/user/src/example/build/gmock/src/gmock-stamp/gmock-configure-*.log
[ 15%] Performing build step for 'gmock'
-- gmock build command succeeded.  See also /home/user/src/example/build/gmock/src/gmock-stamp/gmock-build-*.log
[ 20%] No install step for 'gmock'
[ 25%] Completed 'gmock'
[ 40%] Built target gmock
[ 75%] Built target project-app
Scanning dependencies of target step_definition_runner
[ 80%] Building CXX object Target/project/CMakeFiles/step_definition_runner.dir/features/step_definitions/App_Steps.cpp.o
In file included from /home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-internal.h:40:0,
                 from /home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/gtest.h:58,
                 from /home/user/src/example/Target/project/features/step_definitions/App_Steps.cpp:1:
/home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-port.h: In function ‘int testing::internal::posix::IsATTY(int)’:
/home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-port.h:2341:45: error: ‘isatty’ was not declared in this scope
 inline int IsATTY(int fd) { return isatty(fd); }
                                             ^
/home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-port.h: In function ‘int testing::internal::posix::RmDir(const char*)’:
/home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-port.h:2347:53: error: ‘rmdir’ was not declared in this scope
 inline int RmDir(const char* dir) { return rmdir(dir); }
                                                     ^
/home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-port.h: In function ‘int testing::internal::posix::ChDir(const char*)’:
/home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-port.h:2365:53: error: ‘chdir’ was not declared in this scope
 inline int ChDir(const char* dir) { return chdir(dir); }
                                                     ^
/home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-port.h: In function ‘int testing::internal::posix::Read(int, void*, unsigned int)’:
/home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-port.h:2379:46: error: ‘read’ was not declared in this scope
   return static_cast<int>(read(fd, buf, count));
                                              ^
/home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-port.h: In function ‘int testing::internal::posix::Write(int, const void*, unsigned int)’:
/home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-port.h:2382:47: error: ‘write’ was not declared in this scope
   return static_cast<int>(write(fd, buf, count));
                                               ^
/home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-port.h: In function ‘int testing::internal::posix::Close(int)’:
/home/user/src/example/build/gmock/src/gmock/googletest/include/gtest/internal/gtest-port.h:2384:43: error: ‘close’ was not declared in this scope
 inline int Close(int fd) { return close(fd); }
                                           ^
Target/project/CMakeFiles/step_definition_runner.dir/build.make:62: recipe for target 'Target/project/CMakeFiles/step_definition_runner.dir/features/step_definitions/App_Steps.cpp.o' failed
make[2]: *** [Target/project/CMakeFiles/step_definition_runner.dir/features/step_definitions/App_Steps.cpp.o] Error 1
CMakeFiles/Makefile2:164: recipe for target 'Target/project/CMakeFiles/step_definition_runner.dir/all' failed
make[1]: *** [Target/project/CMakeFiles/step_definition_runner.dir/all] Error 2
Makefile:94: recipe for target 'all' failed
make: *** [all] Error 2

我正在使用 cucumber-cpp 项目中的 FindGMock.cmake 来处理 GTest 依赖项。

在我的 CMakeLists.txt 文件中,我有:

add_executable(step_definition_runner
    features/step_definitions/App_Steps.cpp
    )
target_include_directories(step_definition_runner
    PRIVATE
        ${GTEST_INCLUDE_DIRS}
    )
target_link_libraries(step_definition_runner project-app ${GTEST_LIBRARIES})

我尝试了很多东西,但有几点需要注意:

  • 在我包含 gtest/gtest.h 之前包含 unistd.h
  • 强制在 gtest/gtest.hgtest/internal/gtest-port.h 中包含 unistd.h

我有什么明显的遗漏吗?

【问题讨论】:

  • 是 Linux 操作系统吗?
  • 是的,至少现在是这样。我最终会努力让它在 Windows 上运行。

标签: c++ cmake googletest gmock


【解决方案1】:

您好像不在 Windows 操作系统上。

这些功能仅适用于窗口

在以下内容中查找 https://github.com/google/googletest/blob/master/googletest/include/gtest/internal/gtest-port.h

402 // Brings in definitions for functions used in the testing::internal::posix 
403 // namespace (read, write, close, chdir, isatty, stat). We do not currently 
404 // use them on Windows Mobile. 
405 #if GTEST_OS_WINDOWS 
406 # if !GTEST_OS_WINDOWS_MOBILE 
407 #  include <direct.h> 
408 #  include <io.h> 
409 # endif

【讨论】:

  • 我尝试的另一件事是在 gtest-port.h 中的#else 中添加一个#error "Testing to see if in this branch"(换句话说,在非Windows 条件下),只是想看看它是否在那个文件中包含了 unistd.h,并且它在 Linux 上成功出错了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-20
  • 2016-08-09
  • 2019-02-17
  • 2021-01-07
  • 2016-10-27
  • 2016-06-03
相关资源
最近更新 更多