{"id":728,"date":"2012-07-03T21:00:22","date_gmt":"2012-07-03T13:00:22","guid":{"rendered":"http:\/\/www.gejo.in\/?p=728"},"modified":"2012-07-03T21:00:22","modified_gmt":"2012-07-03T13:00:22","slug":"linux%e4%b8%8b%e7%bc%96%e8%af%91pythonc-api%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.mujins.com\/?p=728","title":{"rendered":"Linux\u4e0b\u7f16\u8bd1Python\/C API\u95ee\u9898"},"content":{"rendered":"<p>\u5728Linux\u4e0b\u7f16\u8bd1python c api\u65f6\u9047\u5230 \u7c7b\u4f3c\u4e0b\u9762\u7684\u9519\u8bef:<\/p>\n<pre class=\"brush:bash\"> undefined reference to `Py_Initialize<\/pre>\n<p>\u5f53\u7136\uff0c\u5982\u679c\u4f60\u5728windows\u5e73\u53f0\u4e0b\uff0c\u4f7f\u7528IDE\u53ef\u80fd\u4e0d\u4f1a\u9047\u5230\u8fd9\u6837\u95ee\u9898\u3002\u4f46\u662f\u5728linux\uff0cunix\u4e0b\u5462\uff0c\u8981\u81ea\u5df1\u52a8\u624b\u5199Makefile\u5462\uff1f<br \/>\n\u731c\u6d4b\u8fd9\u53ef\u80fd\u662f\u7f3a\u5c11\u67d0\u4e9b\u5e93\uff0cGoogle\u4e00\u4e0b\u53ef\u4ee5\u627e\u5230\u7b54\u6848\uff0c\u8fd9\u91cc\u6587\u7ae0\u4f1a\u7ed9\u51fa\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u662f\u5728\u591a\u4e00\u4e0b\u5e9f\u8bdd\u7ed9\u521a\u521a\u63a5\u89e6python C API \u7684\u7ae5\u978b\u4eec\uff0c\u8fd9\u4e0d\u662f\u6240\u8c13\u7684\u6280\u672f\u6587\u7ae0\uff0c\u53ea\u662f\u5e0c\u671b\u4f17\u591a\u521a\u521a\u6b65\u5165python\u5927\u95e8\u9047\u5230\u6b64\u7c7b\u95ee\u9898\u7684\u4e00\u4e2a\u53c2\u8003\u3002\u5f53\u7136\u6211\u4e5f\u662f\u83dc\u9e1f&#8230;<\/p>\n<p><a href=\"https:\/\/www.gigiwangs.com\/wp-content\/uploads\/2012\/07\/python-logo.png\" data-lightbox=\"gal[728]\"><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-729\" title=\"python-logo\" src=\"https:\/\/www.gigiwangs.com\/wp-content\/uploads\/2012\/07\/python-logo.png\" alt=\"\" width=\"401\" height=\"301\" \/><\/a><\/p>\n<p>Linux\u4e0b\u5b89\u88c5python\uff0c\u5f53\u524d\u7684\u53d1\u884c\u7248\u901a\u5e38\u5df2\u7ecf\u5b89\u88c5\u4e86python\uff0c\u4f46\u662f\u53ef\u80fd\u7248\u672c\u7b49\u539f\u56e0\uff0c\u5982\u679c\u9700\u8981\u5b89\u88c5\uff0c\u5efa\u8bae\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5\uff1a<\/p>\n<p>\u5230\u8fd9\u91cc\u4e0b\u8f7d\u6240\u5bf9\u5e94\u7684\u7248\u672c\uff1a<a href=\"http:\/\/python.org\/\">http:\/\/python.org\/<\/a>\u89e3\u538b\uff0ccd\u5230\u89e3\u538b\u540e\u7684python(X.X.X).<\/p>\n<pre class=\"brush:bash\"># .\/configure\n#  make\n#  make install<\/pre>\n<p>\u8fd9\u6837\u7f16\u8bd1\u5b89\u88c5\u5b8c\u6210\uff0c\u5728Terminal\u4e0b\u6572python\uff1a<\/p>\n<pre class=\"brush:bash\">Python 2.7.3 (default, Jul  3 2012, 18:01:45) \n[GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n&gt;&gt;&gt;<\/pre>\n<p>OK,\u73b0\u5728\u6765\u8bd5\u8bd5Python\/C API.\u7f51\u4e0a\u7684\u4f8b\u5b50\u5f88\u591a\uff0c\u968f\u4fbf\u62ff\u6765\u4e00\u4e2a\u6700\u7b80\u5355\u7684,\u5047\u8bbe\u4fdd\u5b58\u4e3amain.c\uff1a<\/p>\n<pre class=\"brush:c\">\/\/This is A sample.\n#include \"Python.h\"\n\nint main()\n{\n        Py_Initialize();\n        printf(\"This is a C-Python Program.\\n\");\n        PyRun_SimpleString(\"print(\\\"Hello,Python\\\")\");\n        Py_Finalize();\n        return 0;\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5199\u4e00\u4e2aMakefile,\u56e0\u4e3apython\u7684\u5b89\u88c5\u76ee\u5f55\u90fd\u91c7\u53d6\u4e86\u9ed8\u8ba4\uff1a<\/p>\n<pre class=\"brush:other\">ALL\t= .\/tc\nCC\t= gcc\nRM\t= rm\nLIBS\t=  -lpthread -lm -ldl -lutil \nINCL\t= \/usr\/local\/include\/python2.7\n\nOBJ\t= main.o\nall:$(ALL)\n\n.\/tc : $(OBJ) \n\t$(CC)  $(OBJ) -I$(INCL) -L$(LIBS) -L\/usr\/localb\/ -lpython2.7 -o $@\n\nclean:\n\t$(RM) $(OBJ) \n\t$(RM) $(ALL)<\/pre>\n<p>\u597d\u4e86\uff0c\u7f16\u8bd1\uff0c\u8fd0\u884c\uff1a<\/p>\n<pre class=\"brush:other\">This is a C-Python Program\nHello,Python<\/pre>\n<p>\u90a3\u4e48\uff0c\u6587\u7ae0\u5f00\u5934\u63d0\u5230\u7684\u95ee\u9898\u5462\uff0c\u6ca1\u5fd8\u8bb0\u5462\u3002\u662f\u56e0\u4e3a\u5728\u7f16\u8bd1\u65f6\u5fd8\u8bb0\u94fe\u63a5\u8fd9\u4e9b\u5e93\uff1a<\/p>\n<pre><span style=\"color: #ff0000;\">-lpthread -lm -ldl -lutil<\/span><\/pre>\n<pre>\u4e0d\u8981\u5fd8\u4e86,\u8fd8\u6709\uff0c\u6ce8\u610fPython.h\u7684\u8def\u5f84\uff0cP\u662f\u5927\u5199\uff01<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Linux\u4e0b\u7f16\u8bd1python c api\u65f6\u9047\u5230 \u7c7b\u4f3c\u4e0b\u9762\u7684\u9519\u8bef: undefined reference to `Py_Initialize \u5f53\u7136\uff0c\u5982\u679c\u4f60\u5728windows\u5e73\u53f0\u4e0b\uff0c\u4f7f\u7528IDE\u53ef\u80fd\u4e0d\u4f1a\u9047\u5230\u8fd9\u6837\u95ee\u9898\u3002\u4f46\u662f\u5728linux\uff0cunix\u4e0b\u5462\uff0c\u8981\u81ea\u5df1\u52a8\u624b\u5199Makefile\u5462\uff1f \u731c\u6d4b\u8fd9\u53ef\u80fd\u662f\u7f3a\u5c11\u67d0\u4e9b\u5e93\uff0cGoogle\u4e00\u4e0b\u53ef\u4ee5\u627e\u5230\u7b54\u6848\uff0c\u8fd9\u91cc\u6587\u7ae0\u4f1a\u7ed9\u51fa\u539f\u56e0\u548c\u89e3\u51b3\u65b9\u6848\uff0c\u4f46\u662f\u5728\u591a\u4e00\u4e0b\u5e9f\u8bdd\u7ed9\u521a\u521a\u63a5\u89e6python C API \u7684\u7ae5\u978b\u4eec\uff0c\u8fd9\u4e0d\u662f\u6240\u8c13\u7684\u6280\u672f\u6587\u7ae0\uff0c\u53ea\u662f\u5e0c\u671b\u4f17\u591a\u521a\u521a\u6b65\u5165python\u5927\u95e8\u9047\u5230\u6b64\u7c7b\u95ee\u9898\u7684\u4e00\u4e2a\u53c2\u8003\u3002\u5f53\u7136\u6211\u4e5f\u662f\u83dc\u9e1f&#8230; Linux\u4e0b\u5b89\u88c5python\uff0c\u5f53\u524d\u7684\u53d1\u884c\u7248\u901a\u5e38\u5df2\u7ecf\u5b89\u88c5\u4e86python\uff0c\u4f46\u662f\u53ef\u80fd\u7248\u672c\u7b49\u539f\u56e0\uff0c\u5982\u679c\u9700\u8981\u5b89\u88c5\uff0c\u5efa\u8bae\u6e90\u7801\u7f16\u8bd1\u5b89\u88c5\uff1a \u5230\u8fd9\u91cc\u4e0b\u8f7d\u6240\u5bf9\u5e94\u7684\u7248\u672c\uff1ahttp:\/\/python.org\/\u89e3\u538b\uff0ccd\u5230\u89e3\u538b\u540e\u7684python(X.X.X). # .\/configure # make # make install \u8fd9\u6837\u7f16\u8bd1\u5b89\u88c5\u5b8c\u6210\uff0c\u5728Terminal\u4e0b\u6572python\uff1a Python 2.7.3 (default, Jul 3 2012, 18:01:45) [GCC 4.7.0 20120507 (Red Hat 4.7.0-5)] on linux2 Type &#8220;help&#8221;, &#8220;copyright&#8221;, &#8220;credits&#8221; or &#8220;license&#8221; for more information. &gt;&gt;&gt; OK,\u73b0\u5728\u6765\u8bd5\u8bd5Python\/C API.\u7f51\u4e0a\u7684\u4f8b\u5b50\u5f88\u591a\uff0c\u968f\u4fbf\u62ff\u6765\u4e00\u4e2a\u6700\u7b80\u5355\u7684,\u5047\u8bbe\u4fdd\u5b58\u4e3amain.c\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":729,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,21,25,5],"tags":[22,13,26],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.mujins.com\/index.php?rest_route=\/wp\/v2\/posts\/728"}],"collection":[{"href":"https:\/\/www.mujins.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mujins.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mujins.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mujins.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=728"}],"version-history":[{"count":0,"href":"https:\/\/www.mujins.com\/index.php?rest_route=\/wp\/v2\/posts\/728\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.mujins.com\/index.php?rest_route=\/"}],"wp:attachment":[{"href":"https:\/\/www.mujins.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=728"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mujins.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=728"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mujins.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=728"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}