简体   繁体   中英

Is it possible for C/C++ preprocessor to detect some compiler options?

I have a function with

static int include[MAX_NODE_NUMBER][MAX_NODE_NUMBER][MAX_NODE_NUMBER*MAX_NODE_NUMBER];
static bool calculated[MAX_NODE_NUMBER][MAX_NODE_NUMBER][MAX_NODE_NUMBER*MAX_NODE_NUMBER]={};

where MAX_NODE_NUMBER is 200. This can be compiled only when -mcmodel=large is in the gcc/g++ option. The two static variables are only to cache some data so they are not so necessary. Is it possible to code like

#if <some condition>
static int include[MAX_NODE_NUMBER][MAX_NODE_NUMBER][MAX_NODE_NUMBER*MAX_NODE_NUMBER];
static bool calculated[MAX_NODE_NUMBER][MAX_NODE_NUMBER][MAX_NODE_NUMBER*MAX_NODE_NUMBER]={};
#endif

so the program can be compiled for both large memory and small memory.

g++ defines:

$ g++ -E -dD -xc++ /dev/null | grep -i model
#define __code_model_small__ 1

$ g++ -E -dD -xc++ -mcmodel=large /dev/null | grep -i model
#define __code_model_large__ 1

Alternatively:

$ diff <(g++ -E -dD -xc++ /dev/null) <(g++ -E -dD -xc++ -mcmodel=large /dev/null)
270c270
< #define __code_model_small__ 1
---
> #define __code_model_large__ 1

you have command line option -D where you can define symbol

for example

-DMODEL_LARGE

then in your source file:

#ifdef MODEL_LARGE
static int include[MAX_NODE_NUMBER][MAX_NODE_NUMBER][MAX_NODE_NUMBER*MAX_NODE_NUMBER];
static bool calculated[MAX_NODE_NUMBER][MAX_NODE_NUMBER][MAX_NODE_NUMBER*MAX_NODE_NUMBER]={};
#endif

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