3
我一直在學習通過swig來擴展python。在痛飲導師,它寫道:爲什麼python可以將共享庫作爲模塊導入?
Building a Python module
Turning C code into a Python module is also easy. Simply do the following (shown for Irix, see the SWIG Wiki Shared Libraries page for help with other operating systems):
unix % swig -python example.i
unix % gcc -c example.c example_wrap.c \
-I/usr/local/include/python2.1
unix % ld -shared example.o example_wrap.o -o _example.so
We can now use the Python module as follows :
>>> import example
>>> example.fact(5)
120
>>> example.my_mod(7,3)
1
>>> example.get_time()
'Sun Feb 11 23:01:07 1996'
>>>
這意味着,共享庫可以被導入爲蟒蛇module.But我知道,共享庫文件是一個對象文件,其中包含許多機器代碼和一些額外的信息,而普通的python模塊是ascii文件或字節碼文件,python虛擬機如何執行機器代碼,我很困惑。
James Kanze,你的回答很好。我想了解更多關於這個主題的內容,我使用Google搜索但只有很少的材料,可以提供一些鏈接,謝謝。 –
http://docs.python.org/2/extending /提供了一個很好的初始介紹,http://docs.python.org/2/c-api/是參考資料,包含所有的細節。 (先閱讀第一本,直到你知道足夠的知道你在找什麼,第二本沒什麼幫助。) –