如何根據包含 .h 檔案的 .cpp 排除 .h 檔案中的某些 #include 陳述句?
例子:
main.cpp 檔案
<tell the header.h file that main.cpp is including it>
#include "header.h"
其他.cpp檔案
<tell the header.h file that other.cpp is including it>
#include "header.h"
header.h 檔案
<if called by main.cpp>
#include "some_file_which_fails_when_used_with_OTHER_CPP.h"
<end if>
uj5u.com熱心網友回復:
執行此操作的典型方法是#define在包含標頭之前將源檔案預期為宏:
// the_header.h
#ifndef MY_HEADER_H_INCLUDED
#define MY_HEADER_H_INCLUDED
#ifdef MY_PROJECT_MAIN
// ...
#else
// ...
#endif
#endif
在使用標題的“默認”行為的檔案中
// some_code.cpp
// Just use the header
#include "the_header.h"
在使用標頭激活行為的檔案中:
// main.cpp
// Make sure this is BEFORE the #include
#define MY_PROJECT_MAIN
#include "the_header.h"
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/329585.html
上一篇:在Elasticsearch聚合中做自定義映射的最佳實踐
下一篇:未檢測到溫度檔案的變化
