Add array build by preproc
This commit is contained in:
commit
80641fc03f
20
C/preproc_array/Makefile
Normal file
20
C/preproc_array/Makefile
Normal file
@ -0,0 +1,20 @@
|
||||
CPPFLAGS ?= -Werror -Wall -std=c99
|
||||
|
||||
program = test
|
||||
sources = $(wildcard *.c)
|
||||
objects = $(sources:%.c=%.o)
|
||||
depends = $(sources:%.c=%.d)
|
||||
|
||||
%.d: %.c
|
||||
@$(CPP) $(CPPFLAGS) -c -MP -MM -MT "$@ $*.o" $< >$@
|
||||
|
||||
$(program): $(objects)
|
||||
$(CXX) -o $@ $^ $(LDFLAGS) $(LDLIBS)
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
rm -f $(program) $(objects) $(depends)
|
||||
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
-include $(depends)
|
||||
endif
|
8
C/preproc_array/a.c
Normal file
8
C/preproc_array/a.c
Normal file
@ -0,0 +1,8 @@
|
||||
#include "i.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static void f1(void) {
|
||||
printf("A\n");
|
||||
}
|
||||
|
||||
ADD_FUNC(f1);
|
8
C/preproc_array/b.c
Normal file
8
C/preproc_array/b.c
Normal file
@ -0,0 +1,8 @@
|
||||
#include "i.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static void f1(void) {
|
||||
printf("B\n");
|
||||
}
|
||||
|
||||
ADD_FUNC(f1);
|
11
C/preproc_array/i.h
Normal file
11
C/preproc_array/i.h
Normal file
@ -0,0 +1,11 @@
|
||||
typedef void (*my_func_cb)(void);
|
||||
|
||||
typedef struct func_ptr_s {
|
||||
my_func_cb cb; /* function callback */
|
||||
} func_ptr_t;
|
||||
|
||||
#define ADD_FUNC(func_cb) \
|
||||
static func_ptr_t ptr_##func_cb \
|
||||
__attribute((used, section("my_array"))) = { \
|
||||
.cb = func_cb, \
|
||||
}
|
31
C/preproc_array/main.c
Normal file
31
C/preproc_array/main.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include "i.h"
|
||||
#include <stdio.h>
|
||||
|
||||
static void f3(void) {
|
||||
printf("MAIN\n");
|
||||
}
|
||||
|
||||
ADD_FUNC(f3);
|
||||
|
||||
#define section_foreach_entry(section_name, type_t, elem) \
|
||||
for (type_t *elem = \
|
||||
({ \
|
||||
extern type_t __start_##section_name; \
|
||||
&__start_##section_name; \
|
||||
}); \
|
||||
elem != \
|
||||
({ \
|
||||
extern type_t __stop_##section_name; \
|
||||
&__stop_##section_name; \
|
||||
}); \
|
||||
++elem)
|
||||
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
section_foreach_entry(my_array, func_ptr_t, entry) {
|
||||
entry->cb(); /* this will call f1, f2 and f3 */
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user