C++ のコメントを C ソース・ファイルで認識させるには、 このオプションを使用します。
>>- -q--+-nocpluscmt-+----------------------------------------->< '-cpluscmt---'
デフォルト設定はさまざまです。
__C99_CPLUSCMT コンパイラー・マクロは、cpluscmt が選択されたときに定義されます。
文字シーケンス // は、ヘッダー名、文字定数、ストリング・リテラル、またはコメント内を除き、 C++ のコメントを開始します。コメントはネストしません。また、マクロ置き換えは、コメント内では実行されません。 以下の文字シーケンスは C++ コメント内では無視されます。
C++ のコメントの形式は、//text です。文字シーケンスの 2 つのスラッシュ (//) はその間に何も入らないで隣接している必要があります。スラッシュの右側から、改行文字で示された論理ソース行の終わりまでが、すべてコメントとして扱われます。 // 区切り文字は、 行内の任意の位置に置くことができます。
// コメントは C89 の一部ではありません。 -qcpluscmt を指定すると、以下の有効な C89 プログラムの結果が誤りになります。
main() { int i = 2; printf("%i¥n", i //* 2 */ + 1); }
正しい答えは 2 (2 / 1) です。 -qcpluscmt を指定すると、 結果は 3 (2 + 1) になります。
プリプロセッサーは、以下の方法ですべてのコメントを処理します。
円記号 (¥) を使用して複数の物理ソース行が 1 つの論理ソース行に結合されている場合、コメントが複数の物理ソース行にわたることがあります。3 文字表記 (??/) によって円記号を表すことができます。
以下の例は、C++ のコメントの使用を示したものです。
// A comment that spans two ¥ physical source lines // A comment that spans two ??/ physical source lines
以下のソース・コード・フラグメントの場合:
int a; int b; // A comment that spans two ¥ physical source lines int c; // This is a C++ comment int d;
-P オプションの出力は、以下の通りです。
int a; int b; int c; int d;
-P -C オプションの C89 モード出力は以下の通りです。
int a; int b; // A comment that spans two physical source lines int c; // This is a C++ comment int d;
-E オプションの出力は、以下の通りです。
int a; int b; int c; int d;
-E -C オプションの C89 モード出力は以下の通りです。
#line 1 "fred.c" int a; int b; // a comment that spans two ¥ physical source lines int c; // This is a C++ comment int d;
-P -C オプションまたは -E -C オプションの拡張モード出力は以下の通りです。
int a; int b; // A comment that spans two ¥ physical source lines int c; // This is a C++ comment int d;
以下のソース・コード・フラグメントの場合:
int a; #define mm 1 // This is a C++ comment on which spans two ¥ physical source lines int b; // This is a C++ comment int c;
-P オプションの出力は、以下の通りです。
int a; int b; int c;
-P -C オプションの出力は以下の通りです。
int a; int b; // This is a C++ comment int c;
-E オプションの出力は、以下の通りです。
#line 1 "fred.c" int a; #line 4 int b; int c;
-E -C オプションの出力は以下の通りです。
#line 1 "fred.c" int a; #line 4 int b; // This is a C++ comment int c;
以下のソース・コード・フラグメントの場合:
#define mm(aa) aa int a; int b; mm(// This is a C++ comment int blah); int c; // This is a C++ comment int d;
-P オプションの出力は、以下の通りです。
int a; int b; int blah; int c; int d;
-P -C オプションの出力は以下の通りです。
int a; int b; int blah; int c; // This is a C++ comment int d;
-E オプションの出力は、以下の通りです。
#line 1 "fred.c" int a; int b; int blah; int c; int d;
-E -C オプションの出力は以下の通りです。
#line 1 "fred.c" int a; int b; int blah; int c; // This is a C++ comment int d;
C++ のコメントがコメントとして認識されるように myprogram.c をコンパイルするには、以下のように入力します。
xlc myprogram.c -qcpluscmt
関連情報