C++ GoogleTest 1.6与Cygwin 1.7编译错误:';文件编号';未在此范围中声明

C++ GoogleTest 1.6与Cygwin 1.7编译错误:';文件编号';未在此范围中声明,c++,eclipse,cygwin,googletest,C++,Eclipse,Cygwin,Googletest,带有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

带有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上运行GCC4.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++标准设置为-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)与此错误无关。

某些函数超出了ANSI标准。 当您使用std=c++11(或std=c++0x)时,这些选项将被禁用

其中包括
fdopen
fileno
strdup
。 使用它们有两种可能性:

  • 使用GNU方言(std=GNU++11)
  • 如果希望编译时不使用方言并生成本地异常,则可以在stdio.h中包含未定义的uu STRICT_uansi_u。(见:)
我已经在Suse Linux Enterprise 11、MinGW和Cygwin上测试了这两个版本


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

#define _POSIX_C_SOURCE 200809L
在第一个之前#包括在您的文件中。这将使您能够访问大多数非标准例程

某些函数(例如,
realpath(…)
)需要


插入到您的文件顶部。

我必须说我还尝试了
GTEST_OS_CYGWIN
符号(结果相同,即没有)。这可能是因为c++0x不包括posix。这也可以解决QNX 6.6 Momentics 5.0的类似问题。
#define _BSD_SOURCE