473,749 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C Function Pointer Wrapping Example not working

Hi All,

I am new to using swig/C++/python. I got some problem with function
pointers. I posted in swig-user, but got no response. So I forwarded
it here. You help is greatly appreciated.

Thanks!

---------- Forwarded message ----------

Hi All,

Yesterday I posted about the question I had of template function. This
time I worked step step from examples in Doc 1.3 to reach my ultimate
goal. But as I worked on the Function Pointer Example in 5.4.9 in Doc
1.3, it doesn't work. My platform is a Ubuntu linux with swig 1.3.36,
python 2.5.2 and gcc 4.2.3.

The compiling and linking has no errors:

swig -python -c++ -o test_wrap.cpp test.i
python setup-test.py build
running build
running build_py
copying test.py -build/lib.linux-i686-2.5
running build_ext
building '_test' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -
Wstrict-prototypes -fPIC -I/usr/include/python2.5 -c test_wrap.cpp -o
build/temp.linux-i686-2.5/test_wrap.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid
for Ada/C/ObjC but not for C++
g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-
i686-2.5/test_wrap.o build/temp.linux-i686-2.5/test.o -o build/
lib.linux-i686-2.5/_test.so

But when I try to import test in python, it complains:
import _test
ImportError: ./_test.so undefined symbol: _Z9binary_opiiP FiiiE

My test.i is:
%module test
%{
#include "test.h"
%}
%include "test.h"

%callback("%s_c b");
int add( int, int ); //add_cb
int sub( int, int ); //sub_cb
int mul( int, int ); //mul_cb
%nocallback;

My test.h is:

int binary_op(int a, int b, int (*op)(int,int) );
int add( int a, int b ) { return a+b; }
int sub( int a, int b ) { return a-b; }
int mul( int a, int b ) { return a*b; }
// NOTE: this is func ptr from example in Doc 1.3: 5.4.9

It is really strange to me. I am a novice swigger but I really need
its power to accelerate my development. Could anybody point out where
my problem is?

TIA

Charlie
Nov 14 '08 #1
5 4307
Charlie <Ch************ *@gmail.comwrot e:
I am new to using swig/C++/python. I got some problem with function
pointers. I posted in swig-user, but got no response. So I forwarded
it here. You help is greatly appreciated.

Thanks!
Hi All,

Yesterday I posted about the question I had of template function. This
time I worked step step from examples in Doc 1.3 to reach my ultimate
goal. But as I worked on the Function Pointer Example in 5.4.9 in Doc
1.3, it doesn't work. My platform is a Ubuntu linux with swig 1.3.36,
python 2.5.2 and gcc 4.2.3.

The compiling and linking has no errors:

swig -python -c++ -o test_wrap.cpp test.i
python setup-test.py build
running build
running build_py
copying test.py -build/lib.linux-i686-2.5
running build_ext
building '_test' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -
Wstrict-prototypes -fPIC -I/usr/include/python2.5 -c test_wrap.cpp -o
build/temp.linux-i686-2.5/test_wrap.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid
for Ada/C/ObjC but not for C++
g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-
i686-2.5/test_wrap.o build/temp.linux-i686-2.5/test.o -o build/
lib.linux-i686-2.5/_test.so

But when I try to import test in python, it complains:
import _test
ImportError: ./_test.so undefined symbol: _Z9binary_opiiP FiiiE
The above is a mangled name so you've got some C vs C++ problems I'd
say.

You could try putting some extern "C" {} in around all the functions
which are imported and exported. Have a look at the code SWIG
generates and see if it puts some extern "C" in and match what it
does in your code.

We used to use SWIG in for python embedding in our C++ project, but we
found that using ctypes is a lot easier. You just write C .so/.dll
and use ctypes to access them. You can do callbacks and embedding
python like this too.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Nov 16 '08 #2
>
*But when I try to import test in python, it complains:
*import _test
*ImportError: ./_test.so undefined symbol: _Z9binary_opiiP FiiiE

The above is a mangled name so you've got some C vs C++ problems I'd
say.

You could try putting some extern "C" {} in around all the functions
which are imported and exported. *Have a look at the code SWIG
generates and see if it puts some extern "C" in and match what it
does in your code.

