简体   繁体   English

跨平台访问C常量

[英]Cross-platform access to C constants

Is there a cross-platform (ie, across Linux, BSD, & OS X, ideally all of POSIX) I can get programmatic access to constants like O_RDWR , based purely on the string "O_RDWR" ? 是否有一个跨平台(即跨Linux,BSD,和OS X,最好所有的POSIX的),我能得到像常量的编程访问O_RDWR ,上串纯粹基于"O_RDWR" I am writing some (non-C) code that interoperates with C functions across platforms and needs to call, eg, open(2) with the correct parameters - but I have no guarantee as far as what the local system sets its constants to. 我正在编写一些(非C)代码,这些代码可跨平台与C函数进行互操作,并且需要使用正确的参数进行调用,例如open(2),但是我不能保证本地系统将其常量设置为什么。

I'm essentially looking for something like get_constant_value("O_RDWR") -> "02" . 我本质上是在寻找类似get_constant_value("O_RDWR") -> "02"

EDIT: I currently have a really nasty hack: directly invoke the preprocessor and examine the output. 编辑:我目前有一个非常讨厌的黑客:直接调用预处理器并检查输出。

printf "#include <fcntl.h>\nO_RDWR" | cpp | tail -n 1

No, there is no such thing as of today. 不,今天没有这样的事情。 In C# this is called reflection. 在C#中,这称为反射。 There are rumors that C++ committee discusses reflection for C++14. 有传言称C ++委员会讨论对C ++ 14的反思。 Once it will be adopted, well in 1-3 yeas you can expect this will be supported in compilers. 一旦被采用,可以在1-3年内得到预期,编译器将支持它。

If the number of constants that you need is limited, you may want to write your custom code that will do the translation. 如果所需常量的数量有限,则可能需要编写自定义代码来执行转换。

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

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