Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 46003c8

Browse files
committedFeb 22, 2025
Add C/C++ extension module with example functions and test script
1 parent 203d70e commit 46003c8

File tree

3 files changed

+85
-0
lines changed

3 files changed

+85
-0
lines changed
 

‎setup.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from setuptools import setup, Extension
2+
3+
# C/C++ extension module configuration
4+
module = Extension(
5+
"pycmtemplate_c",
6+
sources=["src/main.cpp"],
7+
include_dirs=[],
8+
extra_compile_args=["-std=c++11"],
9+
)
10+
11+
setup(
12+
name="pycmtemplate",
13+
version="0.0.1",
14+
description="Python C template",
15+
long_description="Python C/C++ template project",
16+
author="Etkin Dogan",
17+
author_email="etkindogan@gmail.com",
18+
python_requires=">=3.8",
19+
ext_modules=[module],
20+
)

‎src/main.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include <Python.h>
2+
3+
// CPP functions that will be called from Python
4+
static PyObject *functionFromCPP(PyObject *self, PyObject *args)
5+
{
6+
printf("This message comes from CPP\n");
7+
return Py_BuildValue("s", "Hello from CPP");
8+
}
9+
10+
static PyObject *functionFromCPP2(PyObject *self, PyObject *args)
11+
{
12+
int a, b;
13+
if (!PyArg_ParseTuple(args, "ii", &a, &b))
14+
return NULL;
15+
printf("The sum of %d and %d is %d\n", a, b, a + b);
16+
return Py_BuildValue("i", a + b);
17+
}
18+
19+
// CPP Variables that will be used in Python
20+
static PyObject *variableFromCPP = PyLong_FromLong(42);
21+
static PyObject *variableFromCPP2 = Py_BuildValue("s", "Hello from CPP variable 2");
22+
23+
// Method definition structure
24+
static PyMethodDef MyMethods[] = {
25+
// Method definition structure
26+
{"functionFromCPP", functionFromCPP, METH_VARARGS, "Function from CPP"},
27+
{"functionFromCPP2", functionFromCPP2, METH_VARARGS, "Function from CPP with arguments"},
28+
{NULL, NULL, 0, NULL} // Sentinel
29+
};
30+
31+
// Module definition to register the module
32+
static struct PyModuleDef etkindmodule = {
33+
PyModuleDef_HEAD_INIT,
34+
"pycmtemplate_c", // Module name
35+
NULL, // Module documentation
36+
-1, // Size of per-interpreter state
37+
MyMethods};
38+
39+
// Module initialization
40+
PyMODINIT_FUNC PyInit_pycmtemplate_c(void)
41+
{
42+
// Create the module
43+
PyObject *module = PyModule_Create(&etkindmodule);
44+
if (module == NULL)
45+
return NULL;
46+
47+
// Add variables to the module
48+
PyModule_AddObject(module, "variableFromCPP", variableFromCPP);
49+
PyModule_AddObject(module, "variableFromCPP2", variableFromCPP2);
50+
51+
return module;
52+
}

‎tests/test1.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import pycmtemplate_c
2+
3+
print("Function calls")
4+
r1 = pycmtemplate_c.functionFromCPP()
5+
r2 = pycmtemplate_c.functionFromCPP2(3, 5)
6+
7+
print("Print results")
8+
print(r1)
9+
print(r2)
10+
11+
print("Print variables")
12+
print(pycmtemplate_c.variableFromCPP)
13+
print(pycmtemplate_c.variableFromCPP2)

0 commit comments

Comments
 (0)
Please sign in to comment.