简体   繁体   中英

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" ? 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.

I'm essentially looking for something like 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. There are rumors that C++ committee discusses reflection for C++14. Once it will be adopted, well in 1-3 yeas you can expect this will be supported in compilers.

If the number of constants that you need is limited, you may want to write your custom code that will do the translation.

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