473,699 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

distutils and ctypes

Hi all,

I suspect that I'm doing something stupid, I would like some other
opinions though.
I'm getting started with ctypes and am trying to use distutils to help
build my module. At the moment I simply want distutils to build a
shared c library (not a python extension!). Under linux, the following
works, under windows xp id doesn't (which I guess is obvious, but the
linux success lead me on).
I have two files at the moment...

/* begin test.c */
int test(int i)
{
return i*i;
}
/* end test.c */

#begin setup.py
from distutils.core import setup, Extension
setup(name="tes t", version="0.0", ext_modules = [Extension("test ",
["test.c"])])
# end setup.py

If I run:

python setup.py build

under linux, I get a nice shared c library under my build dir, which
can be imported by ctypes.
If I run it under windows I get the following:

running build
running build_ext
building 'test' extension
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\bin\li nk.exe
/DLL /nologo /INCREMENTAL:NO /LIBPATH:C:\Pyth on25\libs
/LIBPATH:C:\Pyth on25\PCBuild /EXPORT:inittest
build\temp.win3 2-2.5\Release\tes t.obj /OUT:build\lib.w in32-2.5\test.pyd
/IMPLIB:build\te mp.win32-2.5\Release\tes t.lib
LINK : error LNK2001: unresolved external symbol inittest
build\temp.win3 2-2.5\Release\tes t.lib : fatal error LNK1120: 1
unresolved externals
LINK : fatal error LNK1141: failure during build of exports file

I can see the problem: python is trying to build an extension module
and is telling the linker to export "inittest", which doesn't exist.
This didn't happen under linux as you don't need to export interfaces
in shared c libraries on linux.

So finally, my question is, is there a way to get distutils to simply
build a shared library on windows so that I can use ctypes with them???

Thanks for your patience with this post, and thanks for any replies.
Best regards,
John Travers

Jan 9 '07 #1
9 4155
jt****@gmail.co m wrote:
So finally, my question is, is there a way to get distutils to simply
build a shared library on windows so that I can use ctypes with them???
Not out-of-box, no. The OOF2 project has added a bdist_shlib command which
should do most of what you want, though. It's somewhat UNIX-oriented, and I
think it tries to install the shared library to a standard location (e.g.
/usr/local/lib). You might want to modify it to install the shared library in
the package so it is easy to locate at runtime.

http://www.ctcms.nist.gov/oof/oof2/
http://www.ctcms.nist.gov/oof/oof2/s...2-2.0.1.tar.gz

The code is in the shlib/ subdirectory.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jan 9 '07 #2
Robert Kern wrote:
jt****@gmail.co m wrote:
>So finally, my question is, is there a way to get distutils to simply
build a shared library on windows so that I can use ctypes with them???

Not out-of-box, no. The OOF2 project has added a bdist_shlib command which
should do most of what you want, though. It's somewhat UNIX-oriented, and I
think it tries to install the shared library to a standard location (e.g.
/usr/local/lib). You might want to modify it to install the shared library in
the package so it is easy to locate at runtime.

http://www.ctcms.nist.gov/oof/oof2/
http://www.ctcms.nist.gov/oof/oof2/s...2-2.0.1.tar.gz

The code is in the shlib/ subdirectory.
And if you do so, please let us know about it! This would be quite useful for
many other ctypes-using projects.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jan 9 '07 #3
jt****@gmail.co m schrieb:
I suspect that I'm doing something stupid, I would like some other
opinions though.
I'm getting started with ctypes and am trying to use distutils to help
build my module. At the moment I simply want distutils to build a
shared c library (not a python extension!). Under linux, the following
works, under windows xp id doesn't (which I guess is obvious, but the
linux success lead me on).
Not sure it's stupid, but I wonder why you want to use ctypes. What's
wrong with extension modules?

Regards,
Martin
Jan 9 '07 #4
Martin v. Löwis wrote:
Not sure it's stupid, but I wonder why you want to use ctypes. What's
wrong with extension modules?
What's wrong with ctypes? They're both valid, useful approaches to connect to C
libraries.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jan 9 '07 #5
Robert Kern schrieb:
>Not sure it's stupid, but I wonder why you want to use ctypes. What's
wrong with extension modules?

What's wrong with ctypes? They're both valid, useful approaches to connect to C
libraries.
See the original posting. Distutils doesn't support building arbitrary
shared libraries, but does support building extension modules.

Furthermore, extension modules are more type-safe and more expressive
than loading shared libraries through ctypes. IMO, you may consider
using ctypes as a last resort - if you have the chance for a
well-engineered solution, write a compiled wrapper.

Regards,
Martin
Jan 9 '07 #6
Martin v. Löwis wrote:
Robert Kern schrieb:
>>Not sure it's stupid, but I wonder why you want to use ctypes. What's
wrong with extension modules?
What's wrong with ctypes? They're both valid, useful approaches to connect to C
libraries.

See the original posting. Distutils doesn't support building arbitrary
shared libraries, but does support building extension modules.

Furthermore, extension modules are more type-safe and more expressive
than loading shared libraries through ctypes. IMO, you may consider
using ctypes as a last resort - if you have the chance for a
well-engineered solution, write a compiled wrapper.
To which I say that doing the type-checking and error handling is much easier in
Python than using the C API. Add to that the tediousness of the edit-compile-run
cycle of C and the finickiness of refcounting.

There's nothing *wrong* with either approach. They're just different and have
different strengths and weaknesses. Some of those weaknesses are remediable (say
by using the bdist_shlib command that the OOF2 project implemented) and some aren't.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco

