在预处理器中对十六进制值进行字符串化 [英] stringizing a hex value in the preprocessor

查看:65
本文介绍了在预处理器中对十六进制值进行字符串化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的小程序。它只是尝试通过记录的预处理器

宏来确定构建它的编译器是什么样的b $ b版本并打印出结果。当编译器是gcc时它工作正常,

因为__VERSION__是作为字符串提供的。但识别Sun

编译器更难,因为它提供__SUNPRO_C作为数值

(在我的情况下等于0x580)。我尝试对其进行字符串化会产生错误:


%cc -g -o foo foo.c

" foo.c",第13行:无效源字符:''#''

" foo.c",第13行:语法错误之前或之后:0x580

cc:acomp失败了foo.c


有没有办法做到这一点?


谢谢,

HT


---------------------------------------------- ----------------

#include< stdio.h>


#if定义(__ GNUC__)

#define COMPILER_STRING" gcc" __VERSION__

#elif定义(__ SUNPRO_C)

#define COMPILER_STRING" SunStudio" #__SUNPRO_C

#else

#define COMPILER_STRING" unknown compiler / version"

#endif


int main(无效)

{

printf("编译为%s \ n",COMPILER_STRING);

返回0 ;

}

Consider the little program below. It simply attempts to determine what
version of what compiler it was built with via documented preprocessor
macros and print out the result. When the compiler is gcc it works fine,
because __VERSION__ is supplied as a string. But identifying the Sun
compiler is harder because it provides __SUNPRO_C as a numerical value
(equal in my case to 0x580). My attempt to stringify it produces an error:

% cc -g -o foo foo.c
"foo.c", line 13: invalid source character: ''#''
"foo.c", line 13: syntax error before or at: 0x580
cc: acomp failed for foo.c

Is there a way to do this?

Thanks,
HT

--------------------------------------------------------------
#include <stdio.h>

#if defined(__GNUC__)
#define COMPILER_STRING "gcc " __VERSION__
#elif defined(__SUNPRO_C)
#define COMPILER_STRING "SunStudio " #__SUNPRO_C
#else
#define COMPILER_STRING "unknown compiler/version"
#endif

int main(void)
{
printf("Compiled with %s\n", COMPILER_STRING);
return 0;
}

推荐答案



Henry Townsend写道:

Henry Townsend wrote:
考虑下面的小程序。它只是试图通过记录的预处理器宏来确定它所构建的编译器的版本,并打印出结果。当编译器是gcc时它工作正常,
因为__VERSION__是作为字符串提供的。但识别Sun
编译器更难,因为它提供__SUNPRO_C作为数值
(在我的情况下等于0x580)。我尝试对其进行字符串化会产生错误:
Consider the little program below. It simply attempts to determine what
version of what compiler it was built with via documented preprocessor
macros and print out the result. When the compiler is gcc it works fine,
because __VERSION__ is supplied as a string. But identifying the Sun
compiler is harder because it provides __SUNPRO_C as a numerical value
(equal in my case to 0x580). My attempt to stringify it produces an error:




这是我的尝试:


#include< stdio。 h>


#define str(x)#x

#define xstr(x)str(x)

#define paste(x,y)x ## y


#define __SUNPRO_C 0x580

#if defined(__ IGNUC__)

#define COMPILER_STRING" gcc" __VERSION__

#elif定义(__ SUNPRO_C)

#define COMPILER_STRING xstr(粘贴(SunStudio,xstr(__ SUNPRO_C)))

#else

#define COMPILER_STRING" unknown compiler / version"

#endif


int main(无效)

{

printf("编译为%s \ n",COMPILER_STRING);

返回0;

}


这是用gcc -std = c99 -Wall编译的。



Here''s my try:

#include <stdio.h>

#define str(x) #x
#define xstr(x) str(x)
#define paste(x, y) x ## y

#define __SUNPRO_C 0x580
#if defined(__IGNUC__)
#define COMPILER_STRING "gcc " __VERSION__
#elif defined(__SUNPRO_C)
#define COMPILER_STRING xstr(paste(SunStudio, xstr(__SUNPRO_C)))
#else
#define COMPILER_STRING "unknown compiler/version"
#endif

int main(void)
{
printf("Compiled with %s\n", COMPILER_STRING);
return 0;
}

and this was compiled with gcc -std=c99 -Wall.




Suman写道:

Suman wrote:
Henry Townsend写道:
Henry Townsend wrote:
考虑下面的小程序。它只是试图通过记录的预处理器宏来确定它所构建的编译器的版本,并打印出结果。当编译器是gcc时它工作正常,
因为__VERSION__是作为字符串提供的。但识别Sun
编译器更难,因为它提供__SUNPRO_C作为数值
(在我的情况下等于0x580)。我尝试将其字符串化会产生错误:
Consider the little program below. It simply attempts to determine what
version of what compiler it was built with via documented preprocessor
macros and print out the result. When the compiler is gcc it works fine,
because __VERSION__ is supplied as a string. But identifying the Sun
compiler is harder because it provides __SUNPRO_C as a numerical value
(equal in my case to 0x580). My attempt to stringify it produces an error:



这是我的尝试:



Here''s my try:




a有点改进:
-------------------------------------->< --- -------------------------------------------------- -----------

#include< stdio.h>


#define str(x)#x

#define xstr(x)str(x)

#define paste(x,y)x ## y


#define __SUNPRO_C 0x580

#if定义(__ IGNUC__)

#define COMPILER_STRING" gcc" __VERSION__

#elif定义(__ SUNPRO_C)

#define COMPILER_STRING xstr(SunStudio xstr(__ SUNPRO_C))

#else

#define COMPILER_STRING" unknown compiler / version"

#endif


int main(void)

{

printf("编译为%s \ n",COMPILER_STRING);

返回0;

}

-------------------------------------->< -------- -------------------------------------------------- ------

gcc -std = c99 -Wall -pedantic -ansi。原谅可耻的

__IGNU__。

我没有Sun编译器可以玩:)


HTH



a tad improved:
--------------------------------------><----------------------------------------------------------------
#include <stdio.h>

#define str(x) #x
#define xstr(x) str(x)
#define paste(x, y) x ## y

#define __SUNPRO_C 0x580
#if defined(__IGNUC__)
#define COMPILER_STRING "gcc " __VERSION__
#elif defined(__SUNPRO_C)
#define COMPILER_STRING xstr(SunStudio xstr(__SUNPRO_C))
#else
#define COMPILER_STRING "unknown compiler/version"
#endif

int main(void)
{
printf("Compiled with %s\n", COMPILER_STRING);
return 0;
}
--------------------------------------><----------------------------------------------------------------
gcc -std=c99 -Wall -pedantic -ansi. And, excuse the ignominious
__IGNU__.
I don''t have a Sun compiler to play with :)

HTH


Suman写道:
这是我的尝试:



有点改进:
[snip]



a tad improved:
[snip]




是的,这很有效。实际上它打印:


使用SunStudio编译0x580


以及我使用该行进行的调整:


#define COMPILER_STRING" SunStudio" ## xstr(__ SUNPRO_C)


删除引号:


使用SunStudio编译0x580


这就是我想要的。谢谢!


HT



Yes, that works. Actually it prints:

Compiled with SunStudio "0x580"

and a tweak I made to use the line:

#define COMPILER_STRING "SunStudio " ## xstr(__SUNPRO_C)

removes the quotes to give:

Compiled with SunStudio 0x580

Which is what I was looking for. Thanks!

HT


这篇关于在预处理器中对十六进制值进行字符串化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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