【问题标题】:Weird compiler error in MSVCMSVC 中出现奇怪的编译器错误
【发布时间】:2013-05-27 19:38:59
【问题描述】:

我正在试用 WinFUSE 库并收到一条奇怪的编译器错误消息。

#include <windows.h>
#include <fuse.h>

void* fuse_init(struct fuse_conn_info* conn) {
    printf("%s(%p)\n", __FUNCTION__, conn);
    if (!conn) return NULL;
    conn->async_read = TRUE;
    conn->max_write = 128;
    conn->max_readahead = 128;
    return conn;
}

int main(int argc, char** argv) {
    struct fuse_operations ops = {0};

    // Fill the operations structure.
    ops.init = fuse_init;         // REFLINE 1

    void* user_data = NULL;       // REFLINE 2  (line 26, error line)
    return fuse_main(argc, argv, &ops, NULL);
}

输出是:

C:\Users\niklas\Desktop\test>cl main.c fuse.c /I. /nologo /link dokan.lib
main.c
main.c(26) : error C2143: syntax error : missing ';' before 'type'
fuse.c
Generating Code...

当我评论 REFLINE 1 REFLINE 2 时,编译工作正常。

1:工作

int main(int argc, char** argv) {
    struct fuse_operations ops = {0};

    // Fill the operations structure.
    // ops.init = fuse_init;         // REFLINE 1

    void* user_data = NULL;       // REFLINE 2
    return fuse_main(argc, argv, &ops, NULL);
}

2:工作

int main(int argc, char** argv) {
    struct fuse_operations ops = {0};

    // Fill the operations structure.
    ops.init = fuse_init;         // REFLINE 1

    // void* user_data = NULL;       // REFLINE 2
    return fuse_main(argc, argv, &ops, NULL);
}

问题

这是一个错误还是我做错了?我正在编译

Microsoft (R) C/C++ 优化编译器版本 17.00.60315.1 for x86

【问题讨论】:

    标签: c syntax visual-studio-2010


    【解决方案1】:

    Microsoft 编译器仅支持 C89,因此不允许混合声明和代码(这是在 C99 中添加的)。所有变量声明必须放在每个块的开头,在其他任何东西之前。这也可以:

    int main(int argc, char** argv) {
        struct fuse_operations ops = {0};
    
        /* Fill the operations structure. */
        ops.init = fuse_init;
    
        {
            void* user_data = NULL;
        }
    
        return fuse_main(argc, argv, &ops, NULL);
    }
    

    【讨论】: