python,Boost和直接(但复杂)的C代码 [英] python , Boost and straight (but complex) C code
问题描述
我有这些C代码片段(非C ++ !!)我想用Python调用。
我找到了Boost。
我有MS Visual Studio 2005用C ++。
就是这个想法:
我写下面的C源文件:
====== ======================
#include< iostream>
#include< stdafx。 h>
namespace {//避免使全局命名空间混乱。
int my_int; / *全局整数:或名称空间外? * /
双计算(双f)
{
my_int =(int)(f / 2);
printf(%f的一半是%d \ n,f,my_int);
返回f / 2;
}
< br $>
}
#include< boost / python.hpp>
使用命名空间boost :: python;
BOOST_PYTHON_MODULE(一半)
{
def(" calc",calc);
}
================================
我放入VC项目并编译成.DLL
这个DLL放在我的磁盘上,Python 2.4可以找到它。
然后我编写以下Python源代码:
=====================
来自一半导入*
calc(34655.0)
et voila?
我也可以这样访问my_int吗?
I have these pieces of C-code (NOT C++ !!) I want to call from Python.
I found Boost.
I have MS Visual Studio 2005 with C++.
is this the idea:
I write the following C source file:
============================
#include <iostream>
#include <stdafx.h>
namespace { // Avoid cluttering the global namespace.
int my_int; /* a global integer: or outside namespace ? */
double calc ( double f)
{
my_int = (int) (f/2);
printf( "Half of %f is %d\n", f, my_int );
return f/2;
}
}
#include <boost/python.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE( half )
{
def("calc", calc );
}
================================
Which I put in a VC project and compile into a .DLL
This DLL I put somewhere on my disk, where Python 2.4 can find it.
then I write the following Python source:
=====================
from half import *
calc(34655.0)
et voila ?
Can I acces my_int too, this way ?
推荐答案
我从Visual C ++中得到这些链接器错误,总共8个,试图建立上面的C ++
来源:
C_test.obj:错误LNK2019:未解析的外部符号
" __ declspec(dllimport)void __cdecl
boost: :python :: detail :: init_module(char const *,void
(__ cdecl *)(void))"
(__ imp_?init_module @ detail @ python @ boost @@ YAXPBDP6A XXZ @ Z)参考
函数_inithalf
C_test.obj:错误LNK2019:未解析的外部符号
" ; __ declspec(dllimport)void __cdecl
boost :: python :: detail :: scope_setattr_doc(char const *,class
boost :: python :: api :: object const &,char const *)"
(__ imp_?scope_setattr_doc @ detail @ python @ boost @@ YAX PBDABVobject @ api @ 23 @ 0 @ Z)
引用function" void __cdecl boost :: python :: def< int
(__ cdecl *)(float)>(char const *,int(__ cdecl *)(float))"
(??
I get, from Visual C++, these linker errors, 8 in total, trying to
build the above C++ source:
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) void __cdecl
boost::python::detail::init_module(char const *,void
(__cdecl*)(void))"
(__imp_?init_module@detail@python@boost@@YAXPBDP6A XXZ@Z) referenced in
function _inithalf
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) void __cdecl
boost::python::detail::scope_setattr_doc(char const *,class
boost::python::api::object const &,char const *)"
(__imp_?scope_setattr_doc@detail@python@boost@@YAX PBDABVobject@api@23@0@Z)
referenced in function "void __cdecl boost::python::def<int
(__cdecl*)(float)>(char const *,int (__cdecl*)(float))"
(??
def @ P6AHM @ Z @ python @ boost @@ YAXPBDP6AHM @ Z @ Z)
C_test。 obj:错误LNK2019:未解析的ex ternal符号
" __ declspec(dllimport)类boost :: python :: api :: object __cdecl
boost :: python :: objects :: function_object(struct
boost :: python :: objects :: py_function const&)"
(__ imp_?function_object @ objects @ python @ boost @@ YA?Vobject @ api @ 23 @ ABUpy_function @ 123 @@ Z)
在函数中引用类boost :: python :: api :: object __cdecl
boost :: python :: detail :: make_function_aux< int(__ cdecl *)(float),struct
boost :: python :: default_call_policies,struct
boost :: mpl :: vector2< int,float(int (__cdecl *)(float),struct
boost :: python :: default_call_policies const&,struct
boost :: mpl :: vector2< int,floatconst& )
(??
def@P6AHM@Z@python@boost@@YAXPBDP6AHM@Z@Z)
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) class boost::python::api::object __cdecl
boost::python::objects::function_object(struct
boost::python::objects::py_function const &)"
(__imp_?function_object@objects@python@boost@@YA?A Vobject@api@23@ABUpy_function@123@@Z)
referenced in function "class boost::python::api::object __cdecl
boost::python::detail::make_function_aux<int (__cdecl*)(float),struct
boost::python::default_call_policies,struct
boost::mpl::vector2<int,float(int (__cdecl*)(float),struct
boost::python::default_call_policies const &,struct
boost::mpl::vector2<int,floatconst &)"
(??
make_function_aux @ P6AHM @ ZUdefault_call_policie s @ python @ boost @@ U?
make_function_aux@P6AHM@ZUdefault_call_policie s@python@boost@@U?
这篇关于python,Boost和直接(但复杂)的C代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!