Run C++ Code from Python – Step-by-Step Guide
Beginner-Friendly Tutorial – Learn How Python Can Compile and Execute C++ Code
Python is an incredibly versatile language, but sometimes you might need the speed and performance of C++. This tutorial explains how to run C++ code directly from Python, making it easy to integrate both languages. You’ll learn how to write, compile, and execute a temporary C++ program from a Python script using the subprocess and tempfile modules. This is a great way to understand cross-language execution and automation using Python!
Table of Contents
- Overview
- Why Run C++ from Python?
- Step 1: Writing C++ Code in Python
- Step 2: Compiling C++ Code
- Step 3: Executing the Compiled Program
- Step 4: Cleaning Up Temporary Files
- Complete Code
- How It Works
- Key Learnings
- Further Experiments
Overview
This project shows how Python can act as a controller for compiling and executing C++ code. The program:
- Writes a simple C++ Hello World program to a temporary file.
- Uses
subprocess.run()to compile it using the g++ compiler. - Executes the compiled program and displays the C++ output inside Python.
- Cleans up all temporary files automatically.
Why Run C++ from Python?
- Combine Python’s flexibility with C++’s performance.
- Automate testing of C++ code or generate code dynamically.
- Build hybrid systems where Python handles logic and C++ handles computation-heavy tasks.
Step 1: Writing C++ Code in Python
The script starts with a simple Python print, followed by defining C++ code as a multi-line string.
import subprocess # To run external programs
import tempfile # To create temporary files for C++ code
import os # To handle file operations
print("Hello World from Python!") # Simple Python print
cpp_code = """
#include <iostream>
using namespace std;
int main() {
cout << "Hello World from C++!" << endl;
return 0;
}
"""
Here, the C++ code is stored as a string that will later be written to a temporary file.
Setting Up g++ on Your System
You need a working C++ compiler (g++) to run this tutorial. Follow the steps below depending on your OS.
Windows
- Install MinGW-w64 (Minimalist GNU for Windows):
- Visit: https://winlibs.com/ or https://sourceforge.net/projects/mingw-w64/
- Download and install it.
- Add the
bindirectory (e.g.,C:\mingw64\bin) to your PATH environment variable. - Open Command Prompt and type:
g++ --versionIf it prints version info, you’re ready to go!
macOS
- Install Xcode Command Line Tools by running:
xcode-select --install - Verify installation with:
g++ --versionYou should see Apple’s
clangcompiler, which works for this tutorial.
Linux (Ubuntu/Debian)
- Open Terminal and install
g++:sudo apt update sudo apt install g++ - Verify installation:
g++ --version
Step 2: Compiling C++ Code
We create a temporary .cpp file and compile it using the g++ compiler.
with tempfile.NamedTemporaryFile(delete=False, suffix=".cpp") as f:
cpp_file = f.name
f.write(cpp_code.encode())
exe_file = cpp_file.replace(".cpp", "")
compile_proc = subprocess.run(
["g++", cpp_file, "-o", exe_file],
capture_output=True,
text=True
)
tempfile.NamedTemporaryFile()creates a unique.cppfile in the system’s temporary directory.subprocess.run()executes the g++ compiler to compile the file into an executable.capture_output=Truecaptures any compilation messages.
Step 3: Executing the Compiled Program
After successful compilation, Python runs the compiled C++ binary.
if compile_proc.returncode != 0:
print("C++ compilation failed!")
print(compile_proc.stderr)
else:
result = subprocess.run([exe_file], capture_output=True, text=True)
print("Output from C++:")
print(result.stdout.strip())
If compilation fails, Python displays the compiler error messages. If it succeeds, it prints the C++ program output.
Example Output:
Hello World from Python!
Output from C++:
Hello World from C++!
Step 4: Cleaning Up Temporary Files
Finally, we remove temporary files created during compilation.
os.remove(cpp_file)
if os.path.exists(exe_file):
os.remove(exe_file)
This ensures no leftover files clutter your system.
Complete Code
import subprocess
import tempfile
import os
print("Hello World from Python!")
cpp_code = """
#include <iostream>
using namespace std;
int main() {
cout << "Hello World from C++!" << endl;
return 0;
}
"""
with tempfile.NamedTemporaryFile(delete=False, suffix=".cpp") as f:
cpp_file = f.name
f.write(cpp_code.encode())
exe_file = cpp_file.replace(".cpp", "")
compile_proc = subprocess.run(["g++", cpp_file, "-o", exe_file], capture_output=True, text=True)
if compile_proc.returncode != 0:
print("C++ compilation failed!")
print(compile_proc.stderr)
else:
result = subprocess.run([exe_file], capture_output=True, text=True)
print("Output from C++:")
print(result.stdout.strip())
os.remove(cpp_file)
if os.path.exists(exe_file):
os.remove(exe_file)
How It Works
- Python writes C++ code to a temporary file.
- g++ compiles the code into an executable.
- Python runs the executable and prints its output.
- Cleanup removes temporary files for a tidy finish.
Key Learnings
- Use
subprocess.run()to execute system commands from Python. - Create temporary files using
tempfilesafely. - Capture compiler output and program results.
- Integrate Python and C++ seamlessly for automation or hybrid projects.
Further Experiments
- Modify the C++ code dynamically (e.g., generate code from user input).
- Run multiple C++ snippets from one Python script.
- Measure execution time differences between Python and C++.
- Use Python to benchmark or visualize C++ results.
With this simple integration, you’ve built a bridge between Python and C++, unlocking a world of hybrid programming possibilities. You can extend this example to dynamically generate more complex C++ programs, compile them on the fly, and benchmark their performance using Python scripts.