How do you include a header file that may or may not exist?

前端 未结 2 1484
天命终不由人
天命终不由人 2021-01-18 04:31

Let\'s assume I define BAR in foo.h. But foo.h might not exist. How do I include it, without the compiler complaining at me?

#include \"foo.h\"

#ifndef BA         


        
2条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-18 05:14

    Use a tool like GNU Autoconf, that's what it's designed for. (On windows, you may prefer to use CMake).

    So in your configure.ac, you'd have a line like:

    AC_CHECK_HEADERS([foo.h])
    

    Which, after running configure, would define HAVE_FOO_H, which you can test like this:

    #ifdef HAVE_FOO_H
    #include "foo.h"
    #else
    #define BAR 1
    #endif
    

    If you intend to go down the autotools route (that is autoconf and automake, because they work well together), I suggest you start with this excellent tutorial.

提交回复
热议问题