[英]Is there any way to check, from a .hpp file, if C stdio functions are used in the corresponding .cpp file?
我有以下問題。 假設我有一個 header 文件header.hpp
包含在test.cpp
文件中。 是否可以在header.hpp
文件中添加指令,以檢查(可能在編譯時)是否在test.cpp
文件中使用了某些 C stdio 函數,並且在肯定的情況下做一些特定的事情? 例如:
header.hpp
:
#ifndef HEAD
#define HEAD
#include <iostream>
if ( C stdio functions are used in test.cpp ){
std::cout << "Hey" << "\n";
}
#endif
test.cpp
#include "header.hpp"
#include <cstdio>
int main(){
printf( "Hello\n" ); // C stdio function has been used.
}
Output:
Hello
Hey
不,這是不可能的。 從根本上講,C++ 和 C 都不是這樣工作的。
#include
在邏輯上等同於將包含文件的內容物理插入到包含文件中。 將test.cpp
剪切並粘貼到header.hpp
的開頭,替換#include
完成完全相同的事情。
生成的代碼按順序從頭到尾編譯。
在讀取 header 文件時,C++ 編譯器不知道它尚未讀取的內容。 #include
停止讀取它所在的文件,並在繼續之前讀取和編譯包含的文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.