在 C++ 中,如何在运行时打印变体中包含的类型?
我的用例:使用pybind11将值字典从 Python 传递到 C++ ,我想打印出收到的类型。
我有定义一个curves类、一个curve类和一个point类的C++ 代码,我正在尝试通过 pybind11 为这些类编写 Python 绑定并在 Python 中使用它们。
这些类的 pybind11 绑定如下所示:
namespace py = pybind11;
PYBIND11_MODULE(mymodule, m) {
py::class_<_point>(m, "_point")
.def(py::init<double, double>())
.def_readwrite("next", &point::next)
.def_readwrite("prev", &point::prev)
.def_readonly("x1", &point::x1)
.def_readonly("x2", &point::x2);
py::class_<curve>(m, "curve")
.def(py::init<point*>()) //constructor 1
.def(py::init()) //constructor 2
.def_readwrite("first", &curve::first)
.def_readwrite("last", &curve::last)
.def_readwrite("next", &curve::next)
.def_readwrite("prev", &curve::prev);
py::class_<curves>(m, "curves")
.def(py::init())
.def_readwrite("first", &curves::first)
.def_readwrite("last", &curves::last);
}
Run Code Online (Sandbox Code Playgroud)
在 C++ 中,我可以通过以下方式遍历一个由curves对象curve
组成的point对象:
for(curve *c=curves_pointer->first; c; c=c->next) {
for(point *p=c->first; p; p=p->next) {
cout …Run Code Online (Sandbox Code Playgroud) 在升级 MacOS 之前,我能够在此处安装 pybind11 示例。但是,在我将 MacOS 升级到 Mojave 后,当我在该链接上编译相同的示例时,我看到以下错误:
clang: warning: libstdc++ is deprecated; move to libc++ with a minimum deployment target of OS X 10.9 [-Wdeprecated]
ld: library not found for -lstdc++
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: command 'g++' failed with exit status 1
Run Code Online (Sandbox Code Playgroud)
我缺少链接器命令吗?我在网上搜索过但找不到解决方案。
I am making code in python using pybind11.
For example, I have test1.cpp, test_pybind.cpp and example.py
heaan_pybind.cpp
namespace py=pybind11;
PYBIND11_MODULE(test, m)
{
py::class_<test1>(m, "test1")
.def("fn", ...)
}
Run Code Online (Sandbox Code Playgroud)
And in example.py, I want to control the exception like below.
import test
while True:
try:
test.test1.fn(...)
except ???:
print("exception error.")
Run Code Online (Sandbox Code Playgroud)
如何在 example.py 中从 test.cpp 中捕获错误或异常?
我有一个具有以下结构的示例。
??? CMakeLists.txt
??? ext
? ??? pybind11
??? main.cpp
Run Code Online (Sandbox Code Playgroud)
CMakeLists.txt
cmake_minimum_required(VERSION 3.5)
project(notworking)
add_subdirectory(ext/pybind11)
add_executable(notworking
main.cpp)
target_link_libraries(notworking PRIVATE python3.6m)
target_link_libraries(notworking PRIVATE pybind11)
Run Code Online (Sandbox Code Playgroud)
主程序
#include <pybind11/pybind11.h>
namespace py = pybind11;
int main() { py::object decimal = py::module::import("decimal"); }
Run Code Online (Sandbox Code Playgroud)
现在在运行时
?? ./notworking
[1] 14879 segmentation fault (core dumped) ./notworking
Run Code Online (Sandbox Code Playgroud)
我错过了什么才能让这个模块在这里正确加载?我已经搜索了文档,特别是构建系统部分,但发现是空的。
在 C++ 的 pybind11 中使用其他包装器时,对我来说似乎也是这种情况。
我正在使用 cppimport,这是一个帮助将 cpp 导入 python 的库。如此处所述:https : //github.com/tbenthompson/cppimport,必须将 setup_pybind11 放入/* */cpp 文件中。
/*
<%
setup_pybind11(cfg)
%>
*/
Run Code Online (Sandbox Code Playgroud)
看来,当我/* */按照文档中的描述将其放入时,从 python 导入 cpp 文件时它没有任何影响。但是,当我不使用/* */. 有什么建议我可以做什么?为什么我必须/* */从 python 中删除它才能正常工作?