无法编译需要 C99 编译器 (AFAIU) 的 pyethash python 包。错误 - 无法打开包含文件:"alloca.h"

Cannot compile pyethash python package which requires C99 compiler (AFAIU). Error - Cannot open include file: 'alloca.h'

本文关键字:错误 文件 alloca 包含 python C99 编译 编译器 pyethash AFAIU      更新时间:2023-10-16

问题

当手动或使用pip安装pyethash时,我会得到相同类型的错误:

致命错误C1083:无法打开包含文件:"alloca.h":没有这样的文件或目录错误:命令"C:\Program Files(x86)\Microsoft VisualStudio 9.0\VC\BIN\aamd64\cl.exe'失败,退出状态为2

相关且已尝试:

  • 错误:找不到vcvarsall.bat
  • 为Python 2.7构建/安装C模块时出错

没有答案的类似问题:

  • 注释4289832_26127562
  • 安装程序脚本已退出,出现错误cl.exe"失败,退出状态为2"

其他事实:

  • 已成功安装需要C++编译器的Crypto和scrypt
  • pyethash-core.c源文件头中提到的另外3个文件在我的驱动器上没有:
    • alloca.h
    • stdint.h
    • stdlib.h

系统

python 2.7.11(v2.7.11:6d1b6a68f7752015年12月5日20:40:30)[MSC v.1500 64位(AMD64)],windows 8.1 x64

\ethash-master>python setup.py安装

PS C:pyethereumethash-master> python setup.py install
running install
running build
running build_ext
building 'pyethash' extension
C:Program Files (x86)Microsoft Visual Studio 9.0VCBINamd64cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -IC:Python2
7include -IC:Python27PC /Tcsrc/python/core.c /Fobuildtemp.win-amd64-2.7Releasesrc/python/core.obj -Isrc/ -std=gnu9
9 -Wall
cl : Command line warning D9002 : ignoring unknown option '-std=gnu99'
core.c
c:program files (x86)microsoft visual studio 9.0vcincludecodeanalysissourceannotations.h(81) : warning C4820: 'Pre
Attribute' : '4' bytes padding added after data member 'Access'
c:program files (x86)microsoft visual studio 9.0vcincludecodeanalysissourceannotations.h(96) : warning C4820: 'Pre
Attribute' : '4' bytes padding added after data member 'NullTerminated'
c:program files (x86)microsoft visual studio 9.0vcincludecodeanalysissourceannotations.h(112) : warning C4820: 'Po
stAttribute' : '4' bytes padding added after data member 'Access'
c:program files (x86)microsoft visual studio 9.0vcincludecodeanalysissourceannotations.h(191) : warning C4820: 'Pr
eRangeAttribute' : '4' bytes padding added after data member 'Deref'
c:program files (x86)microsoft visual studio 9.0vcincludecodeanalysissourceannotations.h(203) : warning C4820: 'Po
stRangeAttribute' : '4' bytes padding added after data member 'Deref'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEio.h(60) : warning C4820: '_finddata32i64_t' : '4' bytes p
adding added after data member 'name'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEio.h(64) : warning C4820: '_finddata64i32_t' : '4' bytes p
adding added after data member 'attrib'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEio.h(73) : warning C4820: '__finddata64_t' : '4' bytes pad
ding added after data member 'attrib'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEio.h(78) : warning C4820: '__finddata64_t' : '4' bytes pad
ding added after data member 'name'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEio.h(126) : warning C4820: '_wfinddata64i32_t' : '4' bytes
 padding added after data member 'attrib'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEio.h(131) : warning C4820: '_wfinddata64i32_t' : '4' bytes
 padding added after data member 'name'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEio.h(135) : warning C4820: '_wfinddata64_t' : '4' bytes pa
