Beginning Python - From Novice to Professional
Beginning Python - From Novice to Professional Beginning Python - From Novice to Professional
CHAPTER 17 ■ EXTENDING PYTHON 363 for (i=0; i
364 CHAPTER 17 ■ EXTENDING PYTHON The only one needed is the -python option, to make sure SWIG wraps your C code so you can use it in Python. Another option you may find useful is -c++, which you use if you’re wrapping a C++ library. You run SWIG with the interface file (or, if you prefer, a header file) like this: $ swig -python palindrome.i After this, you should have two new files—one called palindrome_wrap.c and one called palindrome.py. Compiling, Linking, and Using This is, perhaps, the most tricky part (at least I think so). In order to compile things properly, you need to know where you keep the source code of your Python distribution (or, at least, the header files called pyconfig.h and Python.h; you will probably find these in the root directory of your Python installation, and in the Include subdirectory, respectively). You also have to figure out the right switches to compile your code into a shared library with your C compiler of choice. Here is an example for Solaris using the cc compiler, assuming that $PYTHON_HOME points to the root of Python installation: $ cc -c palindrome.c $ cc -I$PYTHON_HOME -I$PYTHON_HOME/Include -c palindrome_wrap.c $ cc -G palindrome.o palindrome_wrap.o -o _palindrome.so Here is the sequence for using the gcc compiler in Linux: gcc -c palindrome.c gcc -I$PYTHON_HOME -I$PYTHON_HOME/Include -c palindrome_wrap.c gcc -shared palindrome.o palindrome_wrap.o -o _palindrome.so It may be that all the necessary include files are found in one place, such as /usr/include/ python2.4 (update the version number as needed). In this case, the following should do the trick: gcc -c palindrome.c gcc -I/usr/include/python2.4 -c palindrome_wrap.c gcc -shared palindrome.o palindrome_wrap.o -o _palindrome.so In Windows (again assuming that you’re using gcc on the command line) you could use the following command as the last one, for creating the shared library: gcc -shared palindrome.o palindrome_wrap.o C:/Python24/libs/libpython24.a -o _palindrome.dll ■Note If you use gcc on Solaris, add the flag -fPIC to the first two command lines (right after the command gcc). Otherwise, the compiler will become mighty confused when you try to link the files in the last command. Also, if you’re using using a package manager (as is common in many Linux platforms), you may have to install a separate package (called something like python-dev) to get the header files needed to compile your extensions.
- Page 343 and 344: 312 CHAPTER 14 ■ NETWORK PROGRAMM
- Page 345 and 346: 314 CHAPTER 15 ■ PYTHON AND THE W
- Page 347 and 348: 316 CHAPTER 15 ■ PYTHON AND THE W
- Page 349 and 350: 318 CHAPTER 15 ■ PYTHON AND THE W
- Page 351 and 352: 320 CHAPTER 15 ■ PYTHON AND THE W
- Page 353 and 354: 322 CHAPTER 15 ■ PYTHON AND THE W
- Page 355 and 356: 324 CHAPTER 15 ■ PYTHON AND THE W
- Page 357 and 358: 326 CHAPTER 15 ■ PYTHON AND THE W
- Page 359 and 360: 328 CHAPTER 15 ■ PYTHON AND THE W
- Page 361 and 362: 330 CHAPTER 15 ■ PYTHON AND THE W
- Page 363 and 364: 332 CHAPTER 15 ■ PYTHON AND THE W
- Page 365 and 366: 334 CHAPTER 15 ■ PYTHON AND THE W
- Page 367 and 368: 336 CHAPTER 15 ■ PYTHON AND THE W
- Page 369 and 370: 338 CHAPTER 15 ■ PYTHON AND THE W
- Page 372 and 373: CHAPTER 16 ■ ■ ■ Testing, 1-2
- Page 374 and 375: CHAPTER 16 ■ TESTING, 1-2-3 343 W
- Page 376 and 377: CHAPTER 16 ■ TESTING, 1-2-3 345 d
- Page 378 and 379: CHAPTER 16 ■ TESTING, 1-2-3 347 u
- Page 380 and 381: CHAPTER 16 ■ TESTING, 1-2-3 349 F
- Page 382 and 383: CHAPTER 16 ■ TESTING, 1-2-3 351 P
- Page 384 and 385: CHAPTER 16 ■ TESTING, 1-2-3 353 "
- Page 386: CHAPTER 16 ■ TESTING, 1-2-3 355 N
- Page 389 and 390: 358 CHAPTER 17 ■ EXTENDING PYTHON
- Page 391 and 392: 360 CHAPTER 17 ■ EXTENDING PYTHON
- Page 393: 362 CHAPTER 17 ■ EXTENDING PYTHON
- Page 397 and 398: 366 CHAPTER 17 ■ EXTENDING PYTHON
- Page 399 and 400: 368 CHAPTER 17 ■ EXTENDING PYTHON
- Page 401 and 402: 370 CHAPTER 17 ■ EXTENDING PYTHON
- Page 404 and 405: CHAPTER 18 ■ ■ ■ Packaging Yo
- Page 406 and 407: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 408 and 409: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 410 and 411: CHAPTER 18 ■ PACKAGING YOUR PROGR
- Page 412 and 413: CHAPTER 19 ■ ■ ■ Playful Prog
- Page 414 and 415: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 416 and 417: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 418 and 419: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 420: CHAPTER 19 ■ PLAYFUL PROGRAMMING
- Page 423 and 424: 392 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 425 and 426: 394 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 427 and 428: 396 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 429 and 430: 398 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 431 and 432: 400 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 433 and 434: 402 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 435 and 436: 404 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 437 and 438: 406 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 439 and 440: 408 CHAPTER 20 ■ PROJECT 1: INSTA
- Page 442 and 443: CHAPTER 21 ■ ■ ■ Project 2: P
364 CHAPTER 17 ■ EXTENDING PYTHON<br />
The only one needed is the -python option, <strong>to</strong> make sure SWIG wraps your C code so you can<br />
use it in <strong>Python</strong>. Another option you may find useful is -c++, which you use if you’re wrapping<br />
a C++ library. You run SWIG with the interface file (or, if you prefer, a header file) like this:<br />
$ swig -python palindrome.i<br />
After this, you should have two new files—one called palindrome_wrap.c and one called<br />
palindrome.py.<br />
Compiling, Linking, and Using<br />
This is, perhaps, the most tricky part (at least I think so). In order <strong>to</strong> compile things properly,<br />
you need <strong>to</strong> know where you keep the source code of your <strong>Python</strong> distribution (or, at least, the<br />
header files called pyconfig.h and <strong>Python</strong>.h; you will probably find these in the root direc<strong>to</strong>ry<br />
of your <strong>Python</strong> installation, and in the Include subdirec<strong>to</strong>ry, respectively). You also have <strong>to</strong><br />
figure out the right switches <strong>to</strong> compile your code in<strong>to</strong> a shared library with your C compiler of<br />
choice. Here is an example for Solaris using the cc compiler, assuming that $PYTHON_HOME points<br />
<strong>to</strong> the root of <strong>Python</strong> installation:<br />
$ cc -c palindrome.c<br />
$ cc -I$PYTHON_HOME -I$PYTHON_HOME/Include -c palindrome_wrap.c<br />
$ cc -G palindrome.o palindrome_wrap.o -o _palindrome.so<br />
Here is the sequence for using the gcc compiler in Linux:<br />
gcc -c palindrome.c<br />
gcc -I$PYTHON_HOME -I$PYTHON_HOME/Include -c palindrome_wrap.c<br />
gcc -shared palindrome.o palindrome_wrap.o -o _palindrome.so<br />
It may be that all the necessary include files are found in one place, such as /usr/include/<br />
python2.4 (update the version number as needed). In this case, the following should do the trick:<br />
gcc -c palindrome.c<br />
gcc -I/usr/include/python2.4 -c palindrome_wrap.c<br />
gcc -shared palindrome.o palindrome_wrap.o -o _palindrome.so<br />
In Windows (again assuming that you’re using gcc on the command line) you could use<br />
the following command as the last one, for creating the shared library:<br />
gcc -shared palindrome.o palindrome_wrap.o C:/<strong>Python</strong>24/libs/libpython24.a -o<br />
_palindrome.dll<br />
■Note If you use gcc on Solaris, add the flag -fPIC <strong>to</strong> the first two command lines (right after the<br />
command gcc). Otherwise, the compiler will become mighty confused when you try <strong>to</strong> link the files in the last<br />
command. Also, if you’re using using a package manager (as is common in many Linux platforms), you may<br />
have <strong>to</strong> install a separate package (called something like python-dev) <strong>to</strong> get the header files needed <strong>to</strong><br />
compile your extensions.