çpreprocessor:串联INT字符串 [英] C Preprocessor: concatenate int to string

查看:166
本文介绍了çpreprocessor:串联INT字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出我怎么可以连接一个的#define 'D INT到的#define 使用的 C preprocessor'D弦。我的编译器是在CentOS 5 GCC 4.1的解决方案还应该MinGW的工作。

我想一个版本号追加到一个字符串,但我可以让它开始工作的唯一方法是使版本号的副本定义为字符串。

我能找到的是引用宏参数的方法,但它并不适用于工作最接近的#define 取值

这是行不通的。

 的#define MAJOR_VER 2
#定义MINOR_VER 6
#定义MY_FILE/home/user/.myapp#MAJOR_VER #MINOR_VER

它不没有工作取值要么是因为值为数字,这将扩大到/ home / user中/ .myapp 2 6 ,这是无效的 C

这的工作,但我不喜欢的版本副本定义,因为我需要他们的号码以及

 的#define MAJOR_VER 2
#定义MINOR_VER 6
的#define MAJOR_VER_STR2
#定义MINOR_VER_STR6
#定义MY_FILE/home/user/.myappMAJOR_VER_STRING MINOR_VER_STRING


解决方案

经典çpreprocessor问题....

 的#define STR_HELPER(X)#X
STR的#define(x)的STR_HELPER(X)#定义MAJOR_VER 2
#定义MINOR_VER 6
#定义MY_FILE/home/user/.myappSTR(MAJOR_VER)STR(MINOR_VER)

间接的额外水平将使preprocessor扩大宏它们转换为字符串之前。

I'm trying to figure out how I can concatenate a #define'd int to a #define'd string using the C Preprocessor. My compiler is GCC 4.1 on CentOS 5. The solution should also work for MinGW.

I'd like to append a version number onto a string, but the only way I can get it to work is to make a copy of the version number defines as strings.

The closest thing I could find was a method of quoting macro arguments, but it doesn't work for #defines

This is does not work.

#define MAJOR_VER 2
#define MINOR_VER 6
#define MY_FILE "/home/user/.myapp" #MAJOR_VER #MINOR_VER

It doesn't work without the #s either because the values are numbers and it would expand to "/home/user/.myapp" 2 6, which isn't valid C.

This does work, but I don't like having copies of the version defines because I do need them as numbers as well.

#define MAJOR_VER 2
#define MINOR_VER 6
#define MAJOR_VER_STR "2"
#define MINOR_VER_STR "6"
#define MY_FILE "/home/user/.myapp" MAJOR_VER_STRING MINOR_VER_STRING

解决方案

Classical C preprocessor question....

#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)

#define MAJOR_VER 2
#define MINOR_VER 6
#define MY_FILE "/home/user/.myapp" STR(MAJOR_VER) STR(MINOR_VER)

The extra level of indirection will allow the preprocessor to expand the macros before they are converted to strings.

这篇关于çpreprocessor:串联INT字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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