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

后端 未结 9 1720
眼角桃花
眼角桃花 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 23:01

    So far as I know cpp does not have a directive regarding the existence of a file.

    You might be able to accomplish this with a bit of help from the Makefile, if you're using the same make across platforms. You can detect the presence of a file in the Makefile:

    foo.o: foo.c
        if [ -f header1.h ]; then CFLAGS+=-DHEADER1_INC
    

    As @Greg Hewgill mentions, you can then make your #includes be conditional:

    #ifdef HEADER1_INC
    #include 
    #endif
    

提交回复
热议问题