🌐
GitHub
github.com › cogu › cfile
GitHub - cogu/cfile: A python C code generator
import cfile C = cfile.CFactory() code = C.sequence() code.append(C.sysinclude("stdio.h")) code.append(C.blank()) char_ptr_type = C.type("char", pointer=True) code.append(C.declaration(C.function("main", "int", params=[C.variable("argc", "int"), C.variable("argv", char_ptr_type, pointer=True) ]))) main_body = C.block() main_body.append(C.statement(C.func_call("printf", C.str_literal(r"Hello World\n")))) main_body.append(C.statement(C.func_return(0))) code.append(main_body) writer = cfile.Writer(cfile.StyleOptions()) print(writer.write_str(code))
Starred by 91 users
Forked by 25 users
Languages   Python 99.7% | Batchfile 0.3% | Python 99.7% | Batchfile 0.3%
🌐
GitHub
github.com › inducer › cgen
GitHub - inducer/cgen: C/C++ source generation from an AST
cgen offers a simple abstract syntax tree for C and related languages (C++/CUDA/OpenCL) to allow structured code generation from Python.
Starred by 171 users
Forked by 35 users
Languages   Python 100.0% | Python 100.0%
Discussions

How to generate C code from Python? - Stack Overflow
I am writing an embedded algorithm. Currently I have lookup tables that are hard-coded: static const int32_t sine[65536] = { ... } The algorithm should be portable on different platforms but,... More on stackoverflow.com
🌐 stackoverflow.com
C++ code generation with Python - Stack Overflow
Can anyone point me to some documentation on how to write scripts in Python (or Perl or any other Linux friendly script language) that generate C++ code from XML or py files from the command line. ... More on stackoverflow.com
🌐 stackoverflow.com
python - Generating C code from script: how to template? - Stack Overflow
I'm looking for a way to insert some auto-generated code into C source files. The code generation is in python and that's not a problem, the trick is how to replace/update blocks of code in existi... More on stackoverflow.com
🌐 stackoverflow.com
Are there any libraries that can easily convert Python to C/C#/or C++? Ones where a person doesn't have to "calibrate" it, just, pip install library and then they can have their Python code in C,C#,or C++?
The closest thing to what you're asking for would be Cython, which is a mostly-superset of Python and generates C code. Nuitka is similar, except that it also compiles said code into binaries. Although if you're expecting said C code to be easy to read, I'm afraid you'll be sorely disappointed. I'm not sure why you listed C# here, considering it's very different from either of the other two you mentioned. Why would you want a Python-to- transpiler anyway? If you want more performance, just write the performance critical parts in C or Rust in the first place, then have Python access them via CFFI. More on reddit.com
🌐 r/learnpython
40
32
April 6, 2023
🌐
GitHub
github.com › cython › cython
GitHub - cython/cython: The most widely used Python to C compiler · GitHub
Cython translates Python code to C/C++ code, but additionally supports calling C functions and declaring C types on variables and class attributes. This allows broad to fine-grained manual tuning that lets the compiler generate very efficient ...
Starred by 10.7K users
Forked by 1.6K users
Languages   Cython 51.6% | Python 41.9% | C 6.2% | C++ 0.2% | Shell 0.1% | Starlark 0.0%
🌐
GitHub
github.com › JhnW › devana
GitHub - JhnW/devana: Python package to parse and generate C/C++ code as context aware preprocessor.
Python package to parse and generate C/C++ code as context aware preprocessor. - JhnW/devana
Starred by 18 users
Forked by 6 users
Languages   C++ 51.4% | C 42.9% | Python 5.4% | CMake 0.3% | C++ 51.4% | C 42.9% | Python 5.4% | CMake 0.3%
🌐
Sokol WebGL
floooh.github.io › fips › docs › codegen
Code Generation | Fips Docs
Fips makes it easy to generate C/C++ source code by running Python scripts during the build process. Special care has been taken to make code generation flexible so that it is useful for many different scenarios.
🌐
GitHub
github.com › topics › c-code-generator
c-code-generator · GitHub Topics · GitHub
Code generation tool, creates python / C programs that parse command line arguments.
🌐
GitHub
github.com › SchrodingersGat › C-Snake
GitHub - SchrodingersGat/C-Snake: Python scripts for generating C/C++ code
Python scripts for generating C/C++ code. Contribute to SchrodingersGat/C-Snake development by creating an account on GitHub.
Starred by 4 users
Forked by 2 users
Languages   Python 100.0% | Python 100.0%
🌐
GitHub
github.com › yuchdev › code_generator
GitHub - yuchdev/code_generator: Simple and straightforward code generator for creating program code. At the moment offers support for C++, Java and HTML5 for generating reports.
Simple and straightforward code generator for creating C++ code. It also could be used for generating code in any programming language. Written in Python, works both with Python 2 and 3
Starred by 100 users
Forked by 21 users
Languages   Python 97.9% | C++ 1.9% | C 0.2% | Python 97.9% | C++ 1.9% | C 0.2%
🌐
GitHub
github.com › biojppm › regen
GitHub - biojppm/regen: Easy C++ reflection and code generation
regen is a python3 package providing C/C++ reflection and source-code generation. You provide your own code generation templates , and have full control over where the generated code goes.
Starred by 39 users
Forked by 7 users
Languages   Python 71.2% | C++ 19.4% | CMake 8.9% | Shell 0.5% | Python 71.2% | C++ 19.4% | CMake 8.9% | Shell 0.5%
Find elsewhere
🌐
InfoWorld
infoworld.com › home › software development › programming languages › python
PythoC: A new way to generate C code from Python | InfoWorld
December 19, 2025 - PythoC doesn’t yet have a mechanism for re-using compiled code when it’s called from Python, the way Cython does. At first this seems like a pretty big limitation. But it’s actually the point: You can use PythoC as a code generation system for C programs that run independently, rather than C modules imported into Python.
🌐
GitHub
github.com › bno1 › CRC-C-Generator
GitHub - bno1/CRC-C-Generator: A python script that generates efficient C code which computes a customizable CRC function · GitHub
A python script that generates efficient C code which computes a customizable CRC function - bno1/CRC-C-Generator
Author   bno1
🌐
Google Groups
groups.google.com › g › cython-users › c › XvCV-TKKnEw
Generate C code from Python.
You should also take a look at Shedskin. It only supports a statically analysable subset of Python (and not all stdlib modules are supported), but for what it supports, it generates very good stand-alone code (C++).
🌐
GitHub
github.com › aristanetworks › ctypegen
GitHub - aristanetworks/ctypegen: Generate ctypes boilerplate code from debugging information; Use python to mock C code for testing · GitHub
CTypeGen generates the python "ctypes" boilerplate code to allow you call C functions from python's ctypes, and inspect and construct C structure types from python.
Starred by 29 users
Forked by 10 users
Languages   Python 50.1% | C++ 35.4% | Assembly 6.9% | C 5.8% | Makefile 1.8%
🌐
GitHub
github.com › mypyc › mypyc
GitHub - mypyc/mypyc: Compile type annotated Python to fast C extensions · GitHub
Mypyc compiles Python modules to C extensions. It uses standard Python type hints to generate fast code.
Starred by 2K users
Forked by 48 users
🌐
Stack Overflow
stackoverflow.com › questions › 49153647 › how-to-generate-c-code-from-python
How to generate C code from Python? - Stack Overflow
in: $(IN) $(IN): build/headers/%: src/%.in mkdir -p $(dir $@) PYTHONPATH=.:$$PYTHONPATH mako-render $< > $@ || true · I like this solution because I can easily generate C code from Python results:
Top answer
1 of 8
16

