简体   繁体   English

C中这段代码的含义是什么?

[英]What's meaning of this code in C?

In some Bison code, what does the following line mean? 在一些Bison代码中,以下行是什么意思?

#define YY_DECL extern "C" int yylex();

I know #define command but I don't understand the whole command. 我知道#define命令,但我不明白整个命令。

It means that YY_DECL will be expanded to 这意味着YY_DECL将扩展到

extern "C" int yylex();

This is actually C++, not C; 这实际上是C ++,而不是C; when you compile this file with a C++ compiler, it declares that the function yylex must be compiled with "C linkage", so that C functions can call it without trouble. 当您使用C ++编译器编译此文件时,它声明必须使用“C linkage”编译函数yylex ,以便C函数可以毫无问题地调用它。

If you don't program in C++, this is largely irrelevant to you, but you may encounter similar declarations in C header files for libraries that try to be compatible with C++. 如果你不用C ++编程,这在很大程度上与你无关,但你可能会在C头文件中遇到类似的声明,试图与C ++兼容的库。 C and C++ can be mixed in a single program, but it requires such declarations for function to nicely work together. C和C ++可以在一个程序中混合使用,但它需要这样的函数声明才能很好地协同工作。

There's probably an #ifdef __cplusplus around this #define ; 这个#define周围可能有一个#ifdef __cplusplus ; that's a special macro used to indicate compilation by a C++ compiler. 这是一个特殊的宏,用于表示C ++编译器的编译。

#define YY_DECL extern "C" int yylex();

定义一个宏YY_DECL代表一个函数yylex的声明,它在C ++程序中具有'C'链接,不带参数并返回int

#define - a preprocessor directive declaring a new variable for the preprocessor. #define - 一个预处理器指令,为预处理器声明一个新变量。 But you know that. 但你知道。

YY_DECL - the name of the variable. YY_DECL - 变量的名称。

extern "C" - tells the compiler that the following code is pure C. There are a lot of differences between C and C++ and one cannot generally mix C and C++ code. extern "C" - 告诉编译器下面的代码是纯C的.C和C ++之间有很多区别,一般不能混合使用C和C ++代码。 If you include this into declaration, it allows you to use C in C++. 如果将此包含在声明中,则允许您在C ++中使用C. EDIT: The code actually not need to be pure C, but it will be linked as such. 编辑:代码实际上不需要是纯C,但它将被链接。 But the most common usage pattern is to make a C code compatible with C++. 但最常见的使用模式是使C代码与C ++兼容。 Thanks @larsmans for the correction. 谢谢@larsmans的纠正。

int yylex() - a declaration of a function named yylex with undefined number of parameters and return type int int yylex() - 一个名为yylex的函数的声明,带有未定义的参数个数,返回类型为int

So the whole command assigns a C function declaration to a preprocessor variable. 因此整个命令将C函数声明分配给预处理器变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM