Browse Source

Add array build by preproc

master
Mathieu Maret 4 years ago
commit
80641fc03f
5 changed files with 78 additions and 0 deletions
  1. +20
    -0
      C/preproc_array/Makefile
  2. +8
    -0
      C/preproc_array/a.c
  3. +8
    -0
      C/preproc_array/b.c
  4. +11
    -0
      C/preproc_array/i.h
  5. +31
    -0
      C/preproc_array/main.c

+ 20
- 0
C/preproc_array/Makefile View 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
- 0
C/preproc_array/a.c View File

@ -0,0 +1,8 @@
#include "i.h"
#include <stdio.h>
static void f1(void) {
printf("A\n");
}
ADD_FUNC(f1);

+ 8
- 0
C/preproc_array/b.c View File

@ -0,0 +1,8 @@
#include "i.h"
#include <stdio.h>
static void f1(void) {
printf("B\n");
}
ADD_FUNC(f1);

+ 11
- 0
C/preproc_array/i.h View 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
- 0
C/preproc_array/main.c View 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…
Cancel
Save