Importing C++ Programs in Python: One Mark Questions, Book Back Answers and Notes
A Python script can compile and run a C++ program without leaving the command prompt. These notes compare scripting and programming languages, lists the ways of wrapping C++ for Python, explains MinGW and g++, and walks through the sys, os and getopt modules and the __name__ == '__main__' check used in the textbook script.
Study this chapter
Asked in previous board exams
6 questions from this chapter came in 6 past public exams. Open any of them to see the answer and explanation.
-
March 2026
A framework for interfacing Python and C++ is:See answer in the solved paper →
-
March 2025
The module which allows to interface with the Windows operating system is:See answer in the solved paper →
-
March 2023
Importing C++ program in a Python program is called __________.See answer in the solved paper →
-
May 2022
A Framework for interfacing Python and C++ is :See answer in the solved paper →
-
Supplementary 2021
The module which allows interface with the Windows Operating System is :See answer in the solved paper →
-
March 2020
A framework for interfacing Python and C++ is :See answer in the solved paper →
About Importing C++ Programs in Python
Medium ~90 min study
C++ is compiled, so its programs run fast. Python is interpreted and quick to write. Python is often called a glue language because it can call code written in other languages and tie the pieces together. In this unit a short Python script takes the name of a C++ file, compiles it with g++ and runs the result, all from one command.
To make this work you need three things. MinGW gives Windows a C++ compiler (g++). The sys module gives the script the words typed on the command line. The os module lets the script run the compile command, and getopt separates the options from their values. The textbook runs the script as python c:\pyprg\pali.py -i c:\pyprg\pali_cpp.
Revise the differences between Python and C++, the wrapping tools, what sys.argv, os.system() and getopt.getopt() do, and the meaning of __name__. Then read the textbook script line by line until you can say what each line does without looking.
What you'll learn
- State the differences between Python and C++ as scripting and programming languages.
- Explain what MinGW and g++ do and why g++ must be on the PATH.
- Trace the textbook script that compiles and runs a C++ file from Python.
- Use sys.argv to read arguments given on the command line.
- Write the syntax of getopt.getopt() and explain its arguments and return values.
- Explain the purpose of if __name__ == '__main__': in a Python file.
Before you start
- Python functions, variables and control flow.
- Basic C++ program structure: header files and main().
- Running a command from the Windows command prompt.
Topics covered in this chapter
Importing C++ Programs in Python explained
Running C++ Programs from Python
Scripting and Programming Languages
C++ is a compiled, statically typed language: the whole program is translated to machine code before it runs, and each variable's type is fixed. Python is an interpreted, dynamically typed language: it needs no separate compile step, and a variable's type is decided when a value is assigned. A scripting language like Python is used to automate tasks and connect programs, which is why it is called a glue language. The fast parts can be written in C++ and controlled from Python.
Wrapping and MinGW
Making C++ code usable from Python is called wrapping. The textbook lists Python-C-API, Ctypes, SWIG (Simplified Wrapper Interface Generator), Cython, Boost.Python and MinGW. This chapter uses MinGW (Minimalist GNU for Windows), a set of runtime header files and GNU tools that lets C, C++ and FORTRAN code be compiled and linked on Windows. Its g++ program calls the GNU C++ compiler. MinGW's bin folder must be added to the PATH so that g++ can be run from the command prompt.
The sys Module and sys.argv
sys.argv is a list of the command-line arguments given when the script is run. sys.argv[0] is always the name of the Python script. The items after it are the options and file names you typed, such as -i and the C++ file name. That is why the script calls main(sys.argv[1:]): it passes everything except the script name.
The os Module and os.system()
The os module lets Python use operating system features. os.system() runs a command in the shell. The script builds the compile command by joining strings, os.system('g++ ' + inp_file + ' -o ' + exe_file), where -o names the output executable. A second call, os.system(exe_file), runs the program it produced. The spaces inside the quotes matter; without them the words run together and g++ fails.
Parsing Options with getopt
The syntax is opts, args = getopt.getopt(argv, options, [long_options]). In the textbook, getopt.getopt(argv, "i:", ['ifile=']) is used, where the colon after i means the -i option needs a value. opts is a list of (option, value) pairs such as ('-i', 'c:\pyprg\pali_cpp'), and args is a list of any remaining arguments that are not options. It is empty when there are none.
__name__ and Compilation Errors
When a file is run directly, Python sets the special variable __name__ to '__main__'. The line if __name__ == '__main__': makes main() run only in that case, not when the file is imported as a module. If the C++ source has errors, g++ prints its error messages, with line numbers, in the same command window where the script was run, and no executable is produced.
Common mistakes to avoid
- Not adding MinGW's bin folder to the PATH, so the command prompt reports that g++ is not recognised. Set the environment variable before running the script.
- Leaving out spaces while joining strings for os.system(). Write 'g++ ' + inp_file + ' -o ' + exe_file so the parts stay separate words.
- Passing the whole sys.argv list to main(). sys.argv[0] is the script name, so pass sys.argv[1:] to skip it.
- Typing the .cpp extension on the command line. The textbook script adds '.cpp' and '.exe' itself, so give only the file name, such as pali_cpp.
- Forgetting the colon in "i:" in getopt.getopt(). Without it, -i is treated as an option that takes no value.
Test yourself on these with the practice test, then check the worked reasoning in the solved MCQs.
Frequently asked questions
What is the difference between a scripting language and a programming language?
A scripting language like Python is interpreted and needs no separate compile step. A programming language like C++ must be compiled into machine code before it runs, and it is statically typed. Scripting languages are often used to automate tasks and connect other programs.
Why is Python called a glue language?
Because it can easily connect with code written in other languages. The parts that must run fast can be written in C or C++, and a Python script calls and controls them.
What is MinGW and why do we need it here?
MinGW (Minimalist GNU for Windows) is a set of runtime header files and GNU tools, including g++, for compiling C, C++ and FORTRAN programs on Windows. The Python script only issues the g++ command; MinGW supplies the compiler that actually builds the .exe file.
How does sys.argv work?
sys.argv is a list of everything typed after python on the command line. For python pali.py -i pali_cpp, sys.argv is ['pali.py', '-i', 'pali_cpp']. sys.argv[0] is the script name, and the rest are the options and values.
What does getopt.getopt() return?
It returns two lists. opts holds (option, value) pairs, for example ('-i', 'pali_cpp'). args holds any remaining arguments that are not options, and is empty if there are none. The script loops through opts and calls run() with the value of -i.
What happens if the C++ program has an error?
When os.system() runs g++, the compiler prints its error messages, with line numbers, in the command window where the script is running. No executable is created, so the second os.system() call has nothing to run.
Last updated 15 September 2026