Jan 9 '07 #7
Robert Kern schrieb:
To which I say that doing the type-checking and error handling is much easier in
Python than using the C API. Add to that the tediousness of the edit-compile-run
cycle of C and the finickiness of refcounting.
Sure: edit-compile-run is tedious, but in the given case, it is
necessary anyway. Also, refcounting is finicky, but with ctypes,
you often have to use just as finicky memory management APIs.
(such as using specific allocation and deallocation routines,
or having to do memory management at all when in C you would
do stack allocation).

My main concern with ctypes is that you have to duplicate
information from the header files, which is error-prone,
especially when the header files may change (either across
versions or across target systems).

Regards,
Martin
Jan 10 '07 #8

Robert Kern wrote:
jt****@gmail.co m wrote:
So finally, my question is, is there a way to get distutils to simply
build a shared library on windows so that I can use ctypes with them???

Not out-of-box, no. The OOF2 project has added a bdist_shlib command which
should do most of what you want, though. It's somewhat UNIX-oriented, and I
think it tries to install the shared library to a standard location (e.g.
/usr/local/lib). You might want to modify it to install the shared library in
the package so it is easy to locate at runtime.

http://www.ctcms.nist.gov/oof/oof2/
http://www.ctcms.nist.gov/oof/oof2/s...2-2.0.1.tar.gz

The code is in the shlib/ subdirectory.
Thank you very much - this looks like exactly what I want.
John

Jan 10 '07 #9

Martin v. Lwis wrote:
Robert Kern schrieb:
To which I say that doing the type-checking and error handling is much easier in
Python than using the C API. Add to that the tediousness of the edit-compile-run
cycle of C and the finickiness of refcounting.

Sure: edit-compile-run is tedious, but in the given case, it is
necessary anyway. Also, refcounting is finicky, but with ctypes,
you often have to use just as finicky memory management APIs.
(such as using specific allocation and deallocation routines,
or having to do memory management at all when in C you would
do stack allocation).

My main concern with ctypes is that you have to duplicate
information from the header files, which is error-prone,
especially when the header files may change (either across
versions or across target systems).
I have looked at: building an extension module, using pyrex and using
ctypes. Initially ctypes won because I was under the impression that to
use pyrex or build an extension module required the same compiler as
python was compiled with. This is not a problem on linux, but under
windows this is much too great a problem for potential users (who will
also need to compile the module). I now have discovered that I can use
mingw32 (without patching) under windows (with python2.5) though I'm
not clear if this is by chance or has been implemented as a new feature
(can't find suitable documentation).

Anyway, at this point I think I will stick with ctypes purely out of
simplicity. My c library is for some legacy lab hardware control. I
only need to call three functions. With ctypes, I can check the data in
python (which I do anyway to validate it) and pass it onto the library
in about 10 lines of code. Writing an extension module I believe would
be much more difficult.

Anyway,
Thanks both for your help.
John

Jan 10 '07 #10

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

Similar topics

1
2578
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call functions exposed from dlls/shared libraries and has extensive facilities to create, access and manipulate
1
2654
by: Mathieu Malaterre | last post by:
Hello, I thought this would be easy but I guess I didn't get the distutil feeling. I am trying to write a setup for install my package but I don't understand how to do that. organisation: setup.py /bin/
19
2496
by: Thomas Heller | last post by:
ctypes 0.9.2 released - Oct 28, 2004 ==================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call functions exposed from dlls/shared libraries and has extensive facilities to create, access and manipulate
15
4120
by: Colin J. Williams | last post by:
The distutils download page has: -------------------------------------------------------- Current stable release The current stable release is Distutils 1.0.2; you can download it as: * Distutils-1.0.2.tar.gz (source distribution) (233k) * Distutils-1.0.2.zip (source distribution) (274k) * Distutils-1.0.2.win32.exe (Windows installer) (187k)
1
1717
by: Terry Hancock | last post by:
Some time ago, I got the idea that I wanted to build image resources from vector graphic originals, instead of marshalling hundreds of tiny little icon images by hand. I wrote "BuildImage" to do this for me, and so far, it works very well, so I'm trying to make it easier to use and available to more developers. There is a brief (and somewhat dated) tutorial explaining what BuildImage itself does at:
0
1873
by: Maarten Sneep | last post by:
I'm trying to build PyBison on Mac OS X, and I'm running into some problems with the distutils. Just for starters: PyBison requires Pyrex. This is not a problem, and Pyrex seems to work without problems, at least the primes sample module shows a nice 25 to 30 fold speed increase over the pure python version. I used the distutils to create the module from the primes.pyx sample, following the setup.py from the PyBison distrubution:
1
4725
by: sjdevnull | last post by:
Hey, I'm trying to wrap GNU readline with ctypes (the Python readline library doesn't support the callback interface), but I can't figure out how to set values to a variable inside the library. This is Python 2.5 on Linux. Here's what I have so far--if you comment out the memmove call (3 lines) it works as expected: # START #!/usr/local/bin/python2.5
6
12857
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the struct below. I haven't been able to figure out how I should declare the return type of the functions and read the fields. Any hint is appreciated. typedef struct { char *country_short; char *country_long;
0
1464
by: newbie73 | last post by:
OS: Vista Python 2.5.2.2 (ActiveState Software Installation) Running latest Cygwin release The error generated is pasted below - please help. - Luis ***************************************
0
8694
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8621
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
9041
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
8928
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
7757
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6538
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
5877
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();...
0
4379
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...
3
2013
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.