使用C预处理器确定编译环境 [英] Using C Preprocessor to Determine Compilation Environment

查看:123
本文介绍了使用C预处理器确定编译环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个由 C 编写的Windows驱动程序和C ++中的用户模式可执行文件组成的应用程序。它们都使用共享头文件来定义几个宏,常量,枚举等。在C ++版本中,我想将所有内容都包含在命名空间中,这是 C 编译器不支持的特性。是否有某些变量,我可以检查使用Visual Studio像预处理指令一样,像下面的例子?

  #ifdef USING_CPLUSPLUS 
namespace mynamespace {
#endif

struct mystruct {
...
};


#ifdef USING_CPLUSPLUS
}
#endif


__ cplusplus 是由C ++标准定义的,并且由于保留名称规则的副作用一个在标准C中这样说的子句,永远不会被C编译器预定义。



每个编译器都有一组预定义的宏,这些宏有时用于区分编译主机和目标平台。这种宏的一个大存储库是 Predef Project ,它们收集有关编译器,主机,

编辑 C:C标准始终保留以两个下划线或一个下划线开始,后跟大写字母的标识符。例如,第7.1.3节中的C99表示


以下划线开头,大写字母或下划线的所有标识符总是保留供任何使用。,


和其他几个子句带有一个脚注,用于读取


实现定义的关键字应具有为7.1.3中所述的任何用途保留的标识符形式。


所以,没有标准的任何进一步指示,在C编译器的兼容实现中,名称 __ cplusplus 是为任何用途保留的。该名称由C ++的实现者选择,特别是因为它在C中保留,具有清楚的含义,并且不知道已被任何C实现定义。



然而,C99标准委员会已经决定确保没有C实现损坏了一个宏的显而易见的实用程序,可以用来区分C编译和C ++汇编。在C99第6.10.8节中,它们写为:


实现不应预定义宏_ _cplusplus,也不应定义
在任何标准标题中。


难以比这更清楚。


I am building an application that consists of both a windows driver written in C and a user mode executable in C++. They both use a shared header file to define several macros, constants, enums, etc. In the C++ version, I want to include everything within a namespace, which a feature not supported by the C compiler. Is there certain variable I can check for to use as a preprocessor directive with Visual Studio like in the following example?

#ifdef USING_CPLUSPLUS
namespace mynamespace{
#endif

    struct mystruct{
       ...
    };


#ifdef USING_CPLUSPLUS
}
#endif

解决方案

For the specific example of distinguishing a C++ compiler from a C compiler, the sensible choice is the macro __cplusplus which is defined by the C++ standard to exist, and due to side effects of the reserved name rules a clause that says so in standard C, will never be predefined by the C compiler.

Every compiler has a collection of predefined macros that are occasionally useful for distinguishing among compilation hosts and target platforms. One large repository of such macros is the Predef Project where they are collecting reports on as many combinations of compiler, host, and target as they can.

Edit: Clarifying the reserved nature of __cplusplus in C: The C standard has always reserved identifiers that begin with two underscores or one underscore followed by a capital letter. For instance, C99 in section 7.1.3 says

"All identifiers that begin with an underscore and either an uppercase letter or another underscore are always reserved for any use.",

and several other clauses carry a footnote that reads in part

"Implementation-defined keywords shall have the form of an identifier reserved for any use as described in 7.1.3."

So, without any further direction from the standard, in a compliant implementation of a C compiler the name __cplusplus is reserved for any use. That name was chosen by the implementors of C++ specifically because it was reserved in C, has a clear meaning, and was not known to have been defined by any C implementation.

However, by C99 the standards committee had decided to make sure that no C implementation damaged the obvious utility of a macro that could be used to distinguish a C compilation from a C++ compilation. In C99 section 6.10.8 they write:

"The implementation shall not predefine the macro _ _cplusplus, nor shall it define it in any standard header."

Its hard to get more clear than that.

这篇关于使用C预处理器确定编译环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