1

在 C++ 中,可以在编译时检查具有给定名称的文件是否在包含路径中。

我正在尝试设置一个库,该库从用户创建的文件(例如 config.h)中获取编译器设置。如果文件不存在,则库将默认为可工作状态。

我想做这样的事情

#IFINCLUDED config.h
#  include config.h
#else
#    define defaultLibrarySettings
#endif

我想一个解决方法是做类似的事情:

#include "config.h
#ifndef defaultLibrarySettings
#   define defaultLibrarySettings
#endif

但是,用户仍然需要该路径中某处的“config.h”文件,否则他们将获得未解决的包含。

最后的想法是做类似的事情

#ifdef USE_EXTERNAL_SETTINGS
#    include "config.h"
#else
#    define defaultLibrarySettings
#endif

然而,这需要用户在每次包含我的库时#define USE_EXTERNAL_SETTINGS。

TLDR:有没有一种简单的方法来检查包含路径中是否存在文件,例如我的第一个示例中所示?

4

2 回答 2

1

您可以使用默认库设置包含并填写 config.h,并允许用户更改它们。这样,用户也可以看到允许的配置

于 2013-06-14T17:24:08.510 回答
0

Since this is part of a library, you probalby have a makefile anyway, so you could handle it there with shell scripting.

Something like this:

 echo "#include \"config.h\"" > /tmp/tst.c
 rm 2>/dev/null /tmp/ok
 gcc -c tst.c -o tst.o 2>/dev/null && touch /tmp/ok
 if [ ! -f /tmp/ok ]; then echo "create default config here"; fi
于 2013-06-14T17:31:26.787 回答