473,695 Members | 2,708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_MO DULE( 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 4911
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(dll import) void __cdecl
boost::python:: detail::init_mo dule(char const *,void
(__cdecl*)(void ))"
(__imp_?init_mo dule@detail@pyt hon@boost@@YAXP BDP6AXXZ@Z) referenced in
function _inithalf

C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dll import) void __cdecl
boost::python:: detail::scope_s etattr_doc(char const *,class
boost::python:: api::object const &,char const *)"
(__imp_?scope_s etattr_doc@deta il@python@boost @@YAXPBDABVobje ct@api@23@0@Z)
referenced in function "void __cdecl boost::python:: def<int
(__cdecl*)(floa t)>(char const *,int (__cdecl*)(floa t))"
(??$def@P6AHM@Z @python@boost@@ YAXPBDP6AHM@Z@Z )

C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dll import) class boost::python:: api::object __cdecl
boost::python:: objects::functi on_object(struc t
boost::python:: objects::py_fun ction const &)"
(__imp_?functio n_object@object s@python@boost@ @YA?AVobject@ap i@23@ABUpy_func tion@123@@Z)
referenced in function "class boost::python:: api::object __cdecl
boost::python:: detail::make_fu nction_aux<int (__cdecl*)(floa t),struct
boost::python:: default_call_po licies,struct
boost::mpl::vec tor2<int,float( int (__cdecl*)(floa t),struct
boost::python:: default_call_po licies const &,struct
boost::mpl::vec tor2<int,floatc onst &)"
(??$make_functi on_aux@P6AHM@ZU default_call_po licies@python@b oost@@U?$vector 2@HM@mpl@3@@det ail@python@boos t@@YA?AVobject@ api@12@P6AHM@ZA BUdefault_call_ policies@12@ABU ?$vector2@HM@mp l@2@@Z)
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dll import) public: __thiscall
boost::python:: objects::py_fun ction_impl_base ::py_function_i mpl_base(void)"
(__imp_??0py_fu nction_impl_bas e@objects@pytho n@boost@@QAE@XZ )
referenced in function "public: __thiscall
boost::python:: objects::caller _py_function_im pl<struct
boost::python:: detail::caller< int (__cdecl*)(floa t),struct
boost::python:: default_call_po licies,struct
boost::mpl::vec tor2<int,float >::caller_py_fu nction_impl<str uct
boost::python:: detail::caller< int (__cdecl*)(floa t),struct
boost::python:: default_call_po licies,struct
boost::mpl::vec tor2<int,float >(struct
boost::python:: detail::caller< int (__cdecl*)(floa t),struct
boost::python:: default_call_po licies,struct
boost::mpl::vec tor2<int,float const &)"
(??0?$caller_py _function_impl@ U?$caller@P6AHM @ZUdefault_call _policies@pytho n@boost@@U?$vec tor2@HM@mpl@3@@ detail@python@b oost@@@objects@ python@boost@@Q AE@ABU?$caller@ P6AHM@ZUdefault _call_policies@ python@boost@@U ?$vector2@HM@mp l@3@@detail@23@ @Z)
C_test.obj : error LNK2001: unresolved external symbol "public:
virtual unsigned int __thiscall
boost::python:: objects::py_fun ction_impl_base ::max_arity(voi d)const "
(?max_arity@py_ function_impl_b ase@objects@pyt hon@boost@@UBEI XZ)
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dll import) public: virtual __thiscall
boost::python:: objects::py_fun ction_impl_base ::~py_function_ impl_base(void) "
(__imp_??1py_fu nction_impl_bas e@objects@pytho n@boost@@UAE@XZ )
referenced in function "public: virtual __thiscall
boost::python:: objects::caller _py_function_im pl<struct
boost::python:: detail::caller< int (__cdecl*)(floa t),struct
boost::python:: default_call_po licies,struct
boost::mpl::vec tor2<int,float >::~caller_py_f unction_impl<st ruct
boost::python:: detail::caller< int (__cdecl*)(floa t),struct
boost::python:: default_call_po licies,struct
boost::mpl::vec tor2<int,float >(void)"
(??1?$caller_py _function_impl@ U?$caller@P6AHM @ZUdefault_call _policies@pytho n@boost@@U?$vec tor2@HM@mpl@3@@ detail@python@b oost@@@objects@ python@boost@@U AE@XZ)
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dll import) struct
boost::python:: converter::rval ue_from_python_ stage1_data __cdecl
boost::python:: converter::rval ue_from_python_ stage1(struct _object
*,struct boost::python:: converter::regi stration const &)"
(__imp_?rvalue_ from_python_sta ge1@converter@p ython@boost@@YA ?AUrvalue_from_ python_stage1_d ata@123@PAU_obj ect@@ABUregistr ation@123@@Z)
referenced in function "public: __thiscall
boost::python:: converter::arg_ rvalue_from_pyt hon<float>::arg _rvalue_from_py thon<float>(str uct
_object *)"
(??0?$arg_rvalu e_from_python@M @converter@pyth on@boost@@QAE@P AU_object@@@Z)
C_test.obj : error LNK2019: unresolved external symbol
"__declspec(dll import) struct boost::python:: converter::regi stration
const & __cdecl boost::python:: converter::regi stry::lookup(st ruct
boost::python:: type_info)"
(__imp_?lookup@ registry@conver ter@python@boos t@@YAABUregistr ation@234@Utype _info@34@@Z)
referenced in function "struct boost::python:: converter::regi stration
const & __cdecl
boost::python:: converter::deta il::registry_lo okup<float const volatile
>(float const volatile & (__cdecl*)(void ))"
(??$registry_lo okup@$$CDM@deta il@converter@py thon@boost@@YAA BUregistration@ 123@P6AADMXZ@Z)
C:\Documents and Settings\Erik\M y Documents\Visua l Studio
2005\Projects\C _test\Debug\C_t est.dll : fatal error LNK1120: 8
unresolved externals
Dec 30 '06 #2
Osiris <no**@hotmail.c omtyped
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_MO DULE( 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.c omwrote:
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.li b, 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.co m>
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.co m>
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.co m>
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
3052
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 is how can I write a C++ method that will either return a boost wrapped C++ class or a Python object type depending on some internal value?
226
12577
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 "help", "copyright", "credits" or "license" for more information. >>> d1 = {'a':1} >>> d2 = {'b':2} >>> d3 = {'c':3}
0
1213
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 already know about boost.python dont bother reading farther cause its a kind of a technical question) anyways i embeded python in a litte c++ test app i wrote and got it working fine. eg: i could write a little python program and my app would load...
20
1843
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, however I expect an answer from the python newsgroup to be something like "everything". So maybe a better question is what type of programming projects is Python a bad choice? What makes it better or worse than languages like perl, php, delphi, or...
4
2368
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 also running a thread that fills that global list, but can't figure out how to do it. Essentially the code looks like; in the C++ app;
13
3852
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 modifications along the way to make the interface more Pythonic. For example, all of these functions return an error code (typically just errno passed along, but not always). They all accept as one of their arguments a pointer to someplace to store...
25
5751
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 advantages of one over the other in implementing this server.which language will be easier? What are the performance issues?In what language are mail servers generally written?
1
2493
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 Boost built using g++. Here's some of my output: Student@YGGDRASIL /c/Boost/libs/python/example/tutorial $ bjam -sTOOLS=mingw -d+2 ....found 1508 targets...
0
8619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
8559
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9112
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8826
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8818
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4338
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2996
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2261
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1971
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.