简体   繁体   English

用C语言编写如何在运行时使用输入的函数名称作为字符串创建函数?

[英]In C language How to Create a function at runtime using the function name being input as a string?

I was reading the stringizing operator (#) in c which does the token pasting what ever is being appended to it .I wanted to know is it possible to create a function(with void or non void arguments) dynamically using the stringizing operator (and Macros if possible - #define) . 我正在阅读c中的字符串运算符(#),它会执行令牌粘贴粘贴附加到其上的内容。我想知道是否有可能使用字符串运算符(和)动态创建一个函数(带有void或non void参数)如果可能,请使用宏-#define)。 I want the following structure of the code : 我想要以下代码结构:

#define #function_name(/*void or non void */)  void function_name(/*void or non void */)

char *function_name;
scanf("%s",function_name);

something like this. 这样的事情。 Basically I am trying to create the function at runtime. 基本上,我试图在运行时创建函数。

One hacky way of doing what you need (and chances are you are not going to be happy with that answer) is as follows: 进行所需工作的一种骇人听闻的方式(而且很可能您对该答案不满意)如下:

  1. Create a text to be compiled 创建要编译的文本
  2. Save it into a file 将其保存到文件中
  3. Invoke a compiler (and make sure it returned "success", of course) 调用编译器(当然,请确保返回“成功”)
  4. Invoke a linker and produce a DLL (on Windows) or a shared library (on *nix) 调用链接器并生成DLL(在Windows上)或共享库(在* nix上)
  5. Dynamically load that library and get the entry address of your function 动态加载该库并获取函数的入口地址
  6. Call your function by its pointer 通过其指针调用函数

C/C++ languages do not provide a mechanism to execute text as code on-a-fly (like JavaScript's eval("var i = 0;") , for instance) C / C ++语言不提供即时执行文本作为代码的机制(例如,像JavaScript的eval("var i = 0;")

C is a statically compiled language, as such it doesn't make creating functions during runtime easy, or even generally possible. C是一种静态编译的语言,因此,它不能使运行时创建函数变得容易,甚至无法实现。 You could write out the C code you need to a .c file, execute a compiler on the system, and then dynamically load the object library. 您可以将所需的C代码写到.c文件,在系统上执行编译器,然后动态加载对象库。 In the course of writing out the initial .c file you could name the function what ever you wanted. 在写出初始.c文件的过程中,您可以根据需要命名函数。 In short though, using macros won't work to do what you want to do. 简而言之,使用宏无法完成您想做的事情。 And using the method I've just mentioned is a massive hack. 使用我刚刚提到的方法是一个巨大的漏洞。 Possible, but ill advised. 可能,但建议不要这样做。

If you just want to rename a function, or choose from a limited set of functions you know in advance and can compile into the program, then you can use a function pointer. 如果您只想重命名功能,或者从预先知道的有限功能集中进行选择并可以编译到程序中,则可以使用功能指针。 But this strikes me as the reverse of what you are trying to do, because the function pointer provides you with a static name (symbol) which can be used to call variable functions. 但这使我感到与您尝试做的事情相反,因为函数指针为您提供了一个静态名称(符号),可用于调用变量函数。

If you really need to rename a function pointer, you could keep an array of function pointers, and index them using a string mapping. 如果确实需要重命名函数指针,则可以保留一个函数指针数组,并使用字符串映射对它们进行索引。 Then call your functions by looking up their pointers via their strings (names). 然后通过通过字符串(名称)查找指针来调用函数。 the string is of course mutable, so you can change the 'name' of your function. 该字符串当然是可变的,因此您可以更改函数的“名称”。 But this is a very roundabout route and I honestly can't see a good reason to do this. 但这是一条非常绕行的路线,老实说,我看不出这样做的充分理由。

Finally, the most useful and correct solution would be to implement a virtual machine with it's own symbol table. 最后,最有用和正确的解决方案是使用自己的符号表来实现虚拟机。 There are implementations out there already; 已经有一些实现了。 Python and Slang spring to mind. 想到PythonSlang

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

相关问题 函数指针背后的原因是c语言中的函数名本身? - The reason behind function pointer being the function name itself in c language? 为什么这段代码有效? function 的输入是“string s”,但我们给出的实际输入是“int name”。 C语言 - Why does this code work? The input for the function is "string s" but the actual input we are giving is "int name". C language 尽管在函数 c 语言中发生了变异,但在使用函数后字符串并未发生变异 - String isn't getting mutated after using a function, despite being mutated inside the function c language 使用C语言从函数返回字符串 - Returning a string from a function using C language 使用单独的函数检查它是否为整数输入C语言 - Using a separate function to check if it's an integer input C Language 如何要求用户用C语言输入数学函数? - How to ask the user to input a mathematical function in c language? 如何用C语言的内存地址映射功能名称和行​​号? - How to map a function name and line number by a memory address in C language? 使用 C 语言创建具有给定字符串的结构名称 - Create a struct name with a given string in C language 如何在运行时在其定义中重命名C函数名称 - How to rename a C function name in its definition at runtime 我在字符串中有问题,在使用 get 函数时,在 c 语言中 - I have problem in string, in using the gets function, in c language
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM