简体   繁体   English

在运行时定义C ++函数

[英]Define C++ function at runtime

I'm trying to adjust some mathematical code I've written to allow for arbitrary functions, but I only seem to be able to do so by pre-defining them at compile time, which seems clunky. 我正在尝试调整我编写的一些数学代码以允许任意函数,但我似乎只能通过在编译时预先定义它们来实现,这似乎很笨拙。 I'm currently using function pointers, but as far as I can see the same problem would arise with functors. 我目前正在使用函数指针,但据我所知,仿函数会出现同样的问题。 To provide a simplistic example, for forward-difference differentiation the code used is: 为了提供一个简单的例子,对于前向差分,使用的代码是:

double xsquared(double x) {
    return x*x;
}

double expx(double x) {
    return exp(x);
}

double forward(double x, double h, double (*af)(double)) {
    double answer = (af(x+h)-af(x))/h;

    return answer;  
}

Where either of the first two functions can be passed as the third argument. 前两个函数中的任何一个都可以作为第三个参数传递。 What I would like to do, however, is pass user input (in valid C++) rather than having to set up the functions beforehand. 但是,我想要做的是传递用户输入(在有效的C ++中),而不是事先设置功能。 Any help would be greatly appreciated! 任何帮助将不胜感激!

Historically the kind of functionality you're asking for has not been available in C++. 从历史上看,C ++中没有提供您要求的功能。 The usual workaround is to embed an interpreter for a language other than C++ (Lua and Python for example are specifically designed for being integrated into C/C++ apps to allow scripting of them), or to create a new language specific to your application with your own parser, compiler, etc. However, that's changing. 通常的解决方法是为C ++以外的语言嵌入解释器(例如,Lua和Python专门用于集成到C / C ++应用程序中以允许脚本编写),或者使用您的应用程序创建特定于您的应用程序的新语言。自己的解析器,编译器等。然而,这正在改变。

Clang is a new open source compiler that's having its development by Apple that leverages LLVM. Clang是一个新的开源编译器,由Apple开发,利用LLVM。 Clang is designed from the ground up to be usable not only as a compiler but also as a C++ library that you can embed into your applications. Clang从一开始就设计为不仅可以用作编译器,还可以用作可以嵌入到应用程序中的C ++库。 I haven't tried it myself, but you should be able to do what you want with Clang -- you'd link it as a library and ask it to compile code your users input into the application. 我自己没有尝试过,但是你应该能够用Clang做你想做的事 - 你将它作为一个库链接,并要求它将用户输入的代码编译到应用程序中。

You might try checking out how the ClamAV team already did this, so that new virus definitions can be written in C . 您可以尝试查看ClamAV团队已经如何做到这一点,以便可以用C语言编写新的病毒定义

As for other compilers, I know that GCC recently added support for plugins. 至于其他编译器,我知道GCC最近增加了对插件的支持。 It maybe possible to leverage that to bridge GCC and your app, but because GCC wasn't designed for being used as a library from the beginning it might be more difficult. 也许可以利用它来桥接GCC和你的应用程序,但由于GCC不是设计用于从一开始就用作库,因此可能更难。 I'm not aware of any other compilers that have a similar ability. 我不知道任何其他具有相似能力的编译器。

As C++ is a fully compiled language, you cannot really transform user input into code unless you write your own compiler or interpreter. 由于C ++是一种完全编译的语言,除非您编写自己的编译器或解释器,否则无法将用户输入转换为代码。 But in this example, it can be possible to build a simple interpreter for a Domain Specific Language which would be mathematical formulae. 但是在这个例子中,可以为域特定语言构建一个简单的解释器,它将是数学公式。 All depends on what you want to do. 一切都取决于你想做什么。

You could always take the user's input and run it through your compiler, then executing the resulting binary. 您始终可以获取用户的输入并通过编译器运行它,然后执行生成的二进制文件。 This of course would have security risks as they could execute any arbitrary code. 这当然会带来安全风险,因为它们可以执行任意代码。

Probably easier is to devise a minimalist language that lets users define simple functions, parsing them in C++ to execute the proper code. 可能更容易设计一种简约语言,让用户定义简单的函数,用C ++解析它们以执行正确的代码。

The best solution is to use an embedded language like lua or python for this type of task. 最好的解决方案是使用嵌入式语言(如lua或python)来完成此类任务。 See eg Selecting An Embedded Language for suggestions. 请参阅例如选择嵌入式语言以获取建议。

While it seems like a blow off, there are a lot of people out there who have written equation parsers and interpreters for c++ and c, many commercial, many flawed, and all as different as faces in a crowd. 虽然看起来像是一个吹嘘,但是有很多人为c ++和c编写了方程解析器和解释器,许多商业,许多有缺陷,并且与人群中的面孔不同。 One place to start is the college guys writing infix to postfix translators. 一个开始的地方是大学的家伙写中缀到postfix翻译。 Some of these systems use paranthetical grouping followed by putting the items on a stack like you would find in the old HP STL library. 其中一些系统使用paranthetical分组,然后将项目放在堆栈中,就像在旧的HP STL库中找到的那样。 I spent 30 seconds and found this one: 我花了30秒钟发现了这个:

http://www.speqmath.com/tutorials/expression_parser_cpp/index.html http://www.speqmath.com/tutorials/expression_parser_cpp/index.html

possible search string:"gcc 'equation parser' infix to postfix" 可能的搜索字符串:“gcc'方程解析器'中缀到后缀'

C++, unlike some other languages like Perl, isn't capable of doing runtime interpretation of itself. 与其他语言(如Perl)不同,C ++无法对其自身进行运行时解释。

Your only option here would be to allow the user to compile small shared libraries that could be dynamically-loaded by your application at runtime. 这里唯一的选择是允许用户编译可在运行时由应用程序动态加载的小型共享库。

Well, there are two things you can do: 嗯,你可以做两件事:

  1. Take full advantage of boost/C++0x lambda's and to define functions at runtime. 充分利用boost / C ++ 0x lambda并在运行时定义函数。

  2. If only mathematical formula's are needed, libraries like muParser are designed to turn a string into bytecode, which can be seen as defining a function at runtime. 如果只需要数学公式,那么像muParser这样的库被设计为将字符串转换为字节码,这可以看作是在运行时定义一个函数。

You may use tiny C compiler as library (libtcc). 您可以使用tiny C编译器作为库(libtcc)。

It allows you to compile arbitrary code in run-time and load it, but it is only works for C not C++. 它允许您在运行时编译任意代码并加载它,但它只适用于C而不是C ++。

Generally the only way is following: 一般来说,唯一的方法是:

  • Pass the code to compiler and create shared object or DLL 将代码传递给编译器并创建共享对象或DLL
  • Load this Shared object or DLL 加载此共享对象或DLL
  • Use function from this shared object. 使用此共享对象中的函数。

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

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