【问题标题】:Multiple VC compile errors多个VC编译错误
【发布时间】:2012-11-02 15:28:04
【问题描述】:

我正在使用 VC++ 在 C 中编写一个 128 位整数“类”(将标头包装在 extern "C" 块中),但是,在例程构建过程中检查错误时,我在添加此函数后遇到了很多问题:

/* Negate (2s compliment NOT) a 128-bit integer */
INT128_INLINE void int128_neg(int128_t *ret, int128_t arg)
{
    /* 2s compliment negation is equivalent to ((~value) + 1) */

    int128_t one;
    int128_from_i8(&one, 1);

    int128_t inv;
    int128_inv(&inv, arg);

    int128_add(ret, one, inv);
}

基本上,如果不够清楚,它会找到给定int128_t 的负数 (-x)。这是int128_t 结构:

/* Work at the byte level to work around endianess crap */
/* (speaking of which, this is big endian, at least is should be) */
#pragma pack(1)
struct _int128_t
{
    /* SIGN BIT: 0xxxxxxx = positive; 1xxxxxxx = negative */
    uint8_t byte01; /* MSB */
    uint8_t byte02;
    uint8_t byte03;
    uint8_t byte04;
    uint8_t byte05;
    uint8_t byte06;
    uint8_t byte07;
    uint8_t byte08;
    uint8_t byte09;
    uint8_t byte10;
    uint8_t byte11;
    uint8_t byte12;
    uint8_t byte13;
    uint8_t byte14;
    uint8_t byte15;
    uint8_t byte16; /* LSB */
};

/* A 128-bit integer structure and functions written in pure C99 code :) */
typedef struct _int128_t int128_t;

以及函数原型:

/* Invert (~) a 128-bit integer */
INT128_INLINE void int128_inv(int128_t *ret, int128_t arg) { ... }

/* Add two 128-bit integers together */
INT128_INLINE void int128_add(int128_t *ret, int128_t lhs, int128_t rhs) { ... }

INT128_INLINE(显然使用了将其设置为__inline的那个):

/* MC++ requires `__inline`*/
#ifdef _MSC_VER
#  define INT128_INLINE __inline
#else
   /* use standard inline */
#  define INT128_INLINE inline
#endif

现在,我收到的功能错误是:

Error   24  error C2275: 'int128_t' : illegal use of this type as an expression d:\libplist\libplist\include\int128.h   313 1   libplist
Error   25  error C2146: syntax error : missing ';' before identifier 'inv' d:\libplist\libplist\include\int128.h   313 1   libplist
Error   26  error C2065: 'inv' : undeclared identifier  d:\libplist\libplist\include\int128.h   313 1   libplist
Error   28  error C2065: 'inv' : undeclared identifier  d:\libplist\libplist\include\int128.h   314 1   libplist
Error   29  error C2065: 'inv' : undeclared identifier  d:\libplist\libplist\include\int128.h   316 1   libplist
Error   30  error C2440: 'function' : cannot convert from 'int' to 'int128_t'   d:\libplist\libplist\include\int128.h   316 1   libplist
Error   32  error C2371: 'int128_inv' : redefinition; different basic types d:\libplist\libplist\include\int128.h   324 1   libplist

在你问之前,第 313 行是int128_t inv;。此外,libplist 是我正在开发的 Apple PList 库,它使用 libxml2 和 iTunes 数据库 (C:\Users\{YOU}\My Music\iTunes\iTunes Library.xml)

我尝试重写该函数以确保它不是一些奇怪的 Unicode 废话(就像人们使用 R2L 字符进行巨魔一样),但它没有帮助。我试着做int128_t inv = *ret;,但这只是给出:

Error   27  error C2440: '=' : cannot convert from 'int128_t' to 'int'  d:\libplist\libplist\include\int128.h   313 1   libplist

【问题讨论】:

  • 如果您使用 C 语言编写,您需要知道 Visual C(遵循 C89 规范)要求所有变量声明在函数顶部组合在一起。使用变量时不能声明变量,就像在 C++ 中一样。

标签: c visual-c++ compiler-errors int128


【解决方案1】:
int128_t one;
int128_from_i8(&one, 1);

int128_t inv;
int128_inv(&inv, arg);

这不是有效的 C89 代码,因为 C89 要求所有变量都在函数的 top 处声明,而 Visual C 仅支持 C89。声明必须出现在函数中的之前语句。因此,如果您真正使用的是 C 编译器(而不是 C++ 编译器),这将导致错误。你应该做的是:

int128_t one;
int128_t inv;

int128_from_i8(&one, 1);
int128_inv(&inv, arg);

另外,这条评论是不正确的:

/* A 128-bit integer structure and functions written in pure C99 code :) */

如果您使用的是 Visual C,它是 C89不是 C99,出于上述原因。微软表示,他们永远不会支持任何晚于 C89 的 C 标准,因为他们认为这样做不值得,每个人都应该改用 C++ 或 C#。我不喜欢它,但我对此无能为力;所以,随便。

【讨论】:

    猜你喜欢
    • 2010-11-08
    • 2017-12-21
    • 2010-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-09
    • 1970-01-01
    相关资源
    最近更新 更多