python , Boost and straight (but complex) C code

O

Osiris

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 ?
 
O

Osiris

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@@YAXPBDP6AXXZ@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::eek:bject const &,char const *)"
(__imp_?scope_setattr_doc@detail@python@boost@@YAXPBDABVobject@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::eek:bject __cdecl
boost::python::eek:bjects::function_object(struct
boost::python::eek:bjects::py_function const &)"
(__imp_?function_object@objects@python@boost@@YA?AVobject@api@23@ABUpy_function@123@@Z)
referenced in function "class boost::python::api::eek:bject __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,float> const &)"
(??$make_function_aux@P6AHM@ZUdefault_call_policies@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::eek:bjects::py_function_impl_base::py_function_impl_base(void)"
(__imp_??0py_function_impl_base@objects@python@boost@@QAE@XZ)
referenced in function "public: __thiscall
boost::python::eek:bjects::caller_py_function_impl<struct
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@ZUdefault_call_policies@python@boost@@U?$vector2@HM@mpl@3@@detail@python@boost@@@objects@python@boost@@QAE@ABU?$caller@P6AHM@ZUdefault_call_policies@python@boost@@U?$vector2@HM@mpl@3@@detail@23@@Z)
C_test.obj : error LNK2001: unresolved external symbol "public:
virtual unsigned int __thiscall
boost::python::eek:bjects::py_function_impl_base::max_arity(void)const "
(?max_arity@py_function_impl_base@objects@python@boost@@UBEIXZ)
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) public: virtual __thiscall
boost::python::eek:bjects::py_function_impl_base::~py_function_impl_base(void)"
(__imp_??1py_function_impl_base@objects@python@boost@@UAE@XZ)
referenced in function "public: virtual __thiscall
boost::python::eek:bjects::caller_py_function_impl<struct
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@ZUdefault_call_policies@python@boost@@U?$vector2@HM@mpl@3@@detail@python@boost@@@objects@python@boost@@UAE@XZ)
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dllimport) struct
boost::python::converter::rvalue_from_python_stage1_data __cdecl
boost::python::converter::rvalue_from_python_stage1(struct _object
*,struct boost::python::converter::registration const &)"
(__imp_?rvalue_from_python_stage1@converter@python@boost@@YA?AUrvalue_from_python_stage1_data@123@PAU_object@@ABUregistration@123@@Z)
referenced in function "public: __thiscall
boost::python::converter::arg_rvalue_from_python<float>::arg_rvalue_from_python<float>(struct
_object *)"
(??0?$arg_rvalue_from_python@M@converter@python@boost@@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@@YAABUregistration@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
 
S

Sebastian 'lunar' Wiesner

Osiris said:
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 <iostream> with
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...
 
E

Erik Max Francis

Osiris said:
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.
 
R

Roman Yakovenko


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.
 
O

Osiris

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...
 
E

Erik Max Francis

Osiris said:
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.
 
C

Christophe Cavalaria

Osiris said:
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.
 
O

Osiris

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....
 
O

Osiris

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...
 
R

Roman Yakovenko

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top