dding added after data member 'attrib'
C:Program FilesMicrosoft SDKsWindowsv7.0includebasetsd.h(114) : warning C4668: '__midl' is not defined as a prepro
cessor macro, replacing with '0' for '#if/#elif'
C:Program FilesMicrosoft SDKsWindowsv7.0includebasetsd.h(424) : warning C4668: '_WIN32_WINNT' is not defined as a
preprocessor macro, replacing with '0' for '#if/#elif'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEstdio.h(62) : warning C4820: '_iobuf' : '4' bytes padding
added after data member '_cnt'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEstdio.h(381) : warning C4255: '_get_printf_count_output' :
 no function prototype given: converting '()' to '(void)'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEstdlib.h(215) : warning C4255: '_get_purecall_handler' : n
o function prototype given: converting '()' to '(void)'
c:python27includepyport.h(206) : warning C4668: 'SIZEOF_PID_T' is not defined as a preprocessor macro, replacing with
 '0' for '#if/#elif'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEmath.h(41) : warning C4820: '_exception' : '4' bytes paddi
ng added after data member 'type'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEsys/stat.h(111) : warning C4820: '_stat32' : '2' bytes pad
ding added after data member 'st_gid'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEsys/stat.h(127) : warning C4820: 'stat' : '2' bytes paddin
g added after data member 'st_gid'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEsys/stat.h(143) : warning C4820: '_stat32i64' : '2' bytes
padding added after data member 'st_gid'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEsys/stat.h(144) : warning C4820: '_stat32i64' : '4' bytes
padding added after data member 'st_rdev'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEsys/stat.h(148) : warning C4820: '_stat32i64' : '4' bytes
padding added after data member 'st_ctime'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEsys/stat.h(157) : warning C4820: '_stat64i32' : '2' bytes
padding added after data member 'st_gid'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEsys/stat.h(171) : warning C4820: '_stat64' : '2' bytes pad
ding added after data member 'st_gid'
C:Program Files (x86)Microsoft Visual Studio 9.0VCINCLUDEsys/stat.h(172) : warning C4820: '_stat64' : '4' bytes pad
ding added after data member 'st_rdev'
c:python27includeobject.h(358) : warning C4820: '_typeobject' : '4' bytes padding added after data member 'tp_flags'
c:python27includeobject.h(411) : warning C4820: '_typeobject' : '4' bytes padding added after data member 'tp_version
_tag'
c:python27includeunicodeobject.h(420) : warning C4820: '<unnamed-tag>' : '4' bytes padding added after data member 'h
ash'
c:python27includeintobject.h(26) : warning C4820: '<unnamed-tag>' : '4' bytes padding added after data member 'ob_iva
l'
c:python27includestringobject.h(49) : warning C4820: '<unnamed-tag>' : '7' bytes padding added after data member 'ob_
sval'
c:python27includebytearrayobject.h(26) : warning C4820: '<unnamed-tag>' : '4' bytes padding added after data member '
ob_exports'
c:python27includesetobject.h(26) : warning C4820: '<unnamed-tag>' : '4' bytes padding added after data member 'hash'
c:python27includesetobject.h(56) : warning C4820: '_setobject' : '4' bytes padding added after data member 'hash'
c:python27includemethodobject.h(42) : warning C4820: 'PyMethodDef' : '4' bytes padding added after data member 'ml_fl
ags'
c:python27includefileobject.h(26) : warning C4820: '<unnamed-tag>' : '4' bytes padding added after data member 'f_ski
pnextlf'
c:python27includefileobject.h(33) : warning C4820: '<unnamed-tag>' : '4' bytes padding added after data member 'writa
ble'
c:python27includegenobject.h(23) : warning C4820: '<unnamed-tag>' : '4' bytes padding added after data member 'gi_run
ning'
c:python27includedescrobject.h(28) : warning C4820: 'wrapperbase' : '4' bytes padding added after data member 'offset
'
c:python27includedescrobject.h(32) : warning C4820: 'wrapperbase' : '4' bytes padding added after data member 'flags'
c:python27includeweakrefobject.h(37) : warning C4820: '_PyWeakReference' : '4' bytes padding added after data member
'hash'
c:python27includepystate.h(70) : warning C4820: '_ts' : '4' bytes padding added after data member 'use_tracing'
c:python27includeimport.h(61) : warning C4820: '_frozen' : '4' bytes padding added after data member 'size'
c:python27includecode.h(26) : warning C4820: '<unnamed-tag>' : '4' bytes padding added after data member 'co_firstlin
eno'
src/python/core.c(2) : fatal error C1083: Cannot open include file: 'alloca.h': No such file or directory
error: command 'C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\amd64\cl.exe' failed with exit status 2

