调用函数只知道C中运行时的参数类型?

J. *_* In 6 c

假设我有一个功能:

int foo (int A, char B){...}
Run Code Online (Sandbox Code Playgroud)

我想要实现的功能之一是用户能够通过Linux终端调用应用程序上的任何功能.因此,作为软件的输入,在终端中输入如下内容:

foo 2 'a'

然后我的应用程序解析它,并使用符号表,它能够找到其foo()所有参数的地址和类型.

但是,我不确定在调用它时如何将参数传递给函数,因为根据调用的函数,我可以有数百种不同的参数类型组合.

在调用函数之前,如果没有数百个嵌套的if语句将参数转换为正确的类型,有何提示如何实现?

该功能类似于GDB所具有的功能,您可以在其中执行call foo(2,'a')GDB调用.

Ber*_*ann 3

There are two approaches to this. If what you described is all you want to do, then you can use the dyncall library so that you dont have to worry about platform/compiler-specific calling semantics yourself:

The dyncall library encapsulates architecture-, OS- and compiler-specific function call semantics in a virtual bind argument parameters from left to right and then call interface allowing programmers to call C functions in a completely dynamic manner. In other words, instead of calling a function directly, the dyncall library provides a mechanism to push the function parameters manually and to issue the call afterwards.

The other approach is, if you might want to do more: e.g. what if an argument cannot be created by a literal? What if the argument is the output of another function? Can you write f(123, g("a")) in your console? Can you write x=g("a"); f(x)? And if(cond) x="a" else x="b"; f(x) In this case you need to embed a scripting language like e.g. LUA.