We used to use SWIG in for python embedding in our C++ project, but we
found that using ctypes is a lot easier. *You just write C .so/.dll
and use ctypes to access them. *You can do callbacks and embedding
python like this too.
Thanks Nick.

I tried your method, if I am right(please see the attached details),
and I still got the undefined symbol error like previous. The only
difference is "_Z9binary_opii PFiiiE" changed to "binary_op" . Could you
help me more on this. It seems to have a mixed problems here and I
guess what you've pointed out is one of them. But really, what I do
now is just try to reproduce the example, how can this fails? What my
ultimate need is wrapping up a template function taking template
function pointer as argument. Did you ever try that? Many thanks
already anyway.

FILE and ERROR details:

---------test.i--------------
%module test
%{
#include "test.h"
%}
%include "test.h"

%callback("%s_c b");
int myadd( int, int ); //myadd_cb
int mysub( int, int ); //mysub_cb
int mymul( int, int ); //mymul_cb
%nocallback;
--------------------------------

-----------test.h--------------
extern "C"{
int binary_op(int a, int b, int (*op)(int,int) );
int myadd( int a, int b ) { return a+b; };
int mysub( int a, int b ) { return a-b; };
int mymul( int a, int b ) { return a*b; };
}
---------------------------------

------error message----------
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 7, in <module>
import _test
ImportError: ./_test.so: undefined symbol: binary_op
----------------------------------

-------compiling message-----
swig -v -python -c++ -o test_wrap.cpp test.i
LangSubDir: python
Search paths:
./
./swig_lib/python/
/usr/local/share/swig/1.3.36/python/
./swig_lib/
/usr/local/share/swig/1.3.36/
Preprocessing.. .
Starting language-specific parse...
Processing types...
C++ analysis...
Generating wrappers...
python setup-test.py build
running build
running build_py
copying test.py -build/lib.linux-i686-2.5
running build_ext
building '_test' extension
gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -
Wstrict-prototypes -fPIC -I/usr/include/python2.5 -c test_wrap.cpp -o
build/temp.linux-i686-2.5/test_wrap.o
cc1plus: warning: command line option "-Wstrict-prototypes" is valid
for Ada/C/ObjC but not for C++
g++ -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions build/temp.linux-
i686-2.5/test_wrap.o build/temp.linux-i686-2.5/test.o -o build/
lib.linux-i686-2.5/_test.so
---------------------------------
Nov 17 '08 #3
On Nov 16, 8:56*pm, Charlie <Charlie.Xia... .@gmail.comwrot e:
*But when I try to import test in python, it complains:
*import _test
*ImportError: ./_test.so undefined symbol: _Z9binary_opiiP FiiiE
The above is a mangled name so you've got some C vs C++ problems I'd
say.
You could try putting some extern "C" {} in around all the functions
which are imported and exported. *Have a look at the code SWIG
generates and see if it puts some extern "C" in and match what it
does in your code.
We used to use SWIG in for python embedding in our C++ project, but we
found that using ctypes is a lot easier. *You just write C .so/.dll
and use ctypes to access them. *You can do callbacks and embedding
python like this too.

Thanks Nick.

I tried your method, if I am right(please see the attached details),
and I still got the undefined symbol error like previous. The only
difference is "_Z9binary_opii PFiiiE" changed to "binary_op" . Could you
help me more on this. It seems to have a mixed problems here and I
guess what you've pointed out is one of them. But really, what I do
now is just try to reproduce the example, how can this fails? What my
ultimate need is wrapping up a template function taking template
function pointer as argument. Did you ever try that? Many thanks
already anyway.
------error message----------
Traceback (most recent call last):
* File "<stdin>", line 1, in <module>
* File "test.py", line 7, in <module>
* * import _test
ImportError: ./_test.so: undefined symbol: binary_op
----------------------------------
Hi Charlie,

I think you're overcomplicatin g. Here's what I think what you want:
(Unproduced.)
>>binary_op( 3, 4, myadd )
7
>>binary_op( 3, 4, mysub )
-1
>>binary_op( 3, 4, mymul )
12