问题

如何编译此包?(请帮忙,现在是第4天了!)

我认为这是x86/x64兼容错误。

我使用过python 3.9.0,这些步骤对我很有效(使用python上的py-evm项目https://github.com/ethereum/py-evm):

  1. 下载pyethash 0.1.27:https://pypi.org/project/pyethash/0.1.27/#files并解压缩项目中lib文件夹中的归档文件。我在/virtualenv/Lib/site-packages/pyethash-0.1.27文件夹中打开了包装

  2. 从git下载ethash:https://github.com/ethereum/ethash

  3. 将ethash文件夹中的所有文件复制到/virtualenv/Lib/site-packages/pyethash-0.1.27

  4. 更改/pyethash-0.1.27/src/libethash/mmap.h将此代码添加到文件末尾:

#pragma comment(lib, "Shell32.lib")
  1. 更改pyethash-0.1.27srcpythoncore.c文件-将#include <alloca.h>替换为以下代码:
#if defined(_WIN32) || defined(WIN32)
#include <malloc.h>
#else
#include <alloca.h>
#endif
  1. 压缩/virtualenv/Lib/site-packages/pyethash-0.1.27文件夹并删除该文件夹(压缩存档将保留)。

  2. 为新创建的zip /virtualenv/Lib/site-packages/pyethash-0.1.27.zip 运行pip安装

  3. 它可能与版本冲突:ethash有0.1.23版本,而不是0.1.27,我刚刚将请求更改为0.1.23,这对我很有效。你可以尝试不复制所有文件(从ethhash到pyethhash),只复制一个接一个破坏编译的文件。

alloca分配自动内存,正如Jens Gustedt所指出的,这是不标准化的。

MSVCRT在<malloc.h>标头中声明它。它在Windows上的实现与UNIX系统上的常见行为一致,因此它应该按预期工作。不过,代码的其他部分可能与UNIX耦合得更紧密,可能需要重写。

另外两个头是标准的C头,应该在编译器自动搜索的INCLUDE目录中。

如果您的Python 2.7解释器是用最新版本编译的(就像您的版本一样,即[MSC v.1500 64 bit (AMD64)]),您可以安装新版本的MSVC(Visual Studio社区版是免费的)并修改Python 2.7的msvxx99.py,直接在<python>/lib/distutils/msvc9compiler.py文件中的find_vcvarsall函数的开头插入以下内容(其中<python>通常是C:Python27或类似内容)。这应该可以解决任何Python 2模块的问题。

注意,如果安装位于C:以外的驱动器上,则需要更改路径。该代码故意简单,不会试图通过使用注册表或vswhere等来变得智能,如果找不到更新版本的MSVC,则会回到原始实现

def find_vcvarsall(version):
    # --[insert starts here]--
    import glob
    paths = glob.glob(r"C:Program Files (x86)Microsoft Visual Studio20**vcAuxiliaryBuildvcvarsall.bat")
    if paths:
        print("{} possible locations for vcvarsall.bat found:".format(len(paths)))
        for i, path in enumerate(paths):
            print("t[{}] {}".format(i, path))
        if len(paths) > 1:
            print("picking one at random:")
            paths.shuffle()
        path = paths[0]
        print("selecting:  {}".format(path))
        return path
    # otherwise, do what it would have done normally