如何使#if 预处理器以另一个不同文件中的值为条件?

How to make #if preprocessor be conditional upon a values in another different file?

例如,如果 "settings" 文件存在并且具有以下内容:

system,ubuntu

是否可以使用预处理器 #if 根据设置文件中的值自定义代码?例如:

#if lookup_system == "ubuntu" // loopup_system is pseudocode

另一个用途可能是查看生产服务器上的代码是否 运行。

你不能那样做。

但是,您可以从外部数据生成一些 header 文件autoconf 实用程序会执行此操作(实际上会生成一些 configure 脚本来生成这样的 header)。

更一般地说,您应该为您的编译器设置 build automation system (e.g. edit your Makefile for GNU make, or your build.ninja for ninja) to generate that header file suitably (perhaps with a tiny script, your own C++ -another- program, or whatever). You can also have a clever build system passing specific -D flags

记住 C++ 文件(header 文件,例如 .hh 和实现文件,例如 .cc ...)可以由其他东西生成。这是常见的做法。

详细了解您的 preprocessor, e.g. about cpp. It is a compile only thing. With GCC you can get the preprocessed form foo.ii of your foo.cc file with a command such as
g++ -C -E foo.cc > foo.ii