在libidn的stringprep.h里面,有这样的声明:
view plaincopy to clipboardprint?
extern IDN_DLL_VAR const Stringprep_profiles stringprep_profiles[];
extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_A_1[];
extern IDN_DLL_VAR const Stringprep_profiles stringprep_profiles[];
extern IDN_DLL_VAR const Stringprep_table_element stringprep_rfc3454_A_1[];
声明的实现在c文件里面,例如profiles.c
view plaincopy to clipboardprint?
Stringprep_profiles stringprep_profiles[] = {
{"Nameprep", stringprep_nameprep},
{"KRBprep", stringprep_kerberos5}, /* Deprecate? */
{"Nodeprep", stringprep_xmpp_nodeprep},
{"Resourceprep", stringprep_xmpp_resourceprep},
{"plain", stringprep_plain}, /* sasl-anon-00. */
{"trace", stringprep_trace}, /* sasl-anon-01,02,03. */
{"SASLprep", stringprep_saslprep},
{"ISCSIprep", stringprep_iscsi}, /* Obsolete. */
{"iSCSI", stringprep_iscsi}, /* IANA. */
{NULL, NULL}
};
Stringprep_profiles stringprep_profiles[] = {
{"Nameprep", stringprep_nameprep},
{"KRBprep", stringprep_kerberos5}, /* Deprecate? */
{"Nodeprep", stringprep_xmpp_nodeprep},
{"Resourceprep", stringprep_xmpp_resourceprep},
{"plain", stringprep_plain}, /* sasl-anon-00. */
{"trace", stringprep_trace}, /* sasl-anon-01,02,03. */
{"SASLprep", stringprep_saslprep},
{"ISCSIprep", stringprep_iscsi}, /* Obsolete. */
{"iSCSI", stringprep_iscsi}, /* IANA. */
{NULL, NULL}
};
libidn是可以在vs里面编译通过的,我有一个c++项目要用libidn,但在编译时会报错,
error C2133: 'stringprep_profiles' : unknown size
在MSDN里面,对error C2133的说明为:http://msdn.microsoft.com/en-us/library/c13wk277(VS.71).aspx
An unsized array is declared as a member of a class, structure, union, or enumeration. The /Za (ANSI C) option does not allow unsized member arrays.
The following sample generates C2133:
// C2133.cpp
// compile with: /Za
struct X
{
int a[0]; // C2133, unsized array
};
int main()
{
}
如果你将stringprep.h的数组声明改为:
extern IDN_DLL_VAR const Stringprep_profiles* stringprep_profiles;
extern IDN_DLL_VAR const Stringprep_table_element* stringprep_rfc3454_A_1;
可以编过,但运行时是错误的,stringprep_profiles指向的0x00000003。
这是为什么呢?不至于要将所有的数组都指定大小吧。
先看一个简化的例子,下面的例子是可以编过的,并且运行正常
view plaincopy to clipboardprint?
//f:\My Documents\Visual Studio 2008\Projects\TestProject\ArrayStruct\ArrayDefine.h
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct{
int i;
int j;
} A;
//const A test[];
A test[];
#ifdef __cplusplus
}
#endif
//f:\My Documents\Visual Studio 2008\Projects\TestProject\ArrayStruct\ArrayDefine.h
#pragma once
#ifdef __cplusplus
extern "C"
{
#endif
typedef struct{
int i;
int j;
} A;
//const A test[];
A test[];
#ifdef __cplusplus
}
#endif
view plaincopy to clipboardprint?
//f:\My Documents\Visual Studio 2008\Projects\TestProject\ArrayStruct\ArrayDef.c
#include "ArrayDefine.h"
//const A test[]=
A test[]=
{
{
1,2
},
{
3,4
}
};
//f:\My Documents\Visual Studio 2008\Projects\TestProject\ArrayStruct\ArrayDef.c
#include "ArrayDefine.h"
//const A test[]=
A test[]=
{
{
1,2
},
{
3,4
}
};
view plaincopy to clipboardprint?
//f:\My Documents\Visual Studio 2008\Projects\TestProject\ArrayStruct\ArrayStruct.cpp
#include "stdafx.h"
#include "ArrayDefine.h"
int _tmain(int argc, _TCHAR* argv[])
{
printf("%d,%d\n",test[0].i,test[0].j);
return 0;
}
//f:\My Documents\Visual Studio 2008\Projects\TestProject\ArrayStruct\ArrayStruct.cpp
#include "stdafx.h"
#include "ArrayDefine.h"
