简体   繁体   English

如何分配预处理器指令

[英]How to malloc a preprocessor directives

I have a macro as follow.我有一个宏如下。

#define WR_BLK_SIZE_REGINFO 123

Am using this macro in a library project(.a).我在库项目(.a)中使用这个宏。 Library projects are pre-compiled where the size of "WR_BLK_SIZE_REGINFO" cannot be changed as it is statically allocated in the library project.库项目是预编译的,其中“WR_BLK_SIZE_REGINFO”的大小不能更改,因为它是在库项目中静态分配的。

Now how can I dynamically allocate preprocessor directives code?现在如何动态分配预处理器指令代码?

I want something like below.我想要像下面这样的东西。 Is that possible?那可能吗?

#define WR_BLK_SIZE_REGINFO malloc

Let me brief out more if confused.如果感到困惑,让我再简要说明一下。

- What for this macro is used? - 这个宏有什么用?

Ans: This macro is used in various files of my library project. Ans:这个宏用于我的库项目的各种文件中。 For reducing code complexity I am using a macro to fix a size为了降低代码复杂性,我使用宏来固定大小

  • Why do you want to malloc the macro?为什么要 malloc 宏?

Ans: Am doing a library project, where this library projects will be used in many controllers. Ans:我在做一个库项目,这个库项目会用到很多控制器中。 Where each controller has its own memory set.每个控制器都有自己的内存集。 So I can't fix the size of that macro statically.所以我无法静态修复该宏的大小。 As it needs to be varied based on controllers memory.因为它需要根据控制器内存而变化。

Demonstrate some code?演示一些代码?

Sample.h样本.h

#define WR_BLK_SIZE_REGINFO 123

sample.c样本.c

if( !((numItems >= 1) && (numItems <= WR_BLK_SIZE_REGINFO)) )
{
    // logic            
}

sample1.c样本1.c

if( !((numItemsWrite >= 1) && (numItemsWrite <= WR_BLK_SIZE_REGINFO)) )
{
    // logic    
}

Note1: all the above files are library files which are pre-compiled and "123" is fixed here, which I don't want it to be fixed.注1:以上所有文件都是预编译的库文件,这里修复了“123”,我不希望它被修复。 I want to allocate dynamically.我想动态分配。

Note2: Am not sure whether malloc is the right choice.注2:我不确定 malloc 是否是正确的选择。 All I need is I wanted to input "123" at run time in the library project or during compile time of application project(Input 123 from application project to library project)我所需要的只是我想在库项目的运行时或应用程序项目的编译时输入“123”(从应用程序项目到库项目输入 123)

Note3: Library project are built using MPLAB X IDE, basically it is a microchip project.注 3:库项目是使用 MPLAB X IDE 构建的,基本上是一个微芯片项目。

In sample.h:在 sample.h 中:

int get_wr_blk_size(void);
#define WR_BLK_SIZE_REGINFO get_wr_blk_size()

In some C file somewhere在某个 C 文件中

static int wr_blk_size = 123;
static int set_wr_blk_size(int j) { wr_blk_size = j; }
int get_wr_blk_size(void) { return wr_blk_size; }

Note that any code that assumes WR_BLK_SIZE_REGINFO is a compile-time constant will have to be rewritten since it isn't anymore.请注意,任何假定 WR_BLK_SIZE_REGINFO 是编译时常量的代码都必须重写,因为它不再是了。

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

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