简体   繁体   English

如何使用宏来调用函数?

[英]How to use macro for calling function?

I want to call function according to func_name string. 我想根据func_name字符串调用函数。 My code is here below: 我的代码如下:

#define MAKE_FUNCNAME func_name##hello

void call_func(void* (*func)(void))
{
    func();
}

void *print_hello(void)
{
    printf("print_hello called\n");
}

int main(void)
{
    char func_name[30] = "print_";

    call_func(MAKE_FUNCNAME);
    return 0;
}

But this code doesn't work. 但是这段代码不起作用。 I want code to work like call_func(print_hello) . 我希望代码像call_func(print_hello)一样工作。 But preprocessor treated my code like call_func("print_hello") . 但是预处理器将我的代码视为call_func("print_hello") How to use macro in C to make my exception? 如何在C中使用宏来实现我的异常? Or is it not possible using C ? 或者是不可能使用C

Then problem with your code is that the value of func_name is only known at run-time. 然后,您的代码问题是func_name的值仅在运行时已知。

You can however to it like this: 但是你可以这样:

#define MAKE_FUNCNAME(FUNCNAME) FUNCNAME##hello

void call_func(void* (*func)(void))
{
    func();
}

void *print_hello(void)
{
    printf("print_hello called\n");
}

int main(void)
{
    call_func(MAKE_FUNCNAME(print_));
    return 0;
}

But it is not possible to use a string value within macro parameters like in your code snippet. 但是在代码片段中的宏参数中不可能使用字符串值。

If you want to get call functions with their names using string values you can use a table to store function pointer with function names like this: 如果你想使用字符串值获取名字的调用函数,你可以使用一个表来存储函数指针,函数名如下:

struct {
    const char *name;
    void (*ptr)(void);
};

You can use an array of this structure to find out the function pointer at run-time using a string value. 您可以使用此结构的数组在运行时使用字符串值查找函数指针。 This is the most common solution to using run-time strings to call functions using their names. 这是使用运行时字符串使用其名称调用函数的最常见解决方案。

You can't do that. 你不能这样做。 The value of func_name is known at run-time (even though it is a const char *), while you want to determine what to call at precompile-time. func_name的值在运行时是已知的(即使它是一个const char *),而您想要确定在预编译时调用的内容。 You should turn your cpp macro into something different (such as an if/switch statement or using an indirection). 您应该将cpp宏转换为不同的东西(例如if / switch语句或使用间接)。

Maybe you could have a look to dlsym(). 也许你可以看看dlsym()。

Not sure I really understand the question, but if you want to "build" the function name at runtime and then call the corresponding function, it should be possible with dlsym() 不确定我是否真的理解这个问题,但是如果你想在运行时“构建”函数名称然后调用相应的函数,那么应该可以使用dlsym()

/* compile with: gcc example.c -ldl -rdynamic */
#include <dlfcn.h>
#include <stdio.h>

int print_hello(void)
{
    return printf("hello\n");
}

int main(int argc, char *argv[])
{
    const char *name = "print_hello";
    if (argc == 42) 
        print_hello(); /* for compiler not to remove print_hello at
                        * compile time optimisation in this example*/
    void *handle = dlopen(NULL /* self */, RTLD_NOW);
    int (*f)(void) = dlsym(handle, name);
    f();
    return dlclose(handle);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM