は私のコードです:マクロ制御DEBUG
macro1.h
#ifndef NO_DEBUG
#define DEBUG(arg) cout<<endl<<arg<<endl
#else
#define DEBUG(arg)
#endif
macro1.cpp
#include <iostream>
#include "macro1.h"
using namespace std;
int main()
{
cout<<endl<<"start"<<endl;
DEBUG("debug line 1");
#undef NO_DEBUG
DEBUG("debug line 2");
#define NO_DEBUG
DEBUG("debug line 3");
cout<<endl<<"end"<<endl;
return 0;
}
私はこのようにそれを実行/コンパイル:
コンパイル+実行1:
$ g++ macro1.cpp
$ ./a.out
start
debug line 1
debug line 2
debug line 3
end
$
コンパイル+実行2:
$ g++ macro1.cpp -DNO_DEBUG
$ ./a.out
start
end
$
しかし、それはNO_DEBUGが、それは印刷に持っていた定義されていないので、私は、最初の実行で を期待したものではありません。セカンドランで
start
debug line 1
debug line 2
end
、我々コマンドラインでマクロを定義しているので、印刷する必要がありました。
start
debug line 2
end
何が起こっているのか教えてください ここに?
私は前処理機能のみを使用していますので、正しく機能するはずです。
'#include" macro1.h "のポイントで定義されたマクロだけが問題です。 –
しかし、その.hファイルの内容は、.cppファイルの中に貼り付けられたコピーでもかまいません。また、.cppの前処理はまだ必要です。だからなぜ '#include" macro1.h "'が問題になるのでしょうか? – Sumit
プリプロセッサはあなたが思っているよりもダンベルです。インクルードを行い、上から下にファイルを移動します。あなたの '.cpp'の状態を' #include "macro1.h" 'の後ろで調べると、何が起きているのかがわかります。 –