2016-02-28 9 views
8

https://msdn.microsoft.com/en-us/library/ew2hz0yd.aspx)は、私は、次を参照:例えばの#ifと#ifdefのよう条件エリアにファイルの境界が含まれていますか? MSDNで

すべての条件付きコンパイルディレクティブ、 ファイルの末尾の前にディレクティブ#endifの閉鎖と一致しなければなりません。 エラーメッセージが生成されます。条件付きコンパイル ディレクティブがインクルードファイルに含まれている場合は、同じ の条件を満たす必要があります。インクルードファイルの末尾に適合しない条件付きコンパイル ディレクティブが存在していない必要があります。

まあ、シンプルで明確です。同じことをC++ 11標準では見つけられません。私の質問はこの法的な制限ですか?

私は、いくつかの#include層にわたる条件付きコンパイルの分割は良い考えではないので、避けてください。

他のコンパイラ(GCC、CLANG)がこのケースをどのように処理しているか知っていますか?多分、これはどこかで議論されたでしょうか?

+0

あなたは明らかにC++標準について尋ねるので、Cタグを追加しないでください。 – Olaf

+0

これを行うと何が起こったのですか? – Alex

+4

これはCにも関係します。それは、何か違うかもしれない異なる標準を持っています。 –

答えて

2
#if FOO 
#include "hashif.h" 


extern "C" int printf(const char* fmt, ...); 

int main() 
{ 
    printf("Hello, World\n"); 
} 

hashif.hこれが含まれています

#define BAR 1 
#else 
#define BAR 2 
#endif 

その後、打ち鳴らす++エラーが発生します。

hashif.cpp:1:2: error: unterminated conditional directive 
#if FOO 
^ 
1 error generated. 

編集:そしてg++cppすべて同じように振る舞います。

正確な出力から:

$ cpp hashif.cpp -DFOO=1 
# 1 "hashif.cpp" 
# 1 "<built-in>" 
# 1 "<command-line>" 
# 1 "/usr/include/stdc-predef.h" 1 3 4 
# 1 "<command-line>" 2 
# 1 "hashif.cpp" 

# 1 "hashif.h" 1 
In file included from hashif.cpp:2:0: 
hashif.h:2:2: error: #else without #if 
#else 
^
hashif.h:3:0: warning: "BAR" redefined 
#define BAR 2 
^ 
hashif.h:1:0: note: this is the location of the previous definition 
#define BAR 1 
^ 
hashif.h:4:2: error: #endif without #if 
#endif 
^
# 3 "hashif.cpp" 2 


extern "C" int printf(const char* fmt, ...); 

int main() 
{ 
    printf("Hello, World\n"); 
hashif.cpp:1:0: error: unterminated #if 
#if FOO 
^ 
} 
+2

あなたはFOOを定義しましたか? – Anedar

+0

@Aernar:問題ではありません(ただし、 '#else'と'#endif'には 'FOO'が定義されているといくつかのエラーがあります) –

+0

これを追加すると良いでしょうあなたの答えに誤りがあります。 –

関連する問題