GTest - isatty未在此范围内声明

时间:2018-03-05 17:44:23

标签: c++ cmake googletest gmock

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

 ~/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.h gtest / internal / gtest-port.h
  • 中包含 unistd.h

有什么明显的东西我不见了吗?

1 个答案:

答案 0 :(得分:1)

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

这些功能仅适用于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