C代码在linux上编译良好,但在Visual Studio中不起作用

And*_*eaF 0 c linux visual-studio

我有一个简单的c程序(它是一个没有任何GUI的基本程序),在linux上这个代码编译和运行没有任何gcc编译器的问题,现在我必须使用Visual Studio 2013在Windows上编译相同的代码,同时使用visual c和英特尔编译器,我已按照本指南选择了新的控制台应用程序

但得到大量的这些错误消息

error : declaration may not appear after executable statement in block  myfile.c
Run Code Online (Sandbox Code Playgroud)

在变量声明的所有行,(也简单int i;)

我使用的唯一库是这些

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <xmmintrin.h>
#include <math.h>
#include <string.h>
Run Code Online (Sandbox Code Playgroud)

有什么建议吗?

hac*_*cks 5

您的程序似乎有混合声明和代码.VS不支持混合类型声明(或者你可以说它完全不支持C99).
例如,在混合声明和代码中,您可以执行以下操作:

int i;
...
i++;
int j = i + 2;
Run Code Online (Sandbox Code Playgroud)

从声明的位置到封闭块的末尾,每个标识符都是可见的.

您需要将所有声明移动到开头,就像在C89中一样.你应该注意到VS不是C编译器.


rod*_*igo 5

问题是这样的代码:

void foo()
{
    bar(); /* statement */

    int i; /* variable declaration */
}
Run Code Online (Sandbox Code Playgroud)

那不是C90有效,因为声明必须在块的开头,就在开头的括号({)之后.

然而,在C99及以后它是正确的,所以GCC接受它,(C90是15岁).

不幸的是,MS-VC没有,也不会支持C99,所以你的代码不会在那里编译.

您可以手动修复它,将声明移动到块的顶部(1),添加大量的大括号(2),尝试将其编译为C++,更改编译器......这取决于您!

例1:

void foo()
{
    int i;

    bar();
    /* beware! if i was initialized, add the initialization here, not there */
}
Run Code Online (Sandbox Code Playgroud)

示例2(隐藏的括号):

void foo()
{
    bar();

    {int i;
}} /*all the closing braces should be at the end of the function */
Run Code Online (Sandbox Code Playgroud)