If you want to do this simply with just standard Python stuff you might try making template files that use the Python 3 style string formatting. For example, a class template might look something like this:

{className}::{className}()
{{
}}

{className}::~{className}()
{{
}}

{className}::{className}(const {className}& other)
{{
}}

{className}& {className}::operator=(const {className}& other)
{{
    return *this;
}}

Then your Python code is super simple:

d = {}
d['className'] = 'MyCPlusPlusClassName'
with open(yourTemplateFile, 'r') as ftemp:
    templateString = ftemp.read()
with open(generatedFile, 'w') as f:
    f.write(templateString.format(**d))

Of course you can add lots of other fields alongside 'className' using the same trick. If you don't need stuff like conditional code generation you can get a lot of mileage out of something this simple.

2 of 8
2

I'm afraid you will not find an already-built in solution that takes your particular xml or python files and transforms them onto your required output "out of the box".

You will have to implement the parsing, the data treatment and output yourself. Not all by yourself, though; here are some pointers regarding the parsing and output.

Python comes with 2 different XML parsers (SAX and DOM -scroll down to see some examples). You will have to use one of them in order to read the source files.

For generating the output more easily, you can probably use a templating library, such as StringTemplate, or just generate the code manually, if it's small.

🌐
GitHub
github.com › eloquentarduino › micromlgen
GitHub - eloquentarduino/micromlgen: Generate C code for microcontrollers from Python's sklearn classifiers
May 25, 2024 - Generate C code for microcontrollers from Python's sklearn classifiers - eloquentarduino/micromlgen
Starred by 203 users
Forked by 37 users
Languages   Python 97.8% | Shell 2.2% | Python 97.8% | Shell 2.2%
🌐
Simon Knibbs
s-knibbs.github.io › blog › 2016 › 09 › 15 › 8-ways-to-use-c-code-from-python
8 ways to use C code from Python - Simon Knibbs
September 15, 2016 - Primitive types such as int and char * are automatically converted to the correct python types. Cython is a much more powerful tool than ctypes. Not only can you call C/C++ code from Python, but you can statically compile Python to C code with almost no code modifications.