CPython源码 - 如何构建一个STATIC python26.lib? [英] CPython sources - how to build a STATIC python26.lib?

查看:693
本文介绍了CPython源码 - 如何构建一个STATIC python26.lib?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Cython编译我的hello.pyx文件到exe。

I'm trying to compile my hello.pyx file to an exe using Cython.

第一步是使用命令cython --cplus --embed hello.pyx将hello.pyx编译成hello.cpp文件。 嵌入选项意味着生成嵌入Python解释器的main()函数。我试图创建一个没有依赖的独立exe。

First step was to compile the hello.pyx into a hello.cpp file using command "cython --cplus --embed hello.pyx". Embed option means to Generate a main() function that embeds the Python interpreter. I'm trying to create an independent exe with no dependencies.

hello.cpp 我有一个 #includePython.h code>,因此我要从这里下载Python源代码: http:// www.python.org/download/releases/2.6.6/ ,选择gzip压缩源tar球(2.6.6) 。我添加包括dir和获取错误关于缺少Python26.lib。所以我试图编译它。默认构建创建python26.lib但是它只有200 KB,这不是一个静态库,当试图编译hello.cpp我得到丢失的引用错误。

In hello.cpp I have an #include "Python.h", so I'm downloading Python sources from here: http://www.python.org/download/releases/2.6.6/ , choosing Gzipped source tar ball (2.6.6). I add include dir and get error about missing Python26.lib. So I am trying to compile it. The default build creates python26.lib but it's only 200 KB, this is not a static lib, when trying to compile hello.cpp I get missing references errors.

在README我没有看到任何有用的指导如何静态构建它。但是,我已经google了一个在线README文件,对于最新版本的python在trunk(3.x),有一些有用的信息静态构建:

In README I don't see any useful instructions of how to build it statically. However, I've googled an online README file, for the latest version of python in trunk (3.x), there is some useful info about static building:

< a href =http://svn.python.org/projects/python/trunk/PCbuild/readme.txt =nofollow> http://svn.python.org/projects/python/trunk/PCbuild/ readme.txt


解决方案没有静态库的配置。然而很容易
它构建一个静态库而不是一个DLL。您只需要将
Configuration Type设置为Static Library(.lib),并将预处理器
宏Py_ENABLE_SHARED改为Py_NO_ENABLE_SHARED。您可能还需要
将运行时库从多线程DLL(/ MD)更改为
多线程(/ MT)。

The solution has no configuration for static libraries. However it is easy it build a static library instead of a DLL. You simply have to set the "Configuration Type" to "Static Library (.lib)" and alter the preprocessor macro "Py_ENABLE_SHARED" to "Py_NO_ENABLE_SHARED". You may also have to change the "Runtime Library" from "Multi-threaded DLL (/MD)" to "Multi-threaded (/MT)".

但是这里没有太多的细节,我是否将所有项目都设置为静态库的解决方案?或者pythoncore只? Python和Pythonw项目是应用程序,所以我只能将运行时库更改为/ MT。这是我正在做的步骤:

But still there aren't too many details here, do I set all projects in the solution to build as static library? Or pythoncore only? "Python" and "Pythonw" projects are "Applications", so there I can only change runtime library to /MT. These are the steps I am doing:


  • 更改为发布模式。

  • Python - 设置 / MT 并添加预处理器定义 Py_NO_ENABLE_SHARED

  • < Python $配置类型到静态库(.lib),/ MT,替换预处理器定义 Py_ENABLE_SHARED $ c> Py_NO_ENABLE_SHARED

  • 项目Pythonw - 与Python相同。

  • onPython。

  • Changing to "Release" mode.
  • Project "Python" - setting /MT and adding preprocessor definition Py_NO_ENABLE_SHARED
  • Project "Pythoncore" - configuration type to Static library (.lib), /MT, replacing preprocessor definition Py_ENABLE_SHARED with Py_NO_ENABLE_SHARED
  • Project "Pythonw" - the same as in "Python".
  • I right click build on "Python".

构建的摘要:


Build:5个成功, 1个失败,0个最新,0个跳过

Build: 5 succeeded, 1 failed, 0 up-to-date, 0 skipped



kill_python (ok)
make_buildinfo (ok)
make_versioninfo (ok)
pythoncore (ok)
w9xpopen (ok)
python (4 errors)

失败的项目是Python,这里是日志: / p>

The project that failed is "Python", here is the log:

