5

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.

user258961
  • 53
  • 4
  • You have the file name and the list of include directories. Why not just check if the file is in any of them? – Nikolai Fetissov Mar 01 '10 at 02:59
  • 1
    @Nikolai: one reason for not just doing that is that the 'headline' header may itself include other files, and it is harder to check whether those files it depends on are present by checking whether they're all present than it is to try the compilation. – Jonathan Leffler Mar 01 '10 at 03:36
  • @Jonathan: I understand that. Of course it totally depends on the scope of the project, but I'd do a recursive search for `include` directives. – Nikolai Fetissov Mar 01 '10 at 03:47
  • 2
    @Nikolai: beware guarded includes - sometimes the header has a #include line that is never active on the local platform. Working out whether that header is included requires a C preprocessor. Yes, it can be done; it is probably easier just to ask the C preprocessor to do it. – Jonathan Leffler Mar 01 '10 at 04:00
  • @Nikolai: The big problem with that is that you do not always know where the compiler will look for files, especially system or third-party libraries. Especially on Unix, its not unusual to have several compilers, or different versions of the same compiler installed. Or to have libraries installed in non-standard places. – KeithB Mar 01 '10 at 14:19
  • possible duplicate of [Can the C preprocessor be used to tell if a file exists?](http://stackoverflow.com/questions/142877/can-the-c-preprocessor-be-used-to-tell-if-a-file-exists) – Ciro Santilli OurBigBook.com Aug 02 '15 at 08:36

3 Answers3

6

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.

Richard Pennington
  • 19,673
  • 4
  • 43
  • 72
  • Agreed; As Jonathan Leffler notes, this is how Autoconf does it, which is at least a reasonably strong vote of experience in its favor. – Brooks Moses Mar 01 '10 at 05:12
5

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
-2

Using the following program ,you can find the existence of the header file.

#include<stdio.h>
main()
{
        FILE * file;
        if ((file = fopen("/usr/include/stdio.h", "r"))!=NULL)
        {
                fclose(file);
                printf("true");
        }
        perror("err");

}
rekha_sri
  • 2,677
  • 1
  • 24
  • 27
  • 1
    Critique? Fixed name for the file you're searching for. Fixed location for the file you're looking for. Using the file you are looking for when compiling the program you are using to look for it. Printing status on stdout when OK and on stderr when not. Not returning different exit statuses for success and failure. Using archaic C definition of main - C99 requires 'int main()' or 'int main(void)', so this must be C89 or older code. Unfortunately, only C99 guarantees a zero exit status from main() when you don't put in an explicit return, so the exit status is undefined. – Jonathan Leffler Mar 02 '10 at 15:12