c-preprocessor

Utility of macros for enum

房东的猫 提交于 2019-12-05 00:27:57
One header socket.h on my Linux system looks like the following. /* Bits in the FLAGS argument to `send', `recv', et al. */ enum { MSG_OOB = 0x01, /* Process out-of-band data. */ #define MSG_OOB MSG_OOB MSG_PEEK = 0x02, /* Peek at incoming messages. */ #define MSG_PEEK MSG_PEEK MSG_DONTROUTE = 0x04, /* Don't use local routing. */ #define MSG_DONTROUTE MSG_DONTROUTE ... Defining an enum is sort of an idiom for creating type-safe-ish constants in C that the language actually treats as compile-time constants. My question is : what purpose does the definition of macros MSG_OOB , MSG_PEEK , … that

How do I use C preprocessor macros with Rust's FFI?

社会主义新天地 提交于 2019-12-05 00:24:18
I'm writing some code that interfaces an existing library written in C. In my Rust code I'd like to be able to use values from CPP macros. If I have a C include.h that looks like this: #define INIT_FLAG 0x00000001 I'd like to be able to use it in Rust like this: #[link(name="mylib")] extern { pub static init_flag: c_int = INIT_FLAG; } I've looked at other FFI code and I see a lot of people duplicating these values in Rust instead of getting them from the FFI. This seems a little brittle, and I'd also like to be able to handle more complicated things that are defined via CPP macros. Running cpp

Behavior of __LINE__ in inline functions

主宰稳场 提交于 2019-12-04 23:10:55
I have a macro that passes the line number and file name to an error handler: #define SYSTEM_FAILURE (error_code, comment) \ System_Failure((error_code), (comment), __LINE__, __FILE__); How will the __LINE__ be resolved when used inside an inlined function? file.h: inline int divide(int x, int y) { if (y == 0) { SYSTEM_FAILURE(ENUM_DIVIDE_BY_ZERO, "divide by zero error"); } return x/y; } Will __LINE__ contain the line number within the header file, or the line number of the source file where the inline function is called (assuming compiler does a "paste" in the source code)? In C and C++,

In C++11 what should happen first: raw string expansion or macros?

99封情书 提交于 2019-12-04 22:12:41
This code works in Visual C++ 2013 but not in gcc/clang: #if 0 R"foo( #else int dostuff () { return 23; } // )foo"; #endif dostuff(); Visual C++ removes the if 0 first. Clang expands the R raw string first (and never defining dostuff). Who is right and why? [Update: Adrian McCarthy comments below saying MSVC++ 2017 fixes this] GCC and clang are right, VC++ is wrong. 2.2 Phases of translation [lex.phases]: [...] The source file is decomposed into preprocessing tokens (2.5) and sequences of white-space characters (including comments). Preprocessing directives are executed, [...] And 2.5

Macro-function to generate macro with prefix (avoid stringification)

筅森魡賤 提交于 2019-12-04 22:04:17
Background I have a project where I have two separate products with near-identical macro names, for which I would like to create a macro-like-function to retrieve the values of the macros quickly. I have written a getTranslation macro-function to take the literal text provided to the "function", which should be treated as a string and a string prefix (shown below). Question How can I accomplish this operation of taking the arguments supplied to the macro, concatenating them together (with an underscore in the middle), and treating that result as a preprocessor macro instead of a string? Code

Define Array in C

十年热恋 提交于 2019-12-04 19:23:51
问题 I have several 450 element character arrays (storing bitmap data to display on lcd screens.) I would like to put them under a header file and #define them, but I keep getting compilation errors. How would I do this in C ? #define numbers[450] {0, 1,etc...} #define numbers {0, 1, etc...} #define numbers[450] then set the numbers later and many more... 回答1: Well... you certainly don't need to use a define. Just add them into the header as const, static arrays. /* prevents multiple, redundant

Multiple inclusion in multiple files

ε祈祈猫儿з 提交于 2019-12-04 19:13:40
I am making a small game. In BattleRecord.h: #ifndef _CHARACTER_H_ #define _CHARACTER_H_ #include "Character.h" #endif class BattleRecord { public: Character Attacker; Character Defender; Status status; int DamageDealt; int GoldEarned; int ExpGained; }; In Character.h: #ifndef _EQUIPMENT_H_ #define _EQUIPMENT_H_ #include "Equipment.h" #endif class BattleRecord; class Character { BattleRecord AttackEnemy(Character &Enemy); } In BattleRecord.h: #ifndef _CHARACTER_H_ #define _CHARACTEr_H_ #include "Character.h" #endif #ifndef _BATLE_RECORD_H_ #define _BATLE_RECORD_H_ #include "BattleRecord.h"

Listing C Constants/Macros

你。 提交于 2019-12-04 19:10:12
问题 Is there a way to make the GNU C Preprocessor , cpp ( or some other tool ) list all available macros and their values at a given point in a C file? I'm looking for system-specific macros while porting a program that's already unix savvy and loading a sparse bunch of unix system files. Just wondering if there's an easier way than going hunting for definitions. 回答1: I don't know about a certain spot in a file, but using: $ touch emptyfile $ cpp -dM emptyfile Dumps all the default ones. Doing

How to force Visual Studio preprocessor case sensitivity with #includes?

孤街浪徒 提交于 2019-12-04 17:43:10
问题 If you have a header file named ThisIsAHeaderFile.h, the following will still locate the file in Visual Studio: #include <ThisIsAheaderFile.h> Is there a way to enforce case sensitivity so that the #include will result in an error? 回答1: You can't, because the Windows file system is itself case-insensitive. If you could get into a situation where you had both RICHIE.h and richie.h, it might make sense to control case sensitivity, but you can't. 回答2: It is (used to be?) possible to create files

Preprocessor directive #ifndef for C/C++ code

随声附和 提交于 2019-12-04 16:16:22
问题 In eclipse, whenever I create a new C++ class, or C header file, I get the following type of structure. Say I create header file example.h, I get this: /*Comments*/ #ifndef EXAMPLE_H_ #define EXAMPLE_H_ /* Place to put all of my definitions etc. */ #endif I think ifndef is saying that if EXAMPLE_H_ isn't defined, define it, which may be useful depending on what tool you are using to compile and link your project. However, I have two questions: Is this fairly common? I don't see it too often.