473,231 Members | 1,866 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,231 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 4866
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve Knight | last post by:
Hello, I'm new to Boost & Python and I'm diving straight in by trying to write an extension module to a third party library. Foolishness probably, but I don't have much choice! My question...
226
by: Stephen C. Waterbury | last post by:
This seems like it ought to work, according to the description of reduce(), but it doesn't. Is this a bug, or am I missing something? Python 2.3.2 (#1, Oct 20 2003, 01:04:35) on linux2 Type...
0
by: Gouda Man | last post by:
I'm planning to write a large program that will have advanced scripting in python in which the scripter will subclass c++ objects to add functionality. this leads me to boost.python (if you dont...
20
by: Todd7 | last post by:
I am looking at learning Python, but I would like to know what its strengths and weaknesses are before I invest much time in learning it. My first thought is what is it good for programming,...
4
by: nik | last post by:
hi, I have a C++ app that loads a python module and calls a function from it. The call is getting the contents of a list that's global in that module. However, I'd like the python script to be...
13
by: Roy Smith | last post by:
I've got a C library with about 50 calls in it that I want to wrap in Python. I know I could use some tool like SWIG, but that will give me a too-literal translation; I want to make some...
25
by: abhinav | last post by:
Hello guys, I am a novice in python.I have to implement a full fledged mail server ..But i am not able to choose the language.Should i go for C(socket API) or python for this project? What are the...
1
by: Max Wilson | last post by:
Hi, Has anyone here built Boost.Python modules under MinGW? I'm trying to build the Boost.Python tutorial under MinGW and getting an error that says it depends on MSVC, which puzzles me because...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.