Skip to main content

xycpplib

xycpplib lets a Python program compile and execute C++ source using the C++ compiler installed on the machine. It is useful for running competitive programming solutions, including GCC conveniences such as #include <bits/stdc++.h>, from Python.

It is not a source-to-source C++-to-Python translator: arbitrary C++ cannot be faithfully translated to Python. Instead, the original C++ is compiled, so programs work to the extent that the selected local compiler supports them.

Install

python -m pip install -e .

g++, clang++, or c++ must be available on PATH. Set XYCPPLIB_CXX to choose another compiler explicitly.

Use in a Python file

Python cannot contain bare C++ after an import: the Python parser rejects it before the import can run. Put the C++ in a string and call main to make the Python file behave like the C++ executable:

from xycpplib import main

main(r"""
#include <bits/stdc++.h>
using namespace std;
#define int long long
signed main(){
    int d,t; cin>>d>>t;
    int cnt=0;
    vector<int> a(d+1,0);
    vector<int> b(d+1,0);
    while(t--){
        int x,y; cin>>x>>y;
        a[x]--;
        if(a[x]<b[x]) b[x]=a[x];
        a[y]++;
    }
    for(int i:b) cnt+=(0-i);
    cout<<cnt;
}
""")

Run it normally and pipe input just as you would with a C++ binary:

Get-Content input.txt | python solution.py

For code that needs the output as a Python value, use run:

import xycpplib

answer = xycpplib.run(CPP_SOURCE, input="3 4\n1 2\n1 2\n2 3\n3 2\n")
assert answer == "2"

Executables are cached by source, compiler arguments, and selected compiler in the user's cache directory. Use xycpplib.clear_cache() to remove this library's cache.

Create a portable Python launcher

import xycpplib

xycpplib.write_python("solution.py", CPP_SOURCE)

The generated file embeds the C++ source and requires xycpplib plus a C++ compiler when it is run.

Release files for xycpplib 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for xycpplib 0.1.0
File Size Uploaded
xycpplib-0.1.0.tar.gz 7.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for xycpplib 0.1.0
File Interpreter ABI Platform
xycpplib-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size:14.6 kB

Release files / xycpplib-0.1.0.tar.gz

Download URL xycpplib-0.1.0.tar.gz
Size 7.1 kB
Tags Source
SHA-256 checksum
How to use checksums
78f0a7051d44372ed3771ab3f7fe4d160b7dea087e6127afbae02337fc87d3e2
BLAKE2b-256 checksum
How to use checksums
4816db39abd6c2b8a819cc4f27180766023ff8a7f0d1078e1a1d6efcdb2d8c7d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.2

Release files / xycpplib-0.1.0-py3-none-any.whl

Download URL xycpplib-0.1.0-py3-none-any.whl
Size 7.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1ee13b284d919593d1341c8011fc46dd365c273ec01c21ae9aad0b13fbead43a
BLAKE2b-256 checksum
How to use checksums
f05c231559d0b6d63a449037d372f3ceac334e9f5ec334a233cb9338bdbd1d70
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.2

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page