简体   繁体   中英

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). I want to understand how each of them runs with many different sets of inputs (which they get via 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
  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. 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).

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

You can compile you C++ code to a shared object (*.so file in linux),

and then load it into python using the ctypes library (using ctypes.LibraryLoader class).

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:

#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). And this function will call your C++ function myCppFunction and return the results back to python. You can also pass structs from python to C and vice-versa.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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