Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to programatically check for existence of header file?

Tags:

c++

c

configure

Is it just to test compile a simple program, with that header file #included in it?

To better understand the compilation process I'm writing my own "configure", which tests for the existence of a few header and library files.

like image 994
user258961 Avatar asked Mar 01 '10 02:03

user258961


2 Answers

Yes, use the compiler to compile your simple test program. That's the best and easiest way to see if the compiler can find the header. If you hard code #include search paths you'll always have to modify and adapt for different compilers.

like image 81
Richard Pennington Avatar answered Sep 29 '22 00:09

Richard Pennington


The GNU Autoconf suite checks for headers by running test compilations. Just testing for the existence of a file 'filename.h' is fairly simple:

#include <filename.h>
int main(void){return 0;}

You might prefer quotes instead of angle brackets.

like image 44
Jonathan Leffler Avatar answered Sep 29 '22 01:09

Jonathan Leffler