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

问题描述

限时删除!!

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

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

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

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



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

难以比这更清楚。


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 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

and several other clauses carry a footnote that reads in part

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:

Its hard to get more clear than that.

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

1403页,肝出来的..

09-06 10:17