简体   繁体   中英

What's meaning of this code in C?

In some Bison code, what does the following line mean?

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

I know #define command but I don't understand the whole command.

It means that YY_DECL will be expanded to

extern "C" int yylex();

This is actually C++, not 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.

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 and C++ can be mixed in a single program, but it requires such declarations for function to nicely work together.

There's probably an #ifdef __cplusplus around this #define ; that's a special macro used to indicate compilation by a C++ compiler.

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

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

#define - a preprocessor directive declaring a new variable for the preprocessor. But you know that.

YY_DECL - the name of the variable.

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. If you include this into declaration, it allows you to use C in C++. EDIT: The code actually not need to be pure C, but it will be linked as such. But the most common usage pattern is to make a C code compatible with C++. Thanks @larsmans for the correction.

int yylex() - a declaration of a function named yylex with undefined number of parameters and return type int

So the whole command assigns a C function declaration to a preprocessor variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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