C Unstringification with Macros

Is there any way to expand the strings provided as macro arguments? I need to be able to call functions whose names are in strings. Something like that:

void hello() {
    printf("Hello, world!");
}

call_func("hello");

How to implement the call_func function. It will be in the module, which will be #included, and will be used to call functions in the main file c. If there is another way to do this, when the name should not be in the strings, but can be passed as an argument to a function that would be ok. Here is what I mean:

#define call_func(X) X()
void do_something(Some_kind_of_C_func_type i) {
    call_func(i)
}
void hello() {
    printf("Hello, world!");
}

do_something(C_FUNC(hello));
+3
source share
2 answers

Well, I see two ways to do this, depending on your goals.

; . . .

, , - dlopen() LoadLibrary(), , /.

+1

, , , , .

, ( ) .

#define INSERT(fn) addfn(#fn, &fn)
void addfn(char const* name, func_t fp);

addfn coult name.

0

Source: https://habr.com/ru/post/1762083/


All Articles