Correct?
Nov 17 '08 #4
Charlie <Ch************ *@gmail.comwrot e:
>
?But when I try to import test in python, it complains:
?import _test
?ImportError: ./_test.so undefined symbol: _Z9binary_opiiP FiiiE
The above is a mangled name so you've got some C vs C++ problems I'd
say.

You could try putting some extern "C" {} in around all the functions
which are imported and exported. ?Have a look at the code SWIG
generates and see if it puts some extern "C" in and match what it
does in your code.

We used to use SWIG in for python embedding in our C++ project, but we
found that using ctypes is a lot easier. ?You just write C .so/.dll
and use ctypes to access them. ?You can do callbacks and embedding
python like this too.

Thanks Nick.

I tried your method, if I am right(please see the attached details),
and I still got the undefined symbol error like previous. The only
difference is "_Z9binary_opii PFiiiE" changed to "binary_op" . Could you
help me more on this. It seems to have a mixed problems here and I
guess what you've pointed out is one of them. But really, what I do
now is just try to reproduce the example, how can this fails? What my
ultimate need is wrapping up a template function taking template
function pointer as argument. Did you ever try that? Many thanks
already anyway.

FILE and ERROR details:

%module test
%{
#include "test.h"
%}
%include "test.h"

%callback("%s_c b");
int myadd( int, int ); //myadd_cb
int mysub( int, int ); //mysub_cb
int mymul( int, int ); //mymul_cb
%nocallback;

extern "C"{
int binary_op(int a, int b, int (*op)(int,int) );
int myadd( int a, int b ) { return a+b; };
int mysub( int a, int b ) { return a-b; };
int mymul( int a, int b ) { return a*b; };
}

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "test.py", line 7, in <module>
import _test
ImportError: ./_test.so: undefined symbol: binary_op
Nowhere in your code is the definition of binary_op - that is why you
get a linker error.

Is it defined in another C file? If so you need to link it with the
swig wrapper before you make the .so

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Nov 17 '08 #5
Nowhere in your code is the definition of binary_op - that is why you
get a linker error.

Is it defined in another C file? *If so you need to link it with the
swig wrapper before you make the .so
Thanks for pointing out. I sorted the code out finally!

Charlie
Nov 18 '08 #6

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

Similar topics

9
2523
by: iceColdFire | last post by:
HI, I have a function as void f(int p) { return p++; } now I have created a function pointer as
7
5189
by: Mike D. | last post by:
I have a problem with a dynamic library I am developing, but it is really more of a pointer issue than anything else. Hopefully someone here can lend me some assistance or insight into resolving this. Ok... here goes.... I have a function that passes a pointer to a string to another function. For example: int FunctionA ()
4
5144
by: H.B. | last post by:
Hi, I successfully implement a static callback function for my dll usign delegates. Now, I need to use member function instead of static function. How can I make that (in Managed C++). Hugo
4
2366
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of which I mean here. Just for a moment, let's just take one definition for one of the
3
3653
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take arguments of their real types, then when I pass this pointer through an interface function, I...
7
2619
by: harishashim | last post by:
I am wrapping a digital camera API using Managed C++ VS .NET 2003). I have this function that called as bellow in the API sample. err = PR_RC_StartViewFinder( m_hCamera, //line 1 (prContext)this, //line 2 (prViewFinderCB*)&ViewFinderCallBackFun ); //line 3 prContext is actually a typedef for unsigned long. ViewFinderCallBackFun is a callback function. There is two error that I get when i tried above code unchanged:
43
2083
by: Tony | last post by:
I'm working with GUI messaging and note that MFC encapsulates the message loop inside of a C++ class member function. Is this somehow inherently less robust than calling the message loop functions within main? (It just doesn't "feel" right to me). Example 1: class MyProg { public:
5
2268
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer to functions are hard to inline for the compiler, and so you won't be able to avoid function call overhead. This is an important aspect when you are calling a function very frequently for evaluation reason: think of the problem of performing...
10
6999
by: SQACPP | last post by:
Hi, I try to figure out how to use Callback procedure in a C++ form project The following code *work* perfectly on a console project #include "Windows.h" BOOL CALLBACK MyEnumWindowsProc(HWND hwnd, LPARAM lparam) {
0
8996
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
8832
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
9388
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9333
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
9254
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...
1
6800
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3319
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
2791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.