1

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
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
pretty
  • 67
  • 2
  • 10
  • you can use ctags. – Haris Jun 17 '16 at 08:40
  • This is not a real answer, except perhaps a bit: most `HAVE_XXX` will come from `config.h`, auto-generated by autoconfig. – unwind Jun 17 '16 at 08:42
  • Search all header files in your project folder. Most likely it comes in an auto-generated header file, which means the header file might not exist yet, and the header file with its macro will be created by a `configure` script (or similar). – Some programmer dude Jun 17 '16 at 08:46
  • Also can you please tell us *why* you "need to know where the macro is"? What is the *actual* problem you are trying to solve? [Related reading about the XY problem](http://xyproblem.info/). – Some programmer dude Jun 17 '16 at 08:46
  • @Jaochim Then how do i know whether this macro is already defined or not ? Also i can't use printf statement to check this macro is defined or not because before main function only they have declared the headers. – pretty Jun 17 '16 at 08:51
  • I assume that the problem is that you need to use the [`select`](http://man7.org/linux/man-pages/man2/select.2.html) function, in a POSIX environment? Then it's not actually certain that the function is declared in the `` header file, or that the file even exists. If you re targeting another operating system (for example Windows) then it's even less likely that the header file exists. Just trust the autoconfig system that it will find the correct header where `select` is declared, otherwise you need to contact the author and tell them about it. – Some programmer dude Jun 17 '16 at 08:57
  • Okay, As unwind said that may be in configuration file. Now i want to know what are the macros that are already defined while compiling the code. I refer man gcc but .... – pretty Jun 17 '16 at 08:58

2 Answers2

1

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.

Community
  • 1
  • 1
1

#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
pretty
  • 67
  • 2
  • 10