简体   繁体   English

Python嵌入式C代码编译错误

[英]Python embedded C code compilation error

I have recently bought a new computer and tried to move a python project from my old computer to my new one. 我最近买了一台新电脑,并试图将一个python项目从我的旧电脑移到我的新电脑上。 The python project used some directx9 C++ code (with #include <Python.h>) to take screenshots and to compile this C++ file I used G++. python项目使用了一些directx9 C ++代码(使用#include <Python.h>)来截取屏幕并编译我使用G ++的这个C ++文件。

Without changing any of the code and after installing G++ and Python I found that what I used to compile the code returns an error: 在不更改任何代码的情况下,在安装G ++和Python之后,我发现我用来编译代码会返回错误:

    g++ -std=c++11 -shared -IC:\Python27\include -LC:\Python27\libs ScreenShots/PictureStr.cpp -lpython27 -ld3d9 -lole32 -o ScreenShots/PictureStr.pyd

returns this: 返回:

    C:\Python27\libs/libpython27.a: error adding symbols: File format not recognized
    collect2.exe: error: ld returned 1 exit status

(removing the -ld3d9 -lole32 doesn't change anything) (删除-ld3d9 -lole32不会改变任何东西)

I have no idea why this happened (and doesn't happen on any other computer that I tried or my old computer) and I wasn't able to find anything that could cause this. 我不知道为什么会发生这种情况(并且不会发生在我尝试的任何其他计算机或我的旧计算机上)并且我无法找到任何可能导致这种情况发生的事情。

In case this is relevant I use windows 10 64bit (in both my old computer and my new one). 如果这是相关的,我使用Windows 10 64bit(在我的旧计算机和新计算机中)。

Perhaps I need to recompile the python libs or that specific file so any advice on how to do that is also greatly appreciated. 也许我需要重新编译python库或那个特定的文件,所以任何关于如何做到这一点的建议也非常感谢。

Thank you for reading this, any advice is welcome. 感谢您阅读本文,欢迎任何建议。

I found a solution, I answered bellow. 我找到了一个解决方案,我回答道。

I tried coping libpython27.a from my old computer to my new one and try using gcc again and it worked, it seems like the new 64bit python 2.7 installation libpython27.a is broken. 我尝试将libpython27.a从旧计算机上复制到我的新计算机并尝试再次使用gcc并且它有效,似乎新的64位python 2.7安装libpython27.a已损坏。 If someone else runs into this problem, try recreating your libpython27.a file. 如果其他人遇到此问题,请尝试重新创建libpython27.a文件。

To recreate libpython.a run: 要重新创建libpython.a运行:

pexports "C:\Windows\SysWOW64\python27.dll" > "C:\Python27\libs\python27.def"
dlltool --dllname "C:\Windows\SysWOW64\python27.dll" --def "C:\Python27\libs\python27.def" --output-lib "C:\Python27\libs\libpython27.a"

Where C:\\Python27\\ is your python folder and should be System32 instead of SysWOW64 if it's a 32 bit computer. 其中C:\\Python27\\是你的python文件夹,如果是32位计算机应该是System32而不是SysWOW64。

To install pexports install mingw and run mingw-get install pexports. 要安装pexports install mingw并运行mingw-get install pexports。

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

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