TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Cool method to use LLM to build cross language interfaces

4 点作者 vadiml10 个月前
Here is a cool tip on how to use hashtag#LLM to build cross-language interfaces. I did it with hashtag#ChatGPT-4o but it will probably work with others.<p>Here is my prompt: I need to use following c++ class from python code: class TestIt { public: std::string f1, f2;<p>std::string getf1() const { return f1; } };<p>And here is what I get as output: To use a C++ class from Python, you can use a tool like `pybind11`, which allows you to create Python bindings for C++ code. Here is a step-by-step guide to expose your `TestIt` class to Python:<p>### Step 1: Write the C++ Code<p>First, let&#x27;s create a header file (`test_it.h`) and an implementation file (`test_it.cpp`) for your C++ class:<p>*test_it.h:*<p>```cpp hashtag#ifndef TEST_IT_H hashtag#define TEST_IT_H<p>hashtag#include &lt;string&gt;<p>class TestIt { public: std::string f1, f2;<p>std::string getf1() const { return f1; } };<p>hashtag#endif &#x2F;&#x2F; TEST_IT_H ```<p>*test_it.cpp:*<p>```cpp hashtag#include &quot;test_it.h&quot;<p>```<p>### Step 2: Write the pybind11 Binding Code<p>Create a new C++ file for the pybind11 bindings, say `bindings.cpp`:<p>*bindings.cpp:*<p>```cpp hashtag#include &lt;pybind11&#x2F;pybind11.h&gt; hashtag#include &lt;pybind11&#x2F;stl.h&gt; hashtag#include &quot;test_it.h&quot;<p>namespace py = pybind11;<p>PYBIND11_MODULE(testit, m) { py::class_&lt;TestIt&gt;(m, &quot;TestIt&quot;) .def(py::init&lt;&gt;()) &#x2F;&#x2F; Bind the default constructor .def_readwrite(&quot;f1&quot;, &amp;TestIt::f1) &#x2F;&#x2F; Bind the public member f1 .def_readwrite(&quot;f2&quot;, &amp;TestIt::f2) &#x2F;&#x2F; Bind the public member f2 .def(&quot;getf1&quot;, &amp;TestIt::getf1); &#x2F;&#x2F; Bind the getf1 method } ```<p>### Step 3: Build the Python Module<p>To compile the code, you need to have `pybind11` installed and properly set up. You can install `pybind11` using pip:<p>```sh pip install pybind11 ```<p>Create a `CMakeLists.txt` file to handle the build process:<p>*CMakeLists.txt:*<p>```cmake cmake_minimum_required(VERSION 3.10) project(testit)<p>set(CMAKE_CXX_STANDARD 14)<p>find_package(pybind11 REQUIRED)<p>add_library(testit MODULE bindings.cpp test_it.cpp) target_link_libraries(testit PRIVATE pybind11::module) ```<p>### Step 4: Compile the Module<p>Run the following commands to compile your module:<p>```sh mkdir build cd build cmake .. make ```<p>This will generate a shared library file (`testit.so` on Linux, `testit.pyd` on Windows) that you can import in Python.<p>### Step 5: Use the Module in Python<p>Finally, you can use your C++ class in Python:<p>*test.py:*<p>```python import testit<p># Create an instance of TestIt test_instance = testit.TestIt()<p># Set values for f1 and f2 test_instance.f1 = &quot;Hello&quot; test_instance.f2 = &quot;World&quot;<p># Get the value of f1 using the method print(test_instance.getf1()) # Output: Hello<p># Print the value of f2 directly print(test_instance.f2) # Output: World ```<p>### Additional Notes<p>- The `pybind11` library should be correctly installed and discoverable by CMake. - Adjust the `CMakeLists.txt` if you have additional include directories or dependencies.<p>By following these steps, you should be able to use your C++ class `TestIt` in Python seamlessly.

暂无评论

暂无评论