2017-12-21 35 views
-1

入れ子関数がgccの拡張機能としてサポートされていますが、cファイルを使用してネストされた関数をエラーでコンパイルしました。 エラーのマッサージは、このようなものです:入れ子関数で使用されるcファイルをコンパイルするときにエラーが発生しました

test.cpp:6:40: error: function definition is not allowed here double

square (double z) { return z * z; } 

そして、ネストされた関数は、このようなものです:

foo (double a, double b) 
{ 
    double square (double z) { return z * z; } 
    return square (a) + square (b); 
} 

私のオペレーションシステムは、Mac OSであり、私はgcc -vを入力すると、バージョンは10.12.6

ですiTerm2の場合、応答は次のとおりです。

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1 
Apple LLVM version 9.0.0 (clang-900.0.38) 
Target: x86_64-apple-darwin16.7.0 
Thread model: posix 
InstalledDir: /Library/Developer/CommandLineTools/usr/bin 
+3

http://en.cppreference.com/w/c/language/function_definition: * "関数宣言とは異なり、関数定義はファイルスコープでのみ使用できます(入れ子関数はありません)。* – UnholySheep

+2

clangはgccが必要な入れ子関数をサポートしていません。 –

+3

Appleの「gcc」は 'clang'の単なるエイリアスです。 – molbdnilo

答えて

0

あなたは実際に私たちですgccではなくClangを使用しています。

お知らせこの

Apple LLVM version 9.0.0 (clang-900.0.38)

the clang websiteから、クランGCC対(GNUコンパイラコレクション)

GCC supports many language extensions, some of which are not implemented by Clang. For instance, in C mode, GCC supports nested functions and has an extension allowing VLAs in structs.

+0

Thx you all。私はちょうどcに連絡を開始しました。 – tinyfeng

関連する問題