簡體   English   中英

有什么方法可以從.hpp 文件中檢查相應的.cpp 文件中是否使用了 C stdio 函數?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM