V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
justou
V2EX  ›  Python

尝试编译一个静态的 pythonxx.lib

  •  
  •   justou · 2017-05-03 14:00:08 +08:00 · 1604 次点击
    这是一个创建于 2767 天前的主题,其中的信息可能已经有所发展或是发生改变。

    有人尝试过用这个python-cmake-buildsystem编译静态的 python lib 么?

    想用静态 python lib 的原因:C 中调用 python 或者 cython 编译成 exe 运行时不需要 python 环境

    我试了下,使用它默认的配置,用 cmake+vs2015 成功编译出了 64 位的 python35.lib(大小 9.60M),里面包含:

    libpython-static.dir\Release\getbuildinfo.obj
    libpython-static.dir\Release\myreadline.obj
    libpython-static.dir\Release\parsetok.obj
    libpython-static.dir\Release\tokenizer.obj
    libpython-static.dir\Release\config.obj
    libpython-static.dir\Release\gcmodule.obj
    libpython-static.dir\Release\main.obj
    libpython-static.dir\Release\getpathp.obj
    libpython-static.dir\Release\_codecsmodule.obj
    libpython-static.dir\Release\errnomodule.obj
    libpython-static.dir\Release\signalmodule.obj
    libpython-static.dir\Release\_sre.obj
    libpython-static.dir\Release\symtablemodule.obj
    libpython-static.dir\Release\_weakref.obj
    libpython-static.dir\Release\xxsubtype.obj
    libpython-static.dir\Release\zipimport.obj
    libpython-static.dir\Release\accu.obj
    libpython-static.dir\Release\bytesobject.obj
    libpython-static.dir\Release\namespaceobject.obj
    libpython-static.dir\Release\odictobject.obj
    libpython-static.dir\Release\invalid_parameter_handler.obj
    libpython-static.dir\Release\abstract.obj
    libpython-static.dir\Release\boolobject.obj
    libpython-static.dir\Release\bytearrayobject.obj
    libpython-static.dir\Release\bytes_methods.obj
    libpython-static.dir\Release\capsule.obj
    libpython-static.dir\Release\cellobject.obj
    libpython-static.dir\Release\classobject.obj
    libpython-static.dir\Release\codeobject.obj
    libpython-static.dir\Release\complexobject.obj
    libpython-static.dir\Release\descrobject.obj
    libpython-static.dir\Release\dictobject.obj
    libpython-static.dir\Release\enumobject.obj
    libpython-static.dir\Release\exceptions.obj
    libpython-static.dir\Release\fileobject.obj
    libpython-static.dir\Release\floatobject.obj
    libpython-static.dir\Release\frameobject.obj
    libpython-static.dir\Release\funcobject.obj
    libpython-static.dir\Release\genobject.obj
    libpython-static.dir\Release\iterobject.obj
    libpython-static.dir\Release\listobject.obj
    libpython-static.dir\Release\longobject.obj
    libpython-static.dir\Release\memoryobject.obj
    libpython-static.dir\Release\methodobject.obj
    libpython-static.dir\Release\moduleobject.obj
    libpython-static.dir\Release\object.obj
    libpython-static.dir\Release\obmalloc.obj
    libpython-static.dir\Release\rangeobject.obj
    libpython-static.dir\Release\setobject.obj
    libpython-static.dir\Release\sliceobject.obj
    libpython-static.dir\Release\structseq.obj
    libpython-static.dir\Release\tupleobject.obj
    libpython-static.dir\Release\typeobject.obj
    libpython-static.dir\Release\unicodectype.obj
    libpython-static.dir\Release\unicodeobject.obj
    libpython-static.dir\Release\weakrefobject.obj
    libpython-static.dir\Release\acceler.obj
    libpython-static.dir\Release\bitset.obj
    libpython-static.dir\Release\firstsets.obj
    libpython-static.dir\Release\grammar1.obj
    libpython-static.dir\Release\grammar.obj
    libpython-static.dir\Release\listnode.obj
    libpython-static.dir\Release\metagrammar.obj
    libpython-static.dir\Release\node.obj
    libpython-static.dir\Release\parser.obj
    libpython-static.dir\Release\pgen.obj
    libpython-static.dir\Release\dl_nt.obj
    libpython-static.dir\Release\dynload_win.obj
    libpython-static.dir\Release\dynamic_annotations.obj
    libpython-static.dir\Release\fileutils.obj
    libpython-static.dir\Release\pyhash.obj
    libpython-static.dir\Release\pylifecycle.obj
    libpython-static.dir\Release\pystrhex.obj
    libpython-static.dir\Release\pystrtod.obj
    libpython-static.dir\Release\pytime.obj
    libpython-static.dir\Release\thread.obj
    libpython-static.dir\Release\asdl.obj
    libpython-static.dir\Release\ast.obj
    libpython-static.dir\Release\bltinmodule.obj
    libpython-static.dir\Release\ceval.obj
    libpython-static.dir\Release\codecs.obj
    libpython-static.dir\Release\compile.obj
    libpython-static.dir\Release\dtoa.obj
    libpython-static.dir\Release\errors.obj
    libpython-static.dir\Release\formatter_unicode.obj
    libpython-static.dir\Release\future.obj
    libpython-static.dir\Release\getargs.obj
    libpython-static.dir\Release\getcompiler.obj
    libpython-static.dir\Release\getcopyright.obj
    libpython-static.dir\Release\getopt.obj
    libpython-static.dir\Release\getplatform.obj
    libpython-static.dir\Release\getversion.obj
    libpython-static.dir\Release\graminit.obj
    libpython-static.dir\Release\import.obj
    libpython-static.dir\Release\importdl.obj
    libpython-static.dir\Release\marshal.obj
    libpython-static.dir\Release\modsupport.obj
    libpython-static.dir\Release\mysnprintf.obj
    libpython-static.dir\Release\mystrtoul.obj
    libpython-static.dir\Release\peephole.obj
    libpython-static.dir\Release\pyarena.obj
    libpython-static.dir\Release\pyctype.obj
    libpython-static.dir\Release\pyfpe.obj
    libpython-static.dir\Release\pymath.obj
    libpython-static.dir\Release\pystate.obj
    libpython-static.dir\Release\pystrcmp.obj
    libpython-static.dir\Release\Python-ast.obj
    libpython-static.dir\Release\pythonrun.obj
    libpython-static.dir\Release\random.obj
    libpython-static.dir\Release\structmember.obj
    libpython-static.dir\Release\symtable.obj
    libpython-static.dir\Release\sysmodule.obj
    libpython-static.dir\Release\traceback.obj
    libpython-static.dir\Release\_warnings.obj
    libpython-static.dir\Release\frozen.obj
    libpython-static.dir\Release\arraymodule.obj
    libpython-static.dir\Release\audioop.obj
    libpython-static.dir\Release\_bisectmodule.obj
    libpython-static.dir\Release\_math.obj
    libpython-static.dir\Release\cmathmodule.obj
    libpython-static.dir\Release\_codecs_cn.obj
    libpython-static.dir\Release\_codecs_hk.obj
    libpython-static.dir\Release\_codecs_iso2022.obj
    libpython-static.dir\Release\_codecs_jp.obj
    libpython-static.dir\Release\_codecs_kr.obj
    libpython-static.dir\Release\_codecs_tw.obj
    libpython-static.dir\Release\_collectionsmodule.obj
    libpython-static.dir\Release\_csv.obj
    libpython-static.dir\Release\_datetimemodule.obj
    libpython-static.dir\Release\_functoolsmodule.obj
    libpython-static.dir\Release\_heapqmodule.obj
    libpython-static.dir\Release\_iomodule.obj
    libpython-static.dir\Release\bufferedio.obj
    libpython-static.dir\Release\bytesio.obj
    libpython-static.dir\Release\fileio.obj
    libpython-static.dir\Release\iobase.obj
    libpython-static.dir\Release\stringio.obj
    libpython-static.dir\Release\textio.obj
    libpython-static.dir\Release\itertoolsmodule.obj
    libpython-static.dir\Release\_json.obj
    libpython-static.dir\Release\_localemodule.obj
    libpython-static.dir\Release\_lsprof.obj
    libpython-static.dir\Release\rotatingtree.obj
    libpython-static.dir\Release\mathmodule.obj
    libpython-static.dir\Release\mmapmodule.obj
    libpython-static.dir\Release\multibytecodec.obj
    libpython-static.dir\Release\parsermodule.obj
    libpython-static.dir\Release\_randommodule.obj
    libpython-static.dir\Release\_struct.obj
    libpython-static.dir\Release\_threadmodule.obj
    libpython-static.dir\Release\timemodule.obj
    libpython-static.dir\Release\atexitmodule.obj
    libpython-static.dir\Release\faulthandler.obj
    libpython-static.dir\Release\_opcode.obj
    libpython-static.dir\Release\_operator.obj
    libpython-static.dir\Release\_pickle.obj
    libpython-static.dir\Release\_stat.obj
    libpython-static.dir\Release\hashtable.obj
    libpython-static.dir\Release\_tracemalloc.obj
    libpython-static.dir\Release\msvcrtmodule.obj
    libpython-static.dir\Release\posixmodule.obj
    libpython-static.dir\Release\winreg.obj
    libpython-static.dir\Release\_winapi.obj
    libpython-static.dir\Release\md5module.obj
    libpython-static.dir\Release\sha1module.obj
    libpython-static.dir\Release\sha256module.obj
    libpython-static.dir\Release\sha512module.obj
    libpython-static.dir\Release\binascii.obj
    libpython-static.dir\Release\zlibmodule.obj
    libpython-static.dir\Release\adler32.obj
    libpython-static.dir\Release\compress.obj
    libpython-static.dir\Release\crc32.obj
    libpython-static.dir\Release\deflate.obj
    libpython-static.dir\Release\infback.obj
    libpython-static.dir\Release\inffast.obj
    libpython-static.dir\Release\inflate.obj
    libpython-static.dir\Release\inftrees.obj
    libpython-static.dir\Release\trees.obj
    libpython-static.dir\Release\uncompr.obj
    libpython-static.dir\Release\zutil.obj
    

    1)从 python 文档里 copy 了个例子:

    #include <Python.h>
    
    int
    main(int argc, char *argv[])
    {
    	wchar_t *program = Py_DecodeLocale(argv[0], NULL);
    	if (program == NULL) {
    		fprintf(stderr, "Fatal error: cannot decode argv[0]\n");
    		exit(1);
    	}
    	Py_SetProgramName(program);  /* optional but recommended */
    	Py_Initialize();
    	PyRun_SimpleString("from time import time,ctime\n"
    					   "print('Today is', ctime(time()))\n");
    	if (Py_FinalizeEx() < 0) {
    		exit(120);
    	}
    	PyMem_RawFree(program);
    	return 0;
    }
    

    用 cl 命令行( python35.lib,include 跟 test.c 在同一目录):

    call vcvarsall amd64
    cl /nologo test.c /I.\include python35.lib
    

    链接错误,所有用到的符号都没找到:

    test.obj : error LNK2019: unresolved external symbol __imp_PyMem_RawFree referenced in function main
    test.obj : error LNK2019: unresolved external symbol __imp_PyRun_SimpleStringFlags referenced in function main
    test.obj : error LNK2019: unresolved external symbol __imp_Py_SetProgramName referenced in function main
    test.obj : error LNK2019: unresolved external symbol __imp_Py_Initialize referenced in function main
    test.obj : error LNK2019: unresolved external symbol __imp_Py_DecodeLocale referenced in function main
    test.obj : error LNK2019: unresolved external symbol Py_FinalizeEx referenced in function main
    test.exe : fatal error LNK1120: 6 unresolved externals
    

    2)尝试 cython 转 c 的情况

    #in cy_test.pyx
    print('Static python lib.')
    

    编译( python35.lib,include 跟 cy_test.c 在同一目录):

    cython cy_test.pyx -3 --embed
    call vcvarsall amd64
    cl /nologo cy_test.c /I.\include python35.lib
    

    一大堆链接错误:

    	cy_test.obj : error LNK2019: unresolved external symbol __imp_PyObject_SetAttrString referenced in function PyInit_cy_test
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyObject_GetAttr referenced in function __Pyx_PyObject_GetAttrStr
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyMem_Malloc referenced in function __pyx_insert_code_object
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyMem_Realloc referenced in function __pyx_insert_code_object
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyBytes_FromStringAndSize referenced in function __Pyx_InitStrings
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyUnicode_FromStringAndSize referenced in function __Pyx_InitStrings
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyUnicode_FromString referenced in function __Pyx_CreateCodeObjectForTraceback
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyUnicode_FromFormat referenced in function __Pyx_CreateCodeObjectForTraceback
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyUnicode_InternFromString referenced in function __Pyx_InitStrings
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyUnicode_Decode referenced in function __Pyx_InitStrings
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyTuple_New referenced in function PyInit_cy_test
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyTuple_Pack referenced in function __Pyx_InitCachedConstants
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyDict_New referenced in function PyInit_cy_test
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyDict_SetItem referenced in function PyInit_cy_test
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyDict_GetItemString referenced in function PyInit_cy_test
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyDict_SetItemString referenced in function PyInit_cy_test
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyModule_GetDict referenced in function PyInit_cy_test
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyThreadState_Get referenced in function __Pyx_PyObject_Call
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyTraceBack_Here referenced in function __Pyx_AddTraceback
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyErr_WarnEx referenced in function __Pyx_check_binary_version
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyErr_SetString referenced in function __Pyx_PyObject_Call
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyErr_Occurred referenced in function __Pyx_PyObject_Call
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyErr_Format referenced in function __Pyx_GetBuiltinName
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyOS_snprintf referenced in function __Pyx_check_binary_version
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyModule_Create2 referenced in function PyInit_cy_test
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyErr_Print referenced in function wmain
    cy_test.obj : error LNK2019: unresolved external symbol __imp_Py_SetProgramName referenced in function wmain
    cy_test.obj : error LNK2019: unresolved external symbol __imp_Py_Initialize referenced in function wmain
    cy_test.obj : error LNK2019: unresolved external symbol __imp_Py_Finalize referenced in function wmain
    cy_test.obj : error LNK2019: unresolved external symbol __imp_Py_GetVersion referenced in function __Pyx_check_binary_version
    cy_test.obj : error LNK2019: unresolved external symbol __imp__Py_CheckRecursiveCall referenced in function __Pyx_PyObject_Call
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PySys_SetArgv referenced in function wmain
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyImport_GetModuleDict referenced in function PyInit_cy_test
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyImport_AddModule referenced in function PyInit_cy_test
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyObject_Call referenced in function __Pyx_PyObject_Call
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyCode_New referenced in function __Pyx_CreateCodeObjectForTraceback
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyFrame_New referenced in function __Pyx_AddTraceback
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyExc_ImportError referenced in function PyInit_cy_test
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyExc_NameError referenced in function __Pyx_GetBuiltinName
    cy_test.obj : error LNK2019: unresolved external symbol __imp_PyExc_SystemError referenced in function __Pyx_PyObject_Call
    cy_test.obj : error LNK2019: unresolved external symbol __imp__Py_CheckRecursionLimit referenced in function __Pyx_PyObject_Call
    cy_test.exe : fatal error LNK1120: 41 unresolved externals
    

    有人实际编译使用过静态的 python lib 么?求指点。

    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2378 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 00:59 · PVG 08:59 · LAX 16:59 · JFK 19:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.