我正在使用rapidjson,它是一個全部頭文件庫。在rapidjson.h
,有一個宏觀RAPIDJSON_ASSERT
,在我的cpp文件之一,我想重新定義它,所以我有我的文件的頂部驗證碼:是否可以將宏定義應用於單個cpp文件?
#include "stdafx.h" // for windows
#pragma push_macro("RAPIDJSON_ASSERT")
#define RAPIDJSON_ASSERT(x) if(!(x)) throw std::logic_error("rapidjson exception");
#include "rapidjson/rapidjson.h"
#include "rapidjson/document.h"
#include "rapidjson/stringbuffer.h"
#include "rapidjson/writer.h"
....
....
#pragma pop_macro("RAPIDJSON_ASSERT")
這裏是whay是rapidjson.h
定義RAPIDJSON_ASSERT
:
#ifndef RAPIDJSON_ASSERT
#include <cassert>
#define RAPIDJSON_ASSERT(x) assert(x)
#endif // RAPIDJSON_ASSERT
的文檔指出覆蓋RAPIDJSON_ASSERT
邏輯,你只需要定義RAPIDJSON_ASSERT
您有任何文件之前。
問題是,當我在調試器中運行代碼時,RAPIDJSON_ASSERT
未被重新定義。我檢查了stdafx.h
以查找包含rapidjson頭文件的任何內容,但沒有任何內容。
我假設每個編譯單元都應該運行頭文件。
請注意,如果將宏的重新定義轉換爲stdafx.h
,我會重新定義宏,但我希望能夠按編譯單元完成此操作。
你在stdafx.h中包括rapidjson? – jaggedSpire
模式看起來不對。您是否想要更改此翻譯單元的rapidjson中的宏,或者只是在翻譯單元內?如果是後者,請在rapidjson頭文件後重新定義。否則,rapidjson可能會簡單地重新定義宏本身 – KABoissonneault
@jaggedSpire - 我沒有在stdafx.h中包含rapidjson.h,所以我不確定爲什麼它不會覆蓋宏。 – bpeikes