------ Build started: Project: python, Configuration: Release Win32 ------
Compiling...
python.c
Compiling resources...
Microsoft (R) Windows (R) Resource Compiler Version 6.1.7600.16385
Copyright (C) Microsoft Corporation.  All rights reserved.
Linking...
pythoncore.lib(sysmodule.obj) : error LNK2019: unresolved external symbol __Py_svnversion referenced in function _svnversion_init
pythoncore.lib(getversion.obj) : error LNK2019: unresolved external symbol _Py_GetBuildInfo referenced in function _Py_GetVersion
pythoncore.lib(dynload_win.obj) : error LNK2019: unresolved external symbol __Py_DeactivateActCtx referenced in function __PyImport_GetDynLoadFunc
pythoncore.lib(dynload_win.obj) : error LNK2019: unresolved external symbol __Py_ActivateActCtx referenced in function __PyImport_GetDynLoadFunc
d:\python\src4\PCbuild\\python.exe : fatal error LNK1120: 4 unresolved externals
Build Time 0:02

我看看/ PCbuild /目录,我看到pythoncore.lib存在(10MB),但没有python26。 lib。

I look at the /PCbuild/ directory and I see that "pythoncore.lib" exists (10MB), but there is no "python26.lib".

当我google这些错误,我只在mail.pythong.org找到一个对话:
http://www.groupsrv.com/computers/about397568.html

When I google for these errors, I only find one conversation at mail.pythong.org: http://www.groupsrv.com/computers/about397568.html

我尝试在pythoncore项目中删除prepreocessor定义_USRDLL,但仍然是相同的错误。

I tried removing prepreocessor definition "_USRDLL" in pythoncore project, but still the errors are the same.

pythoncore中的预处理器定义是:

The preprocessor definitions in pythoncore are:

_USRDLL
Py_BUILD_CORE
Py_NO_ENABLE_SHARED
WIN32

当我点击编辑时,我在另一个框中看到Inherited values,即:

When I click edit I see in another box "Inherited values" which are:

NDEBUG
_WIN32

那Ndebug很奇怪,因为我改变了模式

That Ndebug is strange, because I've changed the mode to Release.

我使用安装了所有服务包的Visual Studio 2008(非Express)。

I am using Visual Studio 2008 (not Express) with all service packs installed.

Btw 。早上我试图构建整个解决方案,但有一些关于丢失文件的错误,其路径以../../ dba - 开头。

Btw. earlier I tried to build the whole solution, but there were some errors about missing files, whose path start with: "../../dba-".

我试过编译Python的2.6.2和2.6.6版本,但是它们都会引起相同的错误: pythoncore.lib(sysmodule.obj):error LNK2019:未解决的外部符号

I tried compiling 2.6.2 and 2.6.6 versions of Python, but they both throw the same errors: pythoncore.lib(sysmodule.obj) : error LNK2019: unresolved external symbol

推荐答案

我一直在构建从2.4到2.7的Python静态,

I've been building Python static from 2.4 through to 2.7, and it does take a bit of work to set up.

您需要更新解决方案中所有已启用项目的配置,将它们设置为/ MT。要构建Python静态,它引用的所有内容(包括SQLite或OpenSSL等库)也必须是静态的。这是真的甚至.pyd模块,实际上没有包括,因为否则他们将无法导入以后。

You do need to update the configuration for all enabled projects in the solution, setting them to /MT. To build Python static, everything that it references - including libraries like SQLite or OpenSSL - must also be static. This is true even of the .pyd modules, which aren't actually included, since otherwise they won't be importable later.

您可能还需要进行此编辑近模块/ socketmodule.h的顶部:

You may also need to make this edit near the top of Modules/socketmodule.h:

#else /* MS_WINDOWS */
# define inet_pton _msvc_inet_pton
# define inet_ntop _msvc_inet_ntop
# include <winsock2.h>
# include <ws2tcpip.h>
# include <wspiapi.h>
# undef inet_pton
# undef inet_ntop

,可能需要的其他一些事情:

Once you get beyond the immediate error, some other things that are probably necessary:


  • 禁用嵌入清单的创建;看到行〜650在distutils msvc9compiler.py;

  • Disable creation of embedded manifests; see line ~650 in distutils msvc9compiler.py; the fact that it creates these even when building with /MT is a bug.

在distutils msvccompiler / msvc9compiler.py中将/ MD更改为/ MT,因为否则第三方库不会正确建立。

Change /MD to /MT in distutils msvccompiler/msvc9compiler.py, since otherwise thirdparty libs won't build correctly.

这篇关于CPython源码 - 如何构建一个STATIC python26.lib?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