Can the C preprocessor be used to tell if a file exists?

后端 未结 9 1738
眼角桃花
眼角桃花 2020-11-30 22:21

I have a very large codebase (read: thousands of modules) that has code shared across numerous projects that all run on different operating systems with different C++ compil

9条回答
  •  醉话见心
    2020-11-30 22:45

    Generally this is done by using a script that tries running the preprocessor on an attempt at including the file. Depending on if the preprocessor returns an error, the script updates a generated .h file with an appropriate #define (or #undef). In bash, the script might look vaguely like this:

    cat > .test.h <<'EOM'
    #include 
    EOM
    if gcc -E .test.h
     then
      echo '#define HAVE_ASDF_H 1' >> config.h
     else 
      echo '#ifdef HAVE_ASDF_H' >> config.h
      echo '# undef HAVE_ASDF_H' >> config.h
      echo '#endif' >> config.h
     fi
    

    A pretty thorough framework for portably working with portability checks like this (as well as thousands others) is autoconf.

提交回复
热议问题