TN Online TestSamacheer Kalvi practice

Importing C++ programs in Python - Study Notes

Share this chapter: Telegram

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

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

Exam Tips

Solved MCQs → Practice test →

More for this chapter

Book Back Questions10 textbook MCQs · solved Additional MCQs15 extra MCQs · solved Practice TestInteractive · instant score Book Back TestTest yourself on the textbook set Additional MCQ TestTest yourself on the extra set Formula SheetAll key formulas

More chapters in Computer Science

View all
1 Function 2 Data Abstraction 3 Scoping 4 Algorithmic Strategies 5 Python Variables and Operators 6 Control Structures 7 Python Functions 8 Strings and String Manipulation 9 Lists, Tuples, Sets and Dictionary 10 Python Classes and Objects 11 Database Concepts 12 Structured Query Language (SQL) 13 Python and CSV Files 15 Data Manipulation through SQL 16 Data Visualization using Pyplot: Line Chart, Pie Chart and Bar Chart