the problem of embedding python

胡兴洋 hu.007 at 163.com
Thu Mar 24 05:22:05 EST 2005


hi,python-chinese!

environment: FreeBSD4.11, gcc2.95&3.4, python2.2(ports安装).


===============source code===============
#include <Python.h>

int main()
{
        Py_Initialize();
        PyRun_SimpleString("from time import time, ctime\n"
                           "print 'Today is', ctime( time() )\n");
        Py_Finalize();
        return 0;
}







===============document===============
5.6 Linking Requirements 
While the configure script shipped with the Python sources will correctly build Python to export the symbols needed by dynamically linked extensions, this is not automatically inherited by applications which embed the Python library statically, at least on Unix. This is an issue when the application is linked to the static runtime library (libpython.a) and needs to load dynamic extensions (implemented as .so files). 

The problem is that some entry points are defined by the Python runtime solely for extension modules to use. If the embedding application does not use any of these entry points, some linkers will not include those entries in the symbol table of the finished executable. Some additional options are needed to inform the linker not to remove these symbols. 

Determining the right options to use for any given platform can be quite difficult, but fortunately the Python configuration already has those values. To retrieve them from an installed Python interpreter, start an interactive interpreter and have a short session like this: 


>>> import distutils.sysconfig
>>> distutils.sysconfig.get_config_var('LINKFORSHARED')
'-Xlinker -export-dynamic'

The contents of the string presented will be the options that should be used. If the string is empty, there's no need to add any additional options. The LINKFORSHARED definition corresponds to the variable of the same name in Python's top-level Makefile. 






===============result when compile===============
xyhu at FreeBSD_xyhu$ gcc34 test_python.cpp -o test_python -L/usr/local/lib/python2.2/config -lpython2.2 -I/usr/local/include/python2.2
 
/usr/local/lib/python2.2/config/libpython2.2.a(posixmodule.o): In function `posix_tmpnam':
posixmodule.o(.text+0x297a): warning: tmpnam() possibly used unsafely; consider using mkstemp()
/usr/local/lib/python2.2/config/libpython2.2.a(posixmodule.o): In function `posix_tempnam':
posixmodule.o(.text+0x28b6): warning: tempnam() possibly used unsafely; consider using mkstemp()
/var/tmp//ccw3igF6.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
/usr/local/lib/python2.2/config/libpython2.2.a(floatobject.o): In function `float_rem':
floatobject.o(.text+0xf69): undefined reference to `fmod'
/usr/local/lib/python2.2/config/libpython2.2.a(floatobject.o): In function `float_divmod':
floatobject.o(.text+0x1111): undefined reference to `fmod'
floatobject.o(.text+0x11a9): undefined reference to `floor'
/usr/local/lib/python2.2/config/libpython2.2.a(floatobject.o): In function `float_pow':
floatobject.o(.text+0x13e2): undefined reference to `fmod'
floatobject.o(.text+0x14b5): undefined reference to `floor'
floatobject.o(.text+0x1541): undefined reference to `pow'
/usr/local/lib/python2.2/config/libpython2.2.a(bltinmodule.o): In function `builtin_round':
bltinmodule.o(.text+0x1e1d): undefined reference to `floor'
bltinmodule.o(.text+0x1e37): undefined reference to `ceil'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function `PyThread_start_new_thread':
thread.o(.text+0x46): undefined reference to `pthread_attr_init'
thread.o(.text+0x57): undefined reference to `pthread_attr_setstacksize'
thread.o(.text+0x8b): undefined reference to `pthread_create'
thread.o(.text+0xa9): undefined reference to `pthread_attr_destroy'
thread.o(.text+0xbb): undefined reference to `pthread_detach'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function `PyThread_get_thread_ident':
thread.o(.text+0xe5): undefined reference to `pthread_self'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function `PyThread_allocate_lock':
thread.o(.text+0x198): undefined reference to `pthread_mutex_init'
thread.o(.text+0x1c2): undefined reference to `pthread_cond_init'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function `PyThread_free_lock':
thread.o(.text+0x20e): undefined reference to `pthread_mutex_destroy'
thread.o(.text+0x231): undefined reference to `pthread_cond_destroy'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function `PyThread_acquire_lock':
thread.o(.text+0x272): undefined reference to `pthread_mutex_lock'
thread.o(.text+0x2b6): undefined reference to `pthread_cond_wait'
thread.o(.text+0x2ec): undefined reference to `pthread_mutex_unlock'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function `PyThread_release_lock':
thread.o(.text+0x32e): undefined reference to `pthread_mutex_lock'
thread.o(.text+0x354): undefined reference to `pthread_mutex_unlock'
thread.o(.text+0x377): undefined reference to `pthread_cond_signal'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function `PyThread_allocate_sema':
thread.o(.text+0x3cc): undefined reference to `pthread_mutex_init'
thread.o(.text+0x3f6): undefined reference to `pthread_cond_init'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function `PyThread_free_sema':
thread.o(.text+0x442): undefined reference to `pthread_cond_destroy'
thread.o(.text+0x462): undefined reference to `pthread_mutex_destroy'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function `PyThread_down_sema':
thread.o(.text+0x49e): undefined reference to `pthread_mutex_lock'
thread.o(.text+0x4d1): undefined reference to `pthread_cond_wait'
thread.o(.text+0x517): undefined reference to `pthread_mutex_unlock'
/usr/local/lib/python2.2/config/libpython2.2.a(thread.o): In function `PyThread_up_sema':
thread.o(.text+0x547): undefined reference to `pthread_mutex_lock'
thread.o(.text+0x56d): undefined reference to `pthread_cond_signal'
thread.o(.text+0x58d): undefined reference to `pthread_mutex_unlock'
/usr/local/lib/python2.2/config/libpython2.2.a(posixmodule.o): In function `posix_openpty':
posixmodule.o(.text+0x127e): undefined reference to `openpty'
/usr/local/lib/python2.2/config/libpython2.2.a(posixmodule.o): In function `posix_forkpty':
posixmodule.o(.text+0x12d7): undefined reference to `forkpty'
/usr/local/lib/python2.2/config/libpython2.2.a(complexobject.o): In function `_Py_c_pow':
complexobject.o(.text+0x28f): undefined reference to `hypot'
complexobject.o(.text+0x2a3): undefined reference to `pow'
complexobject.o(.text+0x2ba): undefined reference to `atan2'
complexobject.o(.text+0x2e6): undefined reference to `exp'
complexobject.o(.text+0x2fa): undefined reference to `log'
complexobject.o(.text+0x31a): undefined reference to `cos'
complexobject.o(.text+0x32e): undefined reference to `sin'
/usr/local/lib/python2.2/config/libpython2.2.a(complexobject.o): In function `complex_remainder':
complexobject.o(.text+0xcdb): undefined reference to `floor'
/usr/local/lib/python2.2/config/libpython2.2.a(complexobject.o): In function `complex_divmod':
complexobject.o(.text+0xe02): undefined reference to `floor'
/usr/local/lib/python2.2/config/libpython2.2.a(complexobject.o): In function `complex_abs':
complexobject.o(.text+0x11e8): undefined reference to `hypot'
collect2: ld returned 1 exit status






Why?
Who can help me?
Thank you very much!


More information about the Python-list mailing list