Automatically create a table of function pointers in C

I am looking for a way to automatically (as part of the compilation / assembly process) generate a “table” of function pointers in C.

In particular, I want to create an array of structures like:

typedef struct {
  void (*p_func)(void);
  char * funcName;
} funcRecord;

/* Automatically generate the lines below: */

extern void func1(void);
extern void func2(void);
/* ... */

funcRecord funcTable[] =
{
  { .p_func = &func1, .funcName = "func1" },
  { .p_func = &func2, .funcName = "func2" }
  /* ... */
};

/* End automatically-generated code. */

... where func1 and func2 are defined in other source files.

So, given the set of source files, each of which contains one function that takes no arguments and does not return void, how does it automatically (as part of the build process) generate an array similar to the one indicated above that contains each function from the files? I would like to be able to add new files and automatically insert them into the table when recompiling.

, , , C , nix- (, make, perl, shell- ( )).

?

, , , - . . " ", , . , . , (), , . main() , , .

"" , (, - ) ( ).

, - - . , StackOverflow!

+5
6

#define FUNC_LIST \
  FUNC( func1 ) \
  FUNC( func2 ) \
  FUNC( func3 ) \
  FUNC( func4 ) \
  FUNC( func5 ) 

extern

#define FUNC( _name ) extern void _name(void);

FUNC_LIST

#undef FUNC

#define FUNC( _name ) { .p_func = &_name, .funcName = #_name },

funcRecord funcTable[] = {
  FUNC_LIST
};

#undef FUNC

dlsym (..)

, - dlsym , RTLD_DEFAULT, , .

#include <stdio.h>
#include <dlfcn.h>

void test2() {
  printf("Second place is the first loser!\n");
}

void test42() {
  printf("Read The Hitchhikers Guide To The Galaxy!\n");
}

int main() {
  int i;
  for (i=1; i<100; i++) {
    char fname[32];
    void (*func)();
    sprintf(fname, "test%d", i);
    func = dlsym(RTLD_DEFAULT, fname);
    if (func)
      func();
  }
  return 0;
}
+7

MSVC,

dumpbin /symbols  foo.obj > foo_symbols.txt

( ) . , . External , UNDEF

exe dll, .MAP, , .

.obj, COFF, . COFF , , , . http://en.wikipedia.org/wiki/COFF

+3

. ,

{ .p_func = &functionname, .funcName = "functionname" },

, . make

UTILITY_FUNCTION_HEADER:= func1.h func2.h func3.h
func_table.h: ${UTILITY_FUNCTION_HEADERS}
        write_header.sh > $@
        for file in @^; do extract_function_name.sh >> $@; done
        write_footer.sh >>$@
+3

, , , , .

" " à la gettext (.. , , ):

#define TEST_CASE(f) f

T TEST_CASE(f)(D x, ...)
{
        /* ... */
}

sed awk, , . awk , , - :

match($0, /TEST_CASE\([a-zA-Z_][a-zA-Z_0-9]*\)/) {
        name = substr($0, RSTART, RLENGTH)
        sub(/^TEST_CASE\(/, "", name)
        sub(/\)$/, "", name)
        funcs[name]
}

END {
        for (f in funcs)
                printf "func_type %s;\n", f
        print "funcRecord funcTable[] = {"
        for (f in funcs)
                printf "\t{ .p_func = %s, .funcName = \"%s\" },\n", f, f
        print "};"
}

( bsearch() - ing), : (sed one-liner ), sort (1), ( awk ). / , extern .

, , . (). typedef (func_type ) , IMHO ( , , , ).

, , make , dmckee ( .c, .h, ). , :

TEST_SRCS=      test1.c test2.c test3.c

test_funcs.c: ${TEST_SRCS}
        echo '#include "test.h"' >$@
        awk -f extract_test_funcs.awk ${TEST_SRCS} >>$@
+1

C . Linux nm foo.o C . " ", , .

: ( " " func1() ") . . C ( ), C. - , . , nm C, " ". make , .

0

-. - , : OK C , /project-devel/project/new_function.c. , /project-devel/tests/test_new_function.c /project-devel/tests/make_new_function_test, , , , .

, , ? ( ), perl/python script, makefile, function.c/.h , , - r"#include \\\"(\s+?).h\\\"" "update script", / makefile . - , test.c, .

.

0

All Articles