相关疑难解决方法(0)

如何在C++中生成编译时的中断处理程序数组?

我希望能够在一个地方写我的ISR:

some_collection TimerHandlers;    

// added to ISR table in linker script
void rawTimerIRQHandler() {
    call_each_handler_in(handlers);
}
Run Code Online (Sandbox Code Playgroud)

这样我就可以在其他文件中注册处理程序了

// file1.cpp
void ledTimerHandler1() {

}
register(ledTimerHandler1); //or in an init function if not possible here
Run Code Online (Sandbox Code Playgroud)
// file2.cpp
void ledTimerHandler2() {

}
register(ledTimerHandler2); //or in an init function if not possible here
Run Code Online (Sandbox Code Playgroud)

而当硬件跳转到rawTimerIRQHandler,它执行ledTimerHandler1以及ledTimerHandler2在一些任意顺序.


显然,我可以使用类似于a的东西来实现它vector<void(*)()>,但由于这些处理程序的数量在编译时是已知的,有没有什么方法可以在编译时生成数组(或模板链表)?我想避免随附的动态内存分配vector.

我愿意使用template<>,#define甚至是GCC特定的属性来实现这一目标.

c++

5
推荐指数
1
解决办法
577
查看次数

在main()之前的编译时或运行时初始化函数指针的全局数组

我正在尝试在C或C++中在编译时初始化函数指针的全局数组.像这样的东西:

module.h中

typedef int16_t (*myfunc_t)(void);
extern myfunc_array[];
Run Code Online (Sandbox Code Playgroud)

module.cpp

#include "module.h"
int16_t myfunc_1();
int16_t myfunc_2();
...
int16_t myfunc_N();

// the ordering of functions is not that important
myfunc_array[] = { myfunc_1, myfunc_2, ... , myfunc_N };
Run Code Online (Sandbox Code Playgroud)

func1.cpp,func2.cpp,... funcN.cpp(指向单个func.cpp文件的符号链接,以便创建不同的目标文件:func1.o,func2.o,func3.o,...,funcN .o.NUMBER定义使用g++ -DNUMBER=N)

#include "module.h"
#define CONCAT2(x, y) x ## y
#define CONCAT(x, y) CONCAT2(x, y)

int16_t CONCAT(myfunc_, NUMBER)() { ... }
Run Code Online (Sandbox Code Playgroud)

使用g ++ -DNUMBER = N编译时,预处理后变为:

func1.cpp

...
int16_t myfunc_1() { ... }
Run Code Online (Sandbox Code Playgroud)

func2.cpp

...
int16_t myfunc_2() { ... } …
Run Code Online (Sandbox Code Playgroud)

c c++

4
推荐指数
2
解决办法
4318
查看次数

标签 统计

c++ ×2

c ×1