简体   繁体   中英

Passing C++ array to python

I would like to embed python into my C++ code. In C++ I am constructing a matrix and vector and would like to use Python's Numpy to solve the linear system. In order to build up to this: I am following this example: http://realgonegeek.blogspot.com/2013/08/how-to-pass-c-array-to-python-solution.html

however this particular line: import_array ();
is not working with my code. I am getting the following error:

1678:144: error: return-statement with no value, in function returning 'int' [-fpermissive] #define import_array() {if (_import_array() < 0) {PyErr_Print(); PyErr_SetString(PyExc_ImportError, "numpy.core.multiarray failed to import"); return NUMPY_IMPORT_ARRAY_RETVAL; } }

Has anyone successfully embedded python code into their C++ code? or may know why that particular line may not work. I am using python 3.4

one of the suggestions for that: if your matrix, vector, or array is static, you can set an argument for the c file just like this:

program.exe -array "[[2,2],[3,3],[4,4]]"

and use strtok or substr in c/c++ for splitting the string.

or if your program dynamically uses a new matrix/vector/array you can save it to a temp file or memory(C/C++) and if all above couldn't solve your problem you may finally re-write that part of the code in C++ language.

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