我可以在 c++ 中使用带有 cython 的动态库编译吗?
Can I use dynamic library compile with cython in c++?
我有一个cython
文件random.pyx
如下所示:
cdef public int get_random_number():
return 4
像这样的setup.py
:
from distutils.core import setup
from Cython.Build import cythonize
from distutils.extension import Extension
extensions = [Extension("librandom", ["random.pyx"])]
setup(
ext_modules = cythonize(extensions)
)
然后我得到了一个动态库librandom.so
,现在我想在 c++ 而不是 python 中使用这个so
文件。
#include <stdio.h>
#include "random.h"
int main() {
printf("%dn",get_random_number());
return 0;
}
现在我在编译时收到这样的错误g++ -o main main.cpp -lrandom -L. -Wl,-rpath,"$ORIGIN"
:
In file included from main.cpp:2:0:
random.h:26:1: error: ‘PyMODINIT_FUNC’ does not name a type
PyMODINIT_FUNC initrandom(void);
尝试将 c 代码更改为:
#include <stdio.h>
#include "Python.h"
#include "random.h"
int main() {
Py_Initialize();
PyInit_random(); // see "random.h"
int r = get_random_number();
Py_Finalize();
printf("%dn", r);
return 0;
}
请注意,要运行可执行文件,您无法摆脱 python 环境。
另请参阅如何将 Cython 生成的模块从 python 导入到 C/C++ 主文件?(C/C++编程)
相关文章:
- 这是使用回溯的 nqueen 问题,但我使用了动态 2d 数组,我的程序编译良好,但不返回任何输出
- 仅使用生成文件动态编译选定的文件
- 动态变量 cpp 编译
- 我正在尝试使用回溯来解决 N queen 问题,但在编译时它会给出运行时错误(动态堆栈缓冲区溢出)
- 在为 Java 编译动态链接文件时,我应该选择哪些 g++ 参数
- 比格犬骨交叉编译动态链接混乱
- 如何访问从C 编译的动态库中定义的全局变量
- 动态数组和静态数组编译时或运行时
- 为什么和如何C++允许使用动态值创建编译时数组
- 编译针对静态或动态库的c++链接
- 我可以在 c++ 中使用带有 cython 的动态库编译吗?
- 我可以动态地跳过一系列代码,而不会使用断点在Visual Studio中进行重新编译
- 编译时动态函数调用
- 编译C++中动态链接库,存在运行路径问题
- CMAKE:阅读和编译CPP文件的动态生成列表
- 可以动态分配和在C 的编译时分配的最大存储器
- 指向动态分配的boost multi_array中的类的指针,而不是编译
- 动态编译时基类
- 使用G++动态编译C++代码片段
- 根据 #define 动态编译文件