简体   繁体   English

如何自动执行 C++ 代码块(最好来自 python)

[英]How to automatically execute chunks of C++ code (ideally from python)

I have ~10k independent and relatively simple.cpp files (let's assume only one 30-line main() function).我有大约 10k 独立且相对简单的.cpp 文件(假设只有一个 30 行 main() 函数)。 I want to understand how each of them runs with many different sets of inputs (which they get via cin ).我想了解它们每个人如何使用许多不同的输入集(它们通过cin获得)运行。 In particular, I want to do the following procedure thousands of times:特别是,我想数千次执行以下过程:

  1. Pick one of the files选择其中一个文件
  2. Give it a set of inputs给它一组输入
  3. Select a chunk of lines in the.cpp file Select .cpp 文件中的一大块行
  4. Obtain which variables have changed and from what values to what values.获取哪些变量已更改以及从哪些值更改为哪些值。

Alternatively, selecting a line and getting all the values for each declared variable is also good enough for me.或者,选择一行并获取每个声明变量的所有值对我来说也足够了。

I'm trying to code this in Python, but I'm open for other languages as well.我正在尝试在 Python 中对此进行编码,但我也对其他语言开放。 Looking at related questions and generic googling I've run into libclang (but seems mostly to look at Abstract Syntax Trees at compile time), gdb (may fit the bill, but not sure exactly how), and pybind11 (seems mostly tuned towards integrating one C++ package at a time and not running line by line).查看相关问题和通用谷歌搜索,我遇到了 libclang(但似乎主要在编译时查看抽象语法树)、gdb(可能符合要求,但不确定具体如何)和 pybind11(似乎主要针对集成一次一个 C++ package 而不是逐行运行)。

In case of multiple alternatives, I care more about ease of code than compute efficiency when running the analysis.在有多种选择的情况下,我更关心代码的易用性而不是运行分析时的计算效率。

How would you approach this task?您将如何处理这项任务?

You should look into python's ctypes library: https://docs.python.org/3.8/library/ctypes.html您应该查看 python 的 ctypes 库: https://docs.python.org/3.8/library/ctypes.html

You can compile you C++ code to a shared object (*.so file in linux),您可以将 C++ 代码编译为共享的 object(Linux 中的 *.so 文件),

and then load it into python using the ctypes library (using ctypes.LibraryLoader class).然后使用 ctypes 库(使用ctypes.LibraryLoader类)将其加载到 python 中。

I did this in the past and I remember that I needed a small C code that "glues" python and C++ (because you need to convert the C data types that python uses into C++ data types). I did this in the past and I remember that I needed a small C code that "glues" python and C++ (because you need to convert the C data types that python uses into C++ data types).

The C "glue" file will look something like this: C“胶水”文件将如下所示:

#include "myCppHeader.h"  // includes prototype for C++ function myCppFunction

extern "C" {
   int callThisFromPython(int intFromPythonCode)
   {
          return myCppFunction(intFromPythonCode);
   }
}

From the python side you will call the callThisFromPython function (using ctypes library).从 python 端,您将调用callThisFromPython function(使用 ctypes 库)。 And this function will call your C++ function myCppFunction and return the results back to python.而这个 function 将调用你的 C++ function myCppFunction并将结果返回给 Z2FCZ6EEEB43A49BDD7526 You can also pass structs from python to C and vice-versa.您还可以将结构从 python 传递到 C,反之亦然。

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

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