編譯c++源代碼時,首先預處理器會将源代碼進行預處理,預處理器執行源代碼中的預處理指令,下面是如何檢視cpp檔案經過預處理後的檔案。
首先有一個簡單的cpp檔案如下:
#include "StdAfx.h"
#include "TestMacro.h"
#include "iostream"
using namespace std;
TestMacro::TestMacro(void)
{
}
void testMacro_2()
{
#ifndef LANGUAGE
#define LANGUAGE 5
#define GERMEN 1
#define ENGLISH 2
#define FRENCH 3
cout << "LANGUAGE can be defined" << endl;
#else
cout << "LANGUAGE has been defined" << endl;
#endif
#if LANGUAGE == ENGLISH
#define Greeting "GoodMorning."
#elif LANGUAGE == GERMEN
#define Greeting "Guten Tag."
#elif LANGUAGE == FRENCH
#define Greeting "Bonjour."
#else
#define Greeting "Hi."
#endif
cout << Greeting << endl;
}
win7系統vs2010環境下,在開始->所有程式->msvs2010->Visual Studio Tools 下找到Visual Studio 指令提示(2010)并打開,将路徑跳到TestMacro.cpp檔案的儲存路徑下,再輸入指令cl -E TestMacro.cpp後按回車,控制台中輸出的就是經過預處理後的cpp檔案。
上面的檔案預處理後變為:
可以将預處理後的檔案重定向到文本檔案中,指令為:
cl -e TestMacro.cpp >stdout.txt
轉載于:https://www.cnblogs.com/chinayfhuang/p/3745490.html