ctypes problem help needed.

This is the place for queries that don't fit in any of the other categories.

ctypes problem help needed.

Postby pizzaboy150 » Sun Feb 24, 2013 11:22 pm

Hi guys,

I am trying out the ctypes library in python and have made a very simple program with a shared library with the following error?

What am I doing wrong?

add.cpp
Code: Select all
int add(int x,int y);
int add(int x, int y)
{
   return x + y;
}


add.py
Code: Select all
#!/usr/bin/env python3

import ctypes

lib = ctypes.CDLL('/home/steve/Programming/add.so')

value = lib.add(ctypes.c_int(10), ctypes.c_int(20))

print(value)



terminal session
Code: Select all
steve@PizzaBoy150:~/Programming$ ./add.py
Traceback (most recent call last):
  File "./add.py", line 7, in <module>
    value = lib.add(ctypes.c_int(10), ctypes.c_int(20))
  File "/usr/lib/python3.2/ctypes/__init__.py", line 366, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib/python3.2/ctypes/__init__.py", line 371, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /home/steve/Programming/add.so: undefined symbol: add
steve@PizzaBoy150:~/Programming$

Thanks!
Last edited by micseydel on Mon Feb 25, 2013 12:16 am, edited 1 time in total.
Reason: Fixed formatting
pizzaboy150
 
Posts: 11
Joined: Sun Feb 24, 2013 11:14 pm

Re: ctypes problem help needed.

Postby micseydel » Mon Feb 25, 2013 12:17 am

What OS are you using? How did you compile your .so file?

Also, you don't need the declare the function if you're going to define it right away (you only need to declare when you need to use before you define), you should use separate code tags for different code (I've edited that for you this time) and tracebacks should be in code tags to preserve indentation.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1510
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: ctypes problem help needed.

Postby pizzaboy150 » Mon Feb 25, 2013 8:10 am

Thanks for the info, I am running Ubuntu linux and using g++ compiler to make the file a shared library.
pizzaboy150
 
Posts: 11
Joined: Sun Feb 24, 2013 11:14 pm

Re: ctypes problem help needed.

Postby micseydel » Mon Feb 25, 2013 8:14 am

I should have been more specific: what is the exact command you use it compile it?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1510
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: ctypes problem help needed.

Postby pizzaboy150 » Mon Feb 25, 2013 9:07 am

$g++ -fPIC -c add.cpp -o add.o

no errors and created add.o

$g++ -shared add.o -o add.so

no errors and compiled add.so
pizzaboy150
 
Posts: 11
Joined: Sun Feb 24, 2013 11:14 pm

Re: ctypes problem help needed.

Postby micseydel » Mon Feb 25, 2013 8:19 pm

Ah, ok I figured it out, C++ name-mangles things, so you have to use gcc instead of g++. There might be a g++ flag to decrease or prevent name-mangling or something, but gcc should be just fine.
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1510
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: ctypes problem help needed.

Postby pizzaboy150 » Mon Feb 25, 2013 10:08 pm

Code: Select all
steve@PizzaBoy150:~/Programming$ rm add.o add.so
steve@PizzaBoy150:~/Programming$ gcc -fPIC -c add.cpp -o add.o
steve@PizzaBoy150:~/Programming$ gcc -shared add.o -o add.so
steve@PizzaBoy150:~/Programming$ ./add.py
Traceback (most recent call last):
  File "./add.py", line 7, in <module>
    value = lib.add(ctypes.c_int(10), ctypes.c_int(20))
  File "/usr/lib/python3.2/ctypes/__init__.py", line 366, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib/python3.2/ctypes/__init__.py", line 371, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: /home/steve/Programming/add.so: undefined symbol: add
steve@PizzaBoy150:~/Programming$ ^C
steve@PizzaBoy150:~/Programming$


same error compiling this way???

I am running python 3.2?
pizzaboy150
 
Posts: 11
Joined: Sun Feb 24, 2013 11:14 pm

Re: ctypes problem help needed.

Postby micseydel » Mon Feb 25, 2013 10:26 pm

I should have been more explicit: you must use gcc and C. ctypes handles C, not C++.

Also, once you get it working , you an pass normal Python ints, no need to explicitly wrap them in ctypes.c_int().
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1510
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: ctypes problem help needed.

Postby pizzaboy150 » Mon Feb 25, 2013 11:25 pm

bingo sorted I never realized ctypes was for c "drrrr"! Just assumed c++ would work!

didn't have to change the code just the file name from add.cpp to add.c so gcc would compile it correctly!

Thanks for all your help!
pizzaboy150
 
Posts: 11
Joined: Sun Feb 24, 2013 11:14 pm


Return to General Coding Help

Who is online

Users browsing this forum: legacy_programmer and 6 guests