Is there any way to check, from a .hpp file, if C stdio functions are used in the corresponding .cpp file?

I have the following question. Supposing I have an header file header.hpp which is include in a test.cpp file. Is it possible to add instructions to the header.hpp file in order to check (maybe at compile time) if some C stdio functions are used in the test.cpp file and in positive case do something specific? For example something like:

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

>Solution :

No, this is not possible. Neither C++, nor C, work like this, on a fundamental level.

An #include is logically equivalent to physically inserting the contents of the included file into the including file. Doing a cut and paste of your header.hpp into the beginning of your test.cpp replacing the #include accomplishes exactly the same thing.

The resulting code gets compiled from beginning to the end, in order.

When reading the header file, the C++ compiler has no knowledge, whatsoever, of something it hasn’t read yet. An #include stops reading the file that it’s in, and the included file gets read and compiled, before proceeding.

Leave a Reply