Importing C++ programs in Python - Study Notes
Chapter Summary
This chapter shows Python working as a scripting or "glue" language that drives a C++ program. C++ is a statically typed language that must be compiled before it runs. Python is dynamically typed and runs through an interpreter. Putting them together lets you write the heavy logic in C++ and control it from a short Python script. The script uses the g++ compiler from MinGW along with Python's built-in sys, os and getopt modules to compile the C++ file, run the executable and show any errors.
Learning Objectives
- Tell a scripting language apart from a programming language and explain how each is run.
- Explain what wrapping a C++ program in Python means and why it is done.
- Use the MinGW interface (g++) on Windows to compile a C++ source file.
- Use the sys, os and getopt modules to read command-line input and run system commands.
- Read C++ compilation errors when they are displayed through a Python script.
Key Concepts and Definitions
Scripting Language: a language that is used to control and connect other programs and languages. It is usually interpreted, so there is no separate compilation step.
Programming Language: a full-featured language whose source code is translated by a compiler into machine code before it can run.
Garbage Collection: Python's automatic memory management, which frees the memory held by objects that are no longer used. C++ does not do this automatically.
Wrapping: writing a Python interface around code in a compiled language such as C++ so that it can be called from Python.
sys.argv: a list in the sys module that holds the command-line arguments given to the script. sys.argv[0] is the name of the script itself.
os.system(): a function in the os module that runs an operating system command, such as the g++ compile command, from inside Python.
getopt.getopt(): a function in the getopt module that splits command-line input into two lists: opts, a list of (option, value) pairs, and args, the arguments left over.
__name__ Variable: a special built-in variable. When a file is run directly as the main program, its value is "__main__".
Worked Methods
Step-by-Step Compilation and Execution
Running a C++ program from Python happens in three stages: read the input, build the program, run it. First, getopt reads the command line and picks out the name of the C++ file given after the -i option. Next, the script forms the file names by joining the name with ".cpp" for the source and ".exe" for the executable. It then calls os.system with a g++ command to compile the source into the executable. Finally, a second os.system call runs that executable, and the program's output appears on the screen.
Common Exam Traps
- Spaces in the Command String: the compile command is built by joining strings, as in 'g++ ' + cpp_file + ' -o ' + exe_file. If you drop the space after g++ or around -o, the words run together and the shell cannot understand the command. Check every space.
- Command-Line Indexing: students often take sys.argv[0] as the first argument typed by the user. It is not: sys.argv[0] is the script name, and the user's arguments start at sys.argv[1]. That is why the script passes sys.argv[1:] to getopt.
- What getopt Returns: getopt gives back two lists, opts and args. opts holds each recognised option with its value; args holds whatever arguments are left after the options. An unrecognised option does not go into args: it raises a getopt.GetoptError.
Exam Tips
- C++ is statically typed and compiled before running; Python is dynamically typed and run by an interpreter.
- Keep the three modules separate in your mind: sys gives the command-line arguments, os runs system commands, and getopt parses options and their values.
- Be ready to explain __name__ and how if __name__ == '__main__': lets code run only when the file is executed directly, not when it is imported.