【问题标题】:GoogleTest 1.6 with Cygwin 1.7 compile error: 'fileno' was not declared in this scope带有 Cygwin 1.7 的 GoogleTest 1.6 编译错误:“fileno”未在此范围内声明
【发布时间】:2013-09-13 10:29:52
【问题描述】:

带有 Cygwin 1.7 的 GoogleTest 1.6:“fileno”未在此范围内声明

在 Eclipse CDT 中对 Factorial() 函数进行简单测试时出现错误消息:

Invoking: Cygwin C++ Compiler
g++ -std=c++0x -DGTEST_OS_CYGWIN=1 -I"E:\source\gtest-1.6.0\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/challenge.d" -MT"src/challenge.d" -o "src/challenge.o" "../src/challenge.cpp"
In file included from E:\source\gtest-1.6.0\include/gtest/internal/gtest-internal.h:40:0,
                 from E:\source\gtest-1.6.0\include/gtest/gtest.h:57,
                 from ../src/challenge.cpp:11:
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h: In function 'int testing::internal::posix::FileNo(FILE*)':
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1589:51: error: 'fileno' was not declared in this scope
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1595:57: error: 'strdup' was not declared in this scope
E:\source\gtest-1.6.0\include/gtest/internal/gtest-port.h:1627:71: error: 'fdopen' was not declared in this scope

Eclipse CDT 8.1 在 Cygwin 1.7.22 上运行 gcc 4.7.3

gTest 1.6 成功构建,包括演示测试,在 Cygwin 1.7.22 上使用 cmake 2.8.9

我已将构建的库与完整路径 E:\lib\gtest-1.6.0\Cygwin\libgtest.a 链接起来

以下命令选项是手动添加的,没有它会出现同样的错误。

-DGTEST_OS_CYGWIN=1

似乎错误与我的代码无关。有人在 Eclipse 和 Cygwin 中使用 gTest 吗?

谢谢,

unsigned long Factorial(unsigned n) {
    return n==0? 0 : n*Factorial(n-1);
}

// Tests factorial of 0.
TEST(FactorialTest, HandlesZeroInput) {
  EXPECT_EQ(1, Factorial(0));
}

// Tests factorial of positive numbers.
TEST(FactorialTest, HandlesPositiveInput) {
  EXPECT_EQ(1, Factorial(1));
  EXPECT_EQ(2, Factorial(2));
  EXPECT_EQ(6, Factorial(3));
  EXPECT_EQ(40320, Factorial(8));
}

【问题讨论】:

    标签: c++ eclipse cygwin googletest


    【解决方案1】:

    将 C++ 标准设置为 -std=gnu++0x 而不是 -std=c++0x,对我有用。你可以试试这个说法:

    g++ -std=gnu++0x -DGTEST_OS_CYGWIN=1 -I"E:\source\gtest-1.6.0\include" -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/challenge.d" -MT"src/challenge.d" -o "src/challenge.o" "../src/challenge.cpp"
    

    设置符号(-DGTEST_OS_CYGWIN=1)与此错误无关。

    【讨论】:

    • 我必须说我也尝试了GTEST_OS_CYGWIN 符号(结果相同,即什么都没有)。可能这是因为 c++0x 不包含 posix。
    • 这也可以解决 QNX 6.6 Momentics 5.0 的类似问题。
    【解决方案2】:

    某些功能超出了 ANSI 标准。 这些在您使用 std=c++11(或 std=c++0x)时被禁用。

    其中有fdopenfilenostrdup。 有两种使用方式:

    我已经在 Suse Linux Enterprise 11、MinGW 和 Cygwin 上进行了测试。


    添加:访问非 ANSI 符号的另一种(可能更好)方法是添加

    #define _POSIX_C_SOURCE 200809L
    

    文件中第一个#include 之前。这将使您可以访问大多数非标准例程。

    某些功能(例如realpath(...))需要

    #define _BSD_SOURCE
    

    插入到您的文件顶部。

    【讨论】:

      猜你喜欢
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 2013-03-05
      • 2011-10-08
      • 2011-01-31
      • 1970-01-01
      • 1970-01-01
      • 2021-05-13
      相关资源
      最近更新 更多