pretty
pretty

Reputation: 67

In compilation time how to find the macro is defined in which header file

Lets take the below code , If the macro HAVE_SYS_SELECT_H is defined then they included the header file. I need to know where the macro is ? That is in which which header file the macro is defined.

Is there any option for that while compiling the source code ?

Is there is a way to find the header file ? Also I want to know whether that macro is defined or not while compiling

#ifdef HAVE_SYS_SELECT_H
    #include <sys/select.h>
#endif

Upvotes: 1

Views: 1373

Answers (2)

pretty
pretty

Reputation: 67

#warning is used to check that macro is defined or not. In functions we can use printf to check whether it is enabled or not. But some macros in headers, we cannot use printf. So we can use #warning or else we can use #error

     #ifdef HAVE_SYS_SELECT_H
     #warning "defined" 
         #include <sys/select.h>
     #endif

Upvotes: 1

user6169399
user6169399

Reputation:

from your question title the only answer is compiler or Verbose compilation (using -v), and it is compiler dependent and you have to read your compiler manual.
but there are static code analysis tools out there to help before compilation, consider this sample code:
"mac.h" file:

#define TEST 1
#define TEST_FILE __FILE__

"mac.c" file:

#include <stdio.h>
#include <limits.h>
#include <stdint.h>

#include "mac.h"
#define DEBUG 1

int main()
{ 
#ifdef DEBUG
    printf("%s",TEST_FILE); // C:\tmp\mac.c
#endif
} 

preprocessor removes include directive in C and so output of this sample code is the path to the "mac.c" file e.g.("C:\tmp\mac.c").
so i think there is only one solution to your question:
using your editor static code analysis capability or tools like:
http://clang-analyzer.llvm.org/

and see:
How can I quickly search all included header files in a project for a specific function or macro?
How to quickly identify functions from header files?
How expensive it is for the compiler to process an include-guarded header?

i hope this helps.

Upvotes: 1

Related Questions