简体   繁体   English

glibc源代码中的L_宏

[英]L_ macro in glibc source code

I was reading through the source code of glibc and I found that it has two macros which have the same name 我正在阅读glibc的源代码,我发现它有两个具有相同名称的宏
This one is on the line 105 这个是在105行
#define L_(Str) L##Str
and this on the line 130 这就在130号线上
#define L_(Str) Str

What do these macros really mean ? 这些宏到底意味着什么? The usage is only for comparing two characters 用法仅用于比较两个字符
For example on line 494, you could see it is used for comparing character values between *f and '$' 例如,在第494行,您可以看到它用于比较* f和'$'之间的字符值
if(*f == L_('$')) . if(*f == L_('$')) If we wanted to compare the two characters, we could have compared them directly, instead of directing them through a macro ? 如果我们想比较两个字符,我们可以直接比较它们,而不是通过宏指导它们? Also, what is the use case for the macro on line 105 ? 另外,第105行宏的用例是什么?

It prepends macro argument with L prefix (wchar_t literal - it uses as large datatype as is needed to represent every possible character code point instead of normal 8 bit in char type) if you're compiling wscanf version of function (line 105). 它假设具有L前缀的宏参数(wchar_t literal - 它使用表示每个可能的字符代码点而不是char类型中的正常8位所需的大数据类型)如果您正在编译函数的wscanf版本(第105行)。 Otherwise it just passes argument as it is (line 130). 否则它只是按原样传递参数(第130行)。

## is string concatenation operator in c preprocessor, L##'$' will expand to L'$' eventually. ##是c预处理器中的字符串连接运算符,L ##'$'最终将扩展为L'$'。

To sum up: it is used to compile two, mutually exclusive versions of vscanf function - one operating on wchar_t, one on char. 总结一下:它用于编译vscanf函数的两个互斥版本 - 一个在wchar_t上运行,一个在char上运行。

Check out this answer: What exactly is the L prefix in C++? 看看这个答案: C ++中的L前缀究竟是什么?

Let's read the code. 我们来看看代码。 (I have no idea what it does, but I can read code) (我不知道它做了什么,但我可以阅读代码)

First, why are there two defines as you point out? 首先,为什么你指出有两个定义? One of them is used when COMPILE_WSCANF is defined, the other is used otherwise. 其中一个在定义COMPILE_WSCANF时使用, COMPILE_WSCANF在其他情况下使用。 What is COMPILE_WSCANF ? 什么是COMPILE_WSCANF If we look further down the file, we can see that different functions are defined. 如果我们进一步查看文件,我们可以看到定义了不同的函数。 When COMPILE_WSCANF is defined, the function we end up with (through various macros) is vfwscanf otherwise we get vfscanf . 当定义COMPILE_WSCANF ,我们最终得到的函数(通过各种宏)是vfwscanf否则我们得到vfscanf This is a pretty good indication that this file might be used to compile two different functions one for normal characters, one for wide characters. 这是一个很好的指示,这个文件可能用于编译两个不同的函数,一个用于普通字符,一个用于宽字符。 Most likely, the build system compiles the file twice with different defines. 最有可能的是,构建系统使用不同的定义将文件编译两次。 This is done so that we don't have to write the same file twice since both the normal and wide character functions will be pretty similar. 这样做是为了使我们不必两次写同一个文件,因为普通和宽字符函数都非常相似。

I'm pretty sure that means that this macro has something to do with wide characters. 我很确定这意味着这个宏与宽字符有关。 If we look at how it's used, it is used to wrap character constants in comparisons and such. 如果我们看看它是如何使用的,它用于在比较等中包装字符常量。 When 'x' is a normal character constant, L'x' is a wide character constant ( wchar_t type) representing the same character. 'x'是正常字符常量时, L'x'是表示相同字符的宽字符常量( wchar_t类型)。

So the macro is used to wrap character constants inside the code so that we don't have to have #ifdef COMPILE_WSCANF . 所以宏用于在代码中包装字符常量,这样我们就不必拥有#ifdef COMPILE_WSCANF

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

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