【问题标题】:Declaring arrays with no initial size声明没有初始大小的数组
【发布时间】:2009-03-29 22:49:49
【问题描述】:

我正在尝试使用 Visual Studio 2008 在 Windows Vista x64 上为 OpenSSL (pyOpenSSL) 编译 Python 绑定。当我运行 python setup.py build_ext -I C:\OpenSSL\include 时,它会因以下错误而死:

building 'OpenSSL.crypto' extension
C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\amd64\cl.exe /c /nologo /Ox /MD /W3 /GS- /DNDEBUG -I\OpenSSL\include -IC:\Python26\include -IC:\Python26\PC /Tcsrc/crypto/x509name.c /Fobuild\temp.win-amd64-2.6\Release\src/crypto/x509name.obj
x509name.c
src/crypto/x509name.c(16) : error C2133: 'crypto_X509Name_methods' : unknown size
error: command '"C:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\BIN\amd64\cl.exe"' failed with exit status 2

当我查看有问题的源时,我在第 16 行看到以下内容:

static PyMethodDef crypto_X509Name_methods[];

我的 C 很生锈,所以我不记得你能不能做到这一点。由于这是一个 Python 库,我猜它是为了在 gcc 中编译而编写的,但我没有在这台计算机上安装 Cygwin 环境。是否有一些开关可以让我使用 VS2008 编译此代码?

答案:

在后面的代码中,有这样的:

/*
 * ADD_METHOD(name) expands to a correct PyMethodDef declaration
 *   {  'name', (PyCFunction)crypto_X509_name, METH_VARARGS }
 * for convenience
 */
#define ADD_METHOD(name)        \
    { #name, (PyCFunction)crypto_X509Name_##name, METH_VARARGS,  crypto_X509Name_##name##_doc }
static PyMethodDef crypto_X509Name_methods[] =
{
    ADD_METHOD(hash),
    ADD_METHOD(der),
    ADD_METHOD(get_components),
    { NULL, NULL }
};
#undef ADD_METHOD

根据 Neil Butterworth 的建议,我将错误的行从:

static PyMethodDef crypto_X509Name_methods[];

到:

static PyMethodDef crypto_X509Name_methods[4];

和编译的代码。

谢谢大家。

【问题讨论】:

  • 您必须搜索 crypto_X509Name_methods 的定义/初始化位置,并在两个地方使用相同的大小。

标签: c


【解决方案1】:

你可能有这样的情况:

#include <stdio.h>

static int a[];     // declaration

// lots of code

int a[3];           // use

使用 gcc 编译为 C。我不确定它是否应该(它不是有效的 C++),但我没有足够的 C 语言律师来肯定地告诉你。

【讨论】:

    【解决方案2】:

    由于它是静态的,它必须在某个编译单元(C 源文件)中以恒定大小进行初始化。

    【讨论】:

    • 它必须在同一个编译单元中初始化,对吧?
    • 当然可以(但警告,它不是同一个 file [一般])。
    猜你喜欢
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多