Changeset 391 for python/trunk/Doc/extending/embedding.rst
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Doc/extending/embedding.rst
r2 r391 26 26 So if you are embedding Python, you are providing your own main program. One of 27 27 the things this main program has to do is initialize the Python interpreter. At 28 the very least, you have to call the function :c func:`Py_Initialize`. There are28 the very least, you have to call the function :c:func:`Py_Initialize`. There are 29 29 optional calls to pass command line arguments to Python. Then later you can 30 30 call the interpreter from any part of the application. 31 31 32 32 There are several different ways to call the interpreter: you can pass a string 33 containing Python statements to :c func:`PyRun_SimpleString`, or you can pass a33 containing Python statements to :c:func:`PyRun_SimpleString`, or you can pass a 34 34 stdio file pointer and a file name (for identification in error messages only) 35 to :c func:`PyRun_SimpleFile`. You can also call the lower-level operations35 to :c:func:`PyRun_SimpleFile`. You can also call the lower-level operations 36 36 described in the previous chapters to construct and use Python objects. 37 37 … … 62 62 main(int argc, char *argv[]) 63 63 { 64 Py_SetProgramName(argv[0]); /* optional but recommended */ 64 65 Py_Initialize(); 65 66 PyRun_SimpleString("from time import time,ctime\n" … … 69 70 } 70 71 71 The above code first initializes the Python interpreter with 72 :cfunc:`Py_Initialize`, followed by the execution of a hard-coded Python script 73 that print the date and time. Afterwards, the :cfunc:`Py_Finalize` call shuts 72 The :c:func:`Py_SetProgramName` function should be called before 73 :c:func:`Py_Initialize` to inform the interpreter about paths to Python run-time 74 libraries. Next, the Python interpreter is initialized with 75 :c:func:`Py_Initialize`, followed by the execution of a hard-coded Python script 76 that prints the date and time. Afterwards, the :c:func:`Py_Finalize` call shuts 74 77 the interpreter down, followed by the end of the program. In a real program, 75 78 you may want to get the Python script from another source, perhaps a text-editor 76 79 routine, a file, or a database. Getting the Python code from a file can better 77 be done by using the :c func:`PyRun_SimpleFile` function, which saves you the80 be done by using the :c:func:`PyRun_SimpleFile` function, which saves you the 78 81 trouble of allocating memory space and loading the file contents. 79 82 … … 138 141 in ``argv[2]``. Its integer arguments are the other values of the ``argv`` 139 142 array. If you compile and link this program (let's call the finished executable 140 :program:`call`), and use it to execute a Python script, such as:: 143 :program:`call`), and use it to execute a Python script, such as: 144 145 .. code-block:: python 141 146 142 147 def multiply(a,b): … … 163 168 164 169 After initializing the interpreter, the script is loaded using 165 :c func:`PyImport_Import`. This routine needs a Python string as its argument,166 which is constructed using the :c func:`PyString_FromString` data conversion170 :c:func:`PyImport_Import`. This routine needs a Python string as its argument, 171 which is constructed using the :c:func:`PyString_FromString` data conversion 167 172 routine. :: 168 173 … … 176 181 177 182 Once the script is loaded, the name we're looking for is retrieved using 178 :c func:`PyObject_GetAttrString`. If the name exists, and the object returned is183 :c:func:`PyObject_GetAttrString`. If the name exists, and the object returned is 179 184 callable, you can safely assume that it is a function. The program then 180 185 proceeds by constructing a tuple of arguments as normal. The call to the Python … … 219 224 }; 220 225 221 Insert the above code just above the :c func:`main` function. Also, insert the222 following two statements directly after :c func:`Py_Initialize`::226 Insert the above code just above the :c:func:`main` function. Also, insert the 227 following two statements directly after :c:func:`Py_Initialize`:: 223 228 224 229 numargs = argc; … … 227 232 These two lines initialize the ``numargs`` variable, and make the 228 233 :func:`emb.numargs` function accessible to the embedded Python interpreter. 229 With these extensions, the Python script can do things like :: 234 With these extensions, the Python script can do things like 235 236 .. code-block:: python 230 237 231 238 import emb … … 252 259 .. _link-reqs: 253 260 254 Linking Requirements 255 ==================== 256 257 While the :program:`configure` script shipped with the Python sources will 258 correctly build Python to export the symbols needed by dynamically linked 259 extensions, this is not automatically inherited by applications which embed the 260 Python library statically, at least on Unix. This is an issue when the 261 application is linked to the static runtime library (:file:`libpython.a`) and 262 needs to load dynamic extensions (implemented as :file:`.so` files). 263 264 The problem is that some entry points are defined by the Python runtime solely 265 for extension modules to use. If the embedding application does not use any of 266 these entry points, some linkers will not include those entries in the symbol 267 table of the finished executable. Some additional options are needed to inform 268 the linker not to remove these symbols. 269 270 Determining the right options to use for any given platform can be quite 271 difficult, but fortunately the Python configuration already has those values. 272 To retrieve them from an installed Python interpreter, start an interactive 273 interpreter and have a short session like this:: 274 275 >>> import distutils.sysconfig 276 >>> distutils.sysconfig.get_config_var('LINKFORSHARED') 261 Compiling and Linking under Unix-like systems 262 ============================================= 263 264 It is not necessarily trivial to find the right flags to pass to your 265 compiler (and linker) in order to embed the Python interpreter into your 266 application, particularly because Python needs to load library modules 267 implemented as C dynamic extensions (:file:`.so` files) linked against 268 it. 269 270 To find out the required compiler and linker flags, you can execute the 271 :file:`python{X.Y}-config` script which is generated as part of the 272 installation process (a :file:`python-config` script may also be 273 available). This script has several options, of which the following will 274 be directly useful to you: 275 276 * ``pythonX.Y-config --cflags`` will give you the recommended flags when 277 compiling:: 278 279 $ /opt/bin/python2.7-config --cflags 280 -I/opt/include/python2.7 -fno-strict-aliasing -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes 281 282 * ``pythonX.Y-config --ldflags`` will give you the recommended flags when 283 linking:: 284 285 $ /opt/bin/python2.7-config --ldflags 286 -L/opt/lib/python2.7/config -lpthread -ldl -lutil -lm -lpython2.7 -Xlinker -export-dynamic 287 288 .. note:: 289 To avoid confusion between several Python installations (and especially 290 between the system Python and your own compiled Python), it is recommended 291 that you use the absolute path to :file:`python{X.Y}-config`, as in the above 292 example. 293 294 If this procedure doesn't work for you (it is not guaranteed to work for 295 all Unix-like platforms; however, we welcome :ref:`bug reports <reporting-bugs>`) 296 you will have to read your system's documentation about dynamic linking and/or 297 examine Python's :file:`Makefile` (use :func:`sysconfig.get_makefile_filename` 298 to find its location) and compilation 299 options. In this case, the :mod:`sysconfig` module is a useful tool to 300 programmatically extract the configuration values that you will want to 301 combine together. For example: 302 303 .. code-block:: python 304 305 >>> import sysconfig 306 >>> sysconfig.get_config_var('LIBS') 307 '-lpthread -ldl -lutil' 308 >>> sysconfig.get_config_var('LINKFORSHARED') 277 309 '-Xlinker -export-dynamic' 278 310 279 .. index:: module: distutils.sysconfig 280 281 The contents of the string presented will be the options that should be used. 282 If the string is empty, there's no need to add any additional options. The 283 :const:`LINKFORSHARED` definition corresponds to the variable of the same name 284 in Python's top-level :file:`Makefile`. 285 311 312 .. XXX similar documentation for Windows missing
Note:
See TracChangeset
for help on using the changeset viewer.