472,098 Members | 2,561 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,098 software developers and data experts.

python , Boost and straight (but complex) C code

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 ?

Dec 30 '06 #1
11 4735
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 : 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?$vector2@HM@mpl@3@@detail@python @boost@@YA?AVobject@api@12@P6AHM@ZABUdefault_call_ policies@12@ABU?$vector2@HM@mpl@2@@Z)
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) public: __thiscall
boost::python::objects::py_function_impl_base::py_ function_impl_base(void)"
(__imp_??0py_function_impl_base@objects@python@boo st@@QAE@XZ)
referenced in function "public: __thiscall
boost::python::objects::caller_py_function_impl<st ruct
boost::python::detail::caller<int (__cdecl*)(float),struct
boost::python::default_call_policies,struct
boost::mpl::vector2<int,float >::caller_py_function_impl<struct
boost::python::detail::caller<int (__cdecl*)(float),struct
boost::python::default_call_policies,struct
boost::mpl::vector2<int,float >(struct
boost::python::detail::caller<int (__cdecl*)(float),struct
boost::python::default_call_policies,struct
boost::mpl::vector2<int,float const &)"
(??0?$caller_py_function_impl@U?$caller@P6AHM@ZUde fault_call_policies@python@boost@@U?$vector2@HM@mp l@3@@detail@python@boost@@@objects@python@boost@@Q AE@ABU?$caller@P6AHM@ZUdefault_call_policies@pytho n@boost@@U?$vector2@HM@mpl@3@@detail@23@@Z)
C_test.obj : error LNK2001: unresolved external symbol "public:
virtual unsigned int __thiscall
boost::python::objects::py_function_impl_base::max _arity(void)const "
(?max_arity@py_function_impl_base@objects@python@b oost@@UBEIXZ)
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) public: virtual __thiscall
boost::python::objects::py_function_impl_base::~py _function_impl_base(void)"
(__imp_??1py_function_impl_base@objects@python@boo st@@UAE@XZ)
referenced in function "public: virtual __thiscall
boost::python::objects::caller_py_function_impl<st ruct
boost::python::detail::caller<int (__cdecl*)(float),struct
boost::python::default_call_policies,struct
boost::mpl::vector2<int,float >::~caller_py_function_impl<struct
boost::python::detail::caller<int (__cdecl*)(float),struct
boost::python::default_call_policies,struct
boost::mpl::vector2<int,float >(void)"
(??1?$caller_py_function_impl@U?$caller@P6AHM@ZUde fault_call_policies@python@boost@@U?$vector2@HM@mp l@3@@detail@python@boost@@@objects@python@boost@@U AE@XZ)
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) struct
boost::python::converter::rvalue_from_python_stage 1_data __cdecl
boost::python::converter::rvalue_from_python_stage 1(struct _object
*,struct boost::python::converter::registration const &)"
(__imp_?rvalue_from_python_stage1@converter@python @boost@@YA?AUrvalue_from_python_stage1_data@123@PA U_object@@ABUregistration@123@@Z)
referenced in function "public: __thiscall
boost::python::converter::arg_rvalue_from_python<f loat>::arg_rvalue_from_python<float>(struct
_object *)"
(??0?$arg_rvalue_from_python@M@converter@python@bo ost@@QAE@PAU_object@@@Z)
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) struct boost::python::converter::registration
const & __cdecl boost::python::converter::registry::lookup(struct
boost::python::type_info)"
(__imp_?lookup@registry@converter@python@boost@@YA ABUregistration@234@Utype_info@34@@Z)
referenced in function "struct boost::python::converter::registration
const & __cdecl
boost::python::converter::detail::registry_lookup< float const volatile
>(float const volatile & (__cdecl*)(void))"
(??$registry_lookup@$$CDM@detail@converter@python@ boost@@YAABUregistration@123@P6AADMXZ@Z)
C:\Documents and Settings\Erik\My Documents\Visual Studio
2005\Projects\C_test\Debug\C_test.dll : fatal error LNK1120: 8
unresolved externals
Dec 30 '06 #2
Osiris <no**@hotmail.comtyped
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>
iostream is a C++ header file...
#include <stdafx.h>

