16

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

Cygwin 1.7.22 で gcc 4.7.3 を実行する Eclipse CDT 8.1

Cygwin 1.7.22 上の cmake 2.8.9 を使用して、デモ テストを含む gTest 1.6 が正常にビルドされました

ビルドした lib をフル パス 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));
}
4

2 に答える 2

11

一部の関数は、ANSI 標準を超えています。これらは、std=c++11 (または std=c++0x) を使用すると無効になります。

その中にはfdopenfilenoとがありstrdupます。それらを使用するには、次の 2 つの可能性があります。

Suse Linux Enterprise 11、MinGW、Cygwin の両方でテストしました。


追加:非 ANSI シンボルにアクセスする別の (おそらくより良い) 方法は、追加することです

#define _POSIX_C_SOURCE 200809L

ファイルの最初の #include の前。これにより、ほとんどの非標準ルーチンにアクセスできます。

一部の機能 (例: realpath(...)) が必要です

#define _BSD_SOURCE

ファイルの上に挿入します。

于 2015-10-05T15:37:24.653 に答える