我无法使用 Visual Studio 将 python.dll 构建为静态库 (/MTd) [英] I cannot build python.dll as a static library (/MTd) using Visual Studio

查看:33
本文介绍了我无法使用 Visual Studio 将 python.dll 构建为静态库 (/MTd)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Python 的 3.6.4 源代码版本.我可以使用 Visual Studio 作为动态库 (/MDd) 轻松构建它,我可以将 Python .dll 链接到我自己的代码并验证其操作.

I am working with the 3.6.4 source release of Python. I have no trouble building it with Visual Studio as a dynamic library (/MDd) I can link the Python .dll to my own code and verify its operation.

但是当我使用 (/MTd) 构建它(和我的代码)时,当我尝试使用 Python 程序打开文件时,它很快就会跑偏.read.cpp 中的调试断言失败(表达式:_osfile(fh) & FOPEN").我认为正在发生的是 Python .dll 与不正确的系统库链接.我不知道如何让它与正确的(静态库)链接.

But when I build it (and my code) with (/MTd) it soon runs off the rails when I try to open a file with a Python program. A Debug assertion fails in read.cpp ("Expression: _osfile(fh) & FOPEN"). What I believe is happening is the Python .dll is linking with improper system libraries. What I can't figure out is how to get it to link with the correct ones (static libraries).

推荐答案

这是我在构建和使用静态嵌入到另一个应用程序中的 python 所需要做的.

This is what I needed to do to build and use python statically embedded in another application.

构建静态python库(例如python36_d.lib、python36.lib)

To build the static python library (e.g., python36_d.lib, python36.lib)

  1. 将 python 解决方案 (pcbuild.sln) 中的所有项目转换为静态项目.这是大约 40 个项目,所以可能需要一段时间.这包括将库产品设置为静态库",并将所有/MD 和/MDd 构建选项设置为/MT 和/MTd.

  1. Convert ALL projects in the python solution (pcbuild.sln) to static. This is about 40 projects, so it may take awhile. This includes setting library products to be build as 'static lib', and setting all /MD and /MDd build options to /MT and /MTd.

至少对于 pythoncore 项目,将预处理定义更改为 Py_NO_ENABLE_SHARED.这告诉项目它将寻找来自静态库的调用.

For at least the pythoncore project alter the Preprocess define to be Py_NO_ENABLE_SHARED. This tells the project it will be looking for calls from static libraries.

随便找一个 pyconfig.h 文件,然后把它放在你的 Python 构建的 Include 区域.目前还不清楚这个文件是如何从 Windows 工具构建的,但似乎可以从其他来源获取一个文件并且它工作正常.人们可能会从您正在构建的代码的预编译版本中获取 pyconfig.h.[顺便说一下,我构建的 Python 是 3.6.5,是用 Windows 2015 构建的,更新 3.]

By hook or crook, find yourself a pyconfig.h file and put it in the Include area of your Python build. It is unclear how this file is built from Windows tools, but one seems to be able to snag one from other sources and it works ok. One could probably grab the pyconfig.h from the Pre-compiled version of the code you are building. [By the way, the Python I built was 3.6.5 and was built with Windows 2015, update 3.]

希望这能让您同时构建 python36.lib 和 python36_d.lib.现在您需要对您的应用程序项目进行更改以使其能够与 python 库链接.你需要这样做:

Hopefully, this should enable you to build both python36.lib and python36_d.lib. Now you need to make changes to your application project(s) to enable it to link with the python library. You need to do this:

  1. 将 Python Include 目录添加到 General->Include Directories 列表中.
  2. 将 Python 库目录添加到 General->Library Directory 列表中.这将是 ..\PCBuild\win32 和 ..\PCBuild\amd64.
  3. 将定义 Py_NO_ENABLE_SHARED 添加到 C/C++ -> 预处理器区域.
  4. 对于链接器->输入添加(对于版本)python36.lib;shlwapi.lib;version.lib 和(对于调试)python36_d.lib;shlwapi.lib;version.lib.

应该就是这样.它应该运行并工作.但还有一件事.为了能够运行,可执行文件需要访问python构建的Lib目录.因此,需要将其副本移动到可执行文件(包含嵌入式 Python)所在的任何位置.或者您可以将 Lib 区域添加到 windows 的执行路径中.这也应该有效.

And that should be it. It should run and work. But one more thing. In order to be able to function, the executable needs to access the Lib directory of the python build. So a copy of that needs to be moved to wherever the executable (containing the embedded python) resides. Or you can add the Lib area to the execution PATH for windows. That should work as well.

仅此而已.

这篇关于我无法使用 Visual Studio 将 python.dll 构建为静态库 (/MTd)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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