简体   繁体   English

使用 MSVC 上定义的 DEBUG 编译 python 模块

[英]Compiling python modules with DEBUG defined on MSVC

Python rather stupidly has a pragma directive in its include files that forces a link against python26_d.lib when the DEBUG preprocessor variable is defined. Python 相当愚蠢地在它的包含文件中有一个 pragma 指令,当定义了DEBUG预处理器变量时,它会强制链接到python26_d.lib This is a problem because the python installer doesn't come with python26_d.lib !这是一个问题,因为 python 安装程序没有python26_d.lib So I can't build applications in MSVC in debug mode.所以我无法在调试模式下在 MSVC 中构建应用程序。 If I temporarily #undef DEBUG for just one file I get many complaints about inconsistent DLL linkage.如果我只是对一个文件进行临时#undef DEBUG ,我会收到很多关于 DLL 链接不一致的抱怨。 If I change the pragma in pythons include file I get undefined references to various debug functions.如果我更改 python 包含文件中的编译指示,我会得到对各种调试函数的未定义引用。

I have tried compiling my own version of python but its somehow different enough from the python that gets distributed that I can't use my modules with apps built with the vanilla version of python我试过编译我自己的 python 版本,但它与分发的 python 有很大不同,我不能将我的模块与使用 python 的香草版本构建的应用程序一起使用

Can anyone give me any advice on how to get round this?任何人都可以就如何解决这个问题给我任何建议吗?

From python list来自python列表

As a workaround to the situation, try to copy the file python26.dll to python26_d.dll.作为这种情况的解决方法,尝试将文件 python26.dll 复制到 python26_d.dll。 (I'm not sure this will work; you say you are building a SWIG library in debug mode, and it's possible that SWIG will try to use features of the Python debugging version. If that's the case, you'll have no choice but to use the debugging version of Python.) (我不确定这是否可行;您说您正在调试模式下构建 SWIG 库,并且 SWIG 可能会尝试使用 Python 调试版本的功能。如果是这种情况,您将别无选择,只能使用 Python 的调试版本。)

Edit: From comments:编辑:来自评论:

You should also edit pyconfig.h and comment out the line "#define Py_DEBUG" (line 374)您还应该编辑 pyconfig.h 并注释掉“#define Py_DEBUG”行(第 374 行)

After you comment out "#define Py_DEBUG" on line 332 and modify在第 332 行注释掉“#define Py_DEBUG”并修改后

#   ifdef _DEBUG
#    pragma comment(lib,"python26_d.lib")
#   else

to

#   ifdef _DEBUG
#    pragma comment(lib,"python26.lib")
#   else

you do not need to python26_d.lib anymore.你不再需要 python26_d.lib 了。

You can also go the other way: switch to «Release» and then debug it.您也可以采用另一种方式:切换到 «Release»,然后对其进行调试。 you need to enable generation of debugging symbols info in project properties in compiler and linker prefs;您需要在编译器和链接器首选项中的项目属性中启用调试符号信息的生成; MSDN here will tell you exactly what options you need to set to debug a release build. MSDN在这里将准确地告诉您需要设置哪些选项来调试发布版本。

Based on all answers I successfully disabled _DEBUG temporary:根据所有答案,我成功禁用了_DEBUG临时:

#if _DEBUG
  #define _DEBUG_IS_ENABLED
  #undef _DEBUG
#endif
#include "pyconfig.h"
#if defined(_DEBUG_IS_ENABLED)
  #define _DEBUG
#endif

This works also when linking with static libraries.这在与静态库链接时也有效。 I made a copy of python26.lib, and renamed it python26_d.lib.我复制了python26.lib,并将其重命名为python26_d.lib。 I commented out the line #define PY_DEBUG in pyconfig.h.我在 pyconfig.h 中注释掉了 #define PY_DEBUG 行。 Also changed the pragma to "pragma comment(lib,"python26.lib")" on line 332. Voila!还在第 332 行将 pragma 更改为“pragma comment(lib,"python26.lib")”。瞧! It worked.有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM