[Python编程(第4版)].(Programming.Python.4th.Edition).Mark.Lutz.文字版

(yzsuai) #1

embed-bytecode.exe


all: $(BASICS)


embed%.exe: embed%.o
gcc embed$*.o -L$(PYLIB) -lpython3.1 -g -o $@


embed%.o: embed%.c
gcc embed$*.c -c -g -I$(PYINC)


clean:
rm -f .o .pyc $(BASICS) core


On some platforms, you may need to also link in other libraries because the Python
library file used may have been built with external dependencies enabled and required.
In fact, you may have to link in arbitrarily many more externals for your Python library,
and frankly, chasing down all the linker dependencies can be tedious. Required libraries
may vary per platform and Python install, so there isn’t a lot of advice I can offer to
make this process simple (this is C, after all). The standard C development techniques
will apply.


One hint here: if you’re going to do much embedding work and you run into external
dependency issues, on some platforms you might want to build Python on your ma-
chine from its source with all unnecessary extensions disabled in its build configuration
files (see the Python source package for details). This produces a Python library with
minimal external requirements, which may links more easily.


Once you’ve gotten the makefile to work, run it to build all of this section’s C programs
at once with Python libraries linked in:


.../PP4E/Integrate/Embed/Basics$ make -f makefile.basics clean
rm -f *.o *.pyc embed-simple.exe embed-string.exe embed-object.exe
embed-dict.exe embed-bytecode.exe core

.../PP4E/Integrate/Embed/Basics$ make -f makefile.basics
gcc embed-simple.c -c -g -I/usr/local/include/python3.1
gcc embed-simple.o -L/usr/local/bin -lpython3.1 -g -o embed-simple.exe
gcc embed-string.c -c -g -I/usr/local/include/python3.1
gcc embed-string.o -L/usr/local/bin -lpython3.1 -g -o embed-string.exe
gcc embed-object.c -c -g -I/usr/local/include/python3.1
gcc embed-object.o -L/usr/local/bin -lpython3.1 -g -o embed-object.exe
gcc embed-dict.c -c -g -I/usr/local/include/python3.1
gcc embed-dict.o -L/usr/local/bin -lpython3.1 -g -o embed-dict.exe
gcc embed-bytecode.c -c -g -I/usr/local/include/python3.1
gcc embed-bytecode.o -L/usr/local/bin -lpython3.1 -g -o embed-bytecode.exe
rm embed-dict.o embed-object.o embed-simple.o embed-bytecode.o embed-string.o

After building with either makefile, you can run the resulting C program as usual:


.../PP4E/Integrate/Embed/Basics$ ./embed-simple
embed-simple
The meaning of life...
THE MEANING OF PYTHON...

Basic Embedding Techniques | 1521
Free download pdf