Importing C++ Files in Pythona
Importing C++ program in a Python program is
called wrapping up of C++ in Python. Wrapping or creating Python interfaces for
C++ programs are done in many ways. The commonly used interfaces are
·
Python-C-API (API-Application
Programming Interface for interfacing with C programs)
·
Ctypes (for interfacing with c programs)
·
SWIG (Simplified Wrapper Interface Generator- Both C and C++)
·
Cython (Cython is both a Python-like language for writing
C-extensions)
·
Boost. Python (a framework for interfacing Python and C++)
·
MinGW (Minimalist GNU for
Windows)
MinGW refers to a set of runtime header files,
used in compiling and linking the code of C, C++ and FORTRAN to be run on
Windows Operating System.
MinGw-W64 (version of MinGW) is the best
compiler for C++ on Windows. To compile and execute the C++ program, you need
‘g++’ for Windows. MinGW allows to compile and execute C++ program dynamically
through Python program using g++.
Python program that contains the C++ coding can
be executed only through minGW-w64 project’ run terminal. The run terminal open
the command-line window through which Python program should be executed.
Refer installation of MinGW in Annexure -3
1. Double click the run terminal of MinGW
2. Go to the folder where the Python software
is located (Python.exe) is located. In this example “Python folder” is located
in
For example here “Python” is located in
C:\Program Files\OpenOffiice 4\Python.
The syntax to change from c:\> to the folder
where Python is located is
cd <absolute path>
where “cd” command refers to change directory
and absolute path refers to the complete path where Python is installed.
In this Example to go to the folder where
Python is located we should type the following command “cd C:\Program
Files\OpenOffiice 4\Program”. See that highlighted area in the above window.
Consider the Example pycpp.py is a Python
program which will read the C++program Pali.cpp. The “Pali.cpp” program accepts
a number and display whether it is a “Palindrome or Not”. For example the
entered input number is 232 the output displayed will be “Palindrome”. The C++
program Pali is typed in notepad and saved as pali.cpp. Same way the Python program pycpp.py code is also typed in notepad and saved as pycpp.py.
3. To execute our program double click the run
terminal change the path to the Python folder location. The syntax to execute
the Python program is
Python <filename.py> -i
<C++ filename without cpp extension>
Where
Python : keyword to execute the Python program from command-Line
filename.py - i : Name of the Python program to executed input
mode
C++ filename without cpp extension : name of C++ file to be compiled and executed
For example type Python pycpp.py –i pali in the
command prompt and press enter key. If the compilation is successful you will
get the desired output. Otherwise the error will be displayed.
Now let us will see the execution through our
example pycpp.py and pali.cpp. These two programs are stored in the folder
c:\pyprg. If the programs are not located in same folder where the Python exe
file is there then the complete path must be specified for the files during
execution. The output is displayed below
Now let us will see how to write the Python
program for compiling C++ code.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.