From e5f835f5de55598a65cfd6e0b68551acccdc78d2 Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Mon, 28 Jul 2025 09:16:46 +0800 Subject: [PATCH] update --- .../example_1/a.py | 9 +++++++++ .../example_2/a.py | 8 ++++++++ .../example_2/example.c | 3 +++ 2025.07.28_programming_with_python_and_cpp/a.py | 4 ++++ .../example.cpp | 12 ++++++++++++ .../setup.py | 14 ++++++++++++++ 6 files changed, 50 insertions(+) create mode 100644 2025.07.27_programming_with_python_and_c/example_1/a.py create mode 100644 2025.07.27_programming_with_python_and_c/example_2/a.py create mode 100644 2025.07.27_programming_with_python_and_c/example_2/example.c create mode 100644 2025.07.28_programming_with_python_and_cpp/a.py create mode 100644 2025.07.28_programming_with_python_and_cpp/example.cpp create mode 100644 2025.07.28_programming_with_python_and_cpp/setup.py diff --git a/2025.07.27_programming_with_python_and_c/example_1/a.py b/2025.07.27_programming_with_python_and_c/example_1/a.py new file mode 100644 index 0000000..eac6cdf --- /dev/null +++ b/2025.07.27_programming_with_python_and_c/example_1/a.py @@ -0,0 +1,9 @@ +from ctypes import cdll + +lib = cdll.LoadLibrary("libc.so.6") # Linux 系统 +# lib = cdll.msvcrt # Windows 系统 + +# 调用 C 标准库中的 printf 函数,字符串需要编码为 bytes +message = b"Hello, ctypes!\n" +result = lib.printf(message) +print(result) \ No newline at end of file diff --git a/2025.07.27_programming_with_python_and_c/example_2/a.py b/2025.07.27_programming_with_python_and_c/example_2/a.py new file mode 100644 index 0000000..54b43bf --- /dev/null +++ b/2025.07.27_programming_with_python_and_c/example_2/a.py @@ -0,0 +1,8 @@ +from ctypes import cdll + +lib = cdll.LoadLibrary('./example.so') # Linux 系统 +# lib = cdll.LoadLibrary('./example.dll') # Windows 系统 + +# 调用 C 函数 +result = lib.add_two_numbers(3, 5) +print(result) \ No newline at end of file diff --git a/2025.07.27_programming_with_python_and_c/example_2/example.c b/2025.07.27_programming_with_python_and_c/example_2/example.c new file mode 100644 index 0000000..36b5c06 --- /dev/null +++ b/2025.07.27_programming_with_python_and_c/example_2/example.c @@ -0,0 +1,3 @@ +int add_two_numbers(int a, int b) { + return a + b; +} \ No newline at end of file diff --git a/2025.07.28_programming_with_python_and_cpp/a.py b/2025.07.28_programming_with_python_and_cpp/a.py new file mode 100644 index 0000000..7c2ad52 --- /dev/null +++ b/2025.07.28_programming_with_python_and_cpp/a.py @@ -0,0 +1,4 @@ +import example + +print(example.hello()) +print(example.add(3, 5)) \ No newline at end of file diff --git a/2025.07.28_programming_with_python_and_cpp/example.cpp b/2025.07.28_programming_with_python_and_cpp/example.cpp new file mode 100644 index 0000000..156ce35 --- /dev/null +++ b/2025.07.28_programming_with_python_and_cpp/example.cpp @@ -0,0 +1,12 @@ +#include + +int add(int a, int b) { + return a + b; +} + +PYBIND11_MODULE(example, m) { + m.def("add", &add, "A function that adds two numbers"); + m.def("hello", []() { + return "Hello from C++!"; + }); +} \ No newline at end of file diff --git a/2025.07.28_programming_with_python_and_cpp/setup.py b/2025.07.28_programming_with_python_and_cpp/setup.py new file mode 100644 index 0000000..729bfbe --- /dev/null +++ b/2025.07.28_programming_with_python_and_cpp/setup.py @@ -0,0 +1,14 @@ +from setuptools import setup, Extension +import pybind11 + +setup( + name='example', + ext_modules=[ + Extension( + 'example', + ['example.cpp'], + include_dirs=[pybind11.get_include()], + language='c++', + ), + ], +) \ No newline at end of file