namespace { // Avoid cluttering the global namespace.
C doesn't know about namespaces. They are a C++ thing, too.
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 );
You include a C++ IO header, but use traditional C IO functions here.
Either you use C++ streams here, or you replace <iostreamwith
<stdio.h>.
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 ?
May be... Didn't you try it?
It may also be, that you have to create a PyObject first...

--
Freedom is always the freedom of dissenters.
(Rosa Luxemburg)
Dec 30 '06 #3
Visual C++ build log at:

http://213.10.133.192/BuildLog.htm
Dec 30 '06 #4
Osiris wrote:
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.
iostream and namespaces are both most definitely C++ features, not C.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
It's almost like / I didn't even have a choice
-- India Arie
Dec 30 '06 #5
On 12/30/06, Osiris <no**@hotmail.comwrote:
Visual C++ build log at:

http://213.10.133.192/BuildLog.htm
It is better to ask Boost.Python related questions on it mailing list:
http://mail.python.org/mailman/listinfo/c++-sig/

You should add to the link line boost_python.lib, thus you will eliminate
"unresolved reference symbol" errors. If you did not build Boost.Python
next page( http://boost.org/libs/python/doc/building.html ) contains pretty
good explanation how to do this.

http://boost.org/libs/python/doc/v2/scope.html - here you will find example how
to expose my_int variable to Python. my_int has type int, so C++ code will not
see changes to the variable that are done from Python. You will have to write
set_my_int( x ) function to achieve this.

If you are new to Boost.Python try to use Py++ - the code generator for
the library. The Py++ GUI( http://tinyurl.com/ycwvwo ) will help you
to start\learn Boost.Python.

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
Dec 30 '06 #6
On Sat, 30 Dec 2006 13:19:28 -0800, Erik Max Francis <ma*@alcyone.com>
wrote:
>Osiris wrote:
>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.

iostream and namespaces are both most definitely C++ features, not C.
yes, but C can be compiled with a C++ compiler, One can put C code in
C++ source.... Boost should not complain... should it ?
Boost text is all about C++.. so... C should not be a problem...
Dec 30 '06 #7
Osiris wrote:
yes, but C can be compiled with a C++ compiler, One can put C code in
C++ source.... Boost should not complain... should it ?
Boost text is all about C++.. so... C should not be a problem...
That you're dealing with a `boost` namespace below clearly indicates
that you're dealing with C++ code throughout, not C, contrary to what
you had claimed. C and C++ are not the same language.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
The enemy of my enemy is my friend.
-- (an Arab proverb)
Dec 30 '06 #8
Osiris wrote:
On Sat, 30 Dec 2006 13:19:28 -0800, Erik Max Francis <ma*@alcyone.com>
wrote:
>>Osiris wrote:
>>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.

iostream and namespaces are both most definitely C++ features, not C.

yes, but C can be compiled with a C++ compiler, One can put C code in
C++ source.... Boost should not complain... should it ?
Boost text is all about C++.. so... C should not be a problem...
Not all C code can be compiled by a C++ compiler. And anyway, this is
definitively NOT C code.
Dec 31 '06 #9
On Sun, 31 Dec 2006 01:01:29 +0100, Christophe Cavalaria
<ch*************@free.frwrote:
>Osiris wrote:
>On Sat, 30 Dec 2006 13:19:28 -0800, Erik Max Francis <ma*@alcyone.com>
wrote:
>>>Osiris wrote:

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.

iostream and namespaces are both most definitely C++ features, not C.

yes, but C can be compiled with a C++ compiler, One can put C code in
C++ source.... Boost should not complain... should it ?
Boost text is all about C++.. so... C should not be a problem...

Not all C code can be compiled by a C++ compiler. And anyway, this is
definitively NOT C code.
ok ok,
but the examples of the application of boost only show how to do C++.
So I followed those examples and try to embed my C code in the
framework of the examples...
Which seems not to be without merit, because my example here compiles
ok, but the linking into a DLL goes wrong, looking at the log.
I told VC where the boost libs are, and still....
Dec 31 '06 #10
On Sat, 30 Dec 2006 23:35:22 +0200, "Roman Yakovenko"
<ro*************@gmail.comwrote:
>On 12/30/06, Osiris <no**@hotmail.comwrote:
>Visual C++ build log at:

http://213.10.133.192/BuildLog.htm

It is better to ask Boost.Python related questions on it mailing list:
http://mail.python.org/mailman/listinfo/c++-sig/

You should add to the link line boost_python.lib, thus you will eliminate
"unresolved reference symbol" errors. If you did not build Boost.Python
next page( http://boost.org/libs/python/doc/building.html ) contains pretty
good explanation how to do this.
That explanation I find confusing:
"To build boost_python, use Boost.Build...": Where is boost.build and
what is it ?
in my D:\boost\libs\python\build there is no boost.build...

in D:\boost there is a build-boost.jam file.... Is that it ?

I have a D:\boost\libs\python\build\bin-stage\boost_python.dll (204kB
size) and a D:\boost\libs\python\build\bin-stage\boost_build.lib (163
KB)
I made them with Visual Studio...
I made a bjam, it is in C:\

In D:\boost\libs\python\build there are a bin_stage and a
VisualStudio folder , the files
jamfile
jamfile.v2
python_v1.zip

Is it so, that I can build the boost_python.dll and lib, either with
bjam OR with VisualStudio C++ ?

Why are all those other folders there in d:\boost ? people,
regression, tools, status etc ?

What do I do with d:\boost\boost ? a lot of headers there...

what are all these folders in d:\boost\libs\ ?

In short: it's all rather confusing....

I think it must be like this:

To use my C/C++ code with Python, add some stuff in the C/C++ source
and compile it into a DLL, that must be combined with some boost-DLL
to make it accessible to Python 2.4.
Therefore I need a boost DLL from boost.org, and some headerfiles that
belong to the boost DLL.
(where are those boost header files )

As you see, a lot of obfuscation....
MAybe I see too many bears on the road...
>
http://boost.org/libs/python/doc/v2/scope.html - here you will find example how
to expose my_int variable to Python. my_int has type int, so C++ code will not
see changes to the variable that are done from Python. You will have to write
set_my_int( x ) function to achieve this.

If you are new to Boost.Python try to use Py++ - the code generator for
the library. The Py++ GUI( http://tinyurl.com/ycwvwo ) will help you
to start\learn Boost.Python.
Dec 31 '06 #11
On 12/31/06, Osiris <no**@hotmail.comwrote:
In short: it's all rather confusing....

I think it must be like this:

To use my C/C++ code with Python, add some stuff in the C/C++ source
and compile it into a DLL, that must be combined with some boost-DLL
to make it accessible to Python 2.4.
Therefore I need a boost DLL from boost.org, and some headerfiles that
belong to the boost DLL.
(where are those boost header files )

As you see, a lot of obfuscation....
MAybe I see too many bears on the road...
Boost project is an open source, consider to contribute your
experience, knowledge
and time to make it better.

You can download pre-built binaries for Windows platform from here:
http://www.boost-consulting.com/download.html

--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
Dec 31 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

226 posts views Thread by Stephen C. Waterbury | last post: by
reply views Thread by Gouda Man | last post: by
20 posts views Thread by Todd7 | last post: by
4 posts views Thread by nik | last post: by
13 posts views Thread by Roy Smith | last post: by
25 posts views Thread by abhinav | last post: by
1 post views Thread by Max Wilson | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.