【问题标题】:Repeating syntax errors at compile time在编译时重复语法错误
【发布时间】:2011-03-15 19:46:53
【问题描述】:

在我的项目中,我需要确定 CPU 支持的 SIMD 指令集。问题是,当我尝试进行测试编译时,会出现一系列重复多次的错误,就像编译器多次解析代码一样。确定支持的 SIMD 指令的原因是因为我正在尝试调整 John the Ripper 的 DES 位片实现,以便在 Windows 和 Linux 的 GPGPU(特别是 CUDA)上使用。

所以,我的错误出现在第 37 行

// File Name: Arch.h
// Purpose: Determine CPU architecture (x86 or x86-64) and to determine instruction set supported by
//          the CPU (MMX, SSE2 or neither)
// 
// Authors: Daniel Beard and Michael Campbell

//If CPU is x86-64 then use x86-64 include
#if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64)
#include "x86-64.h"
#endif

//Determine if CPU architecture is 32-bit, then determine which compiler is being used, finally determine if GCC (GNUC) or MS Visual C++ compiler is being used
#if defined(i386) || defined(__i386__) || defined(__i486__) || defined(__i586__) || defined(__i686__) || defined(_M_IX86)
    #if defined(__GNUC__) 
    #define cpuid(func,ax,bx,cx,dx)\
        __asm__ __volatile__ ("cpuid":\
        "=a" (ax), "=b" (bx), "=c" (cx), "=d" (dx) : "a" (func));
    int a,b,c,d;
    cpuid(0x1,a,b,c,d);
    if ((d & 0x17)== 1)
    {
        #include "x86-mmx.h"
    }
    else if (d & 0x1A) == 1)
    {
        #include "x86-sse.h"
    }
    else if((d & 0x17) != 1 || (d & 0x1A) != 1)
    {
        #include "x86-any.h"
    }
    #endif

    #if defined(_MSC_VER)
        #include<intrin.h>
        int CPUInfo[4] = {0};
        __cpuid( CPUInfo, 1 );
        if( (CPUInfo[3] & 0x1A) == 1 )
        {
            #include "x86-sse.h"
        }
        else if( (CPUInfo[3] & 0x17) == 1 )
        {
            #include "x86-mmx.h"
        }
        else if( (CPUInfo[3] & 0x17) != 1 || (CPUInfo[3] & 0x1A) != 1 )
        {
            #include "x86-any.h"
        }
    #endif
#endif

这是我得到的错误(其中有 86 个,但它们一直重复相同系列的错误/行号):

Error   1   error C2059: syntax error : ','                    line 37  
Error   2   error C2143: syntax error : missing ')' before 'constant'      line 37  
Error   3   error C2143: syntax error : missing '{' before 'constant'      line 37  
Error   4   error C2059: syntax error : '<Unknown>'                line 37  
Error   5   error C2059: syntax error : ')'                    line 37  
Error   6   error C2059: syntax error : 'if'                   line 38  
Error   7   error C2059: syntax error : 'else'                 line 42  
Error   8   error C2059: syntax error : 'else'                 line 46  
Error   9   error C2374: 'CPUInfo' : redefinition; multiple initialization     line 36

【问题讨论】:

  • 你不能像往常一样只#include这个文件,它需要包含在一个函数中才能正确编译。当然不知道该函数应该是什么样子,如果此标头找到示例用法。
  • 编译器通常会在第一个错误后尝试继续,因为他们可能会在代码中发现多个问题。问题是 C++ 真的很难解析,而且很多时候,第一个错误足以使编译器混淆,从而误解其余的代码,引发许多不同的错误。尝试修复第一个,看看其余的是否消失(或者至少你得到一些更好的诊断)

标签: c++ visual-studio-2008 visual-c++ compiler-errors cpuid


【解决方案1】:

您的第一个错误在第 36 行 - 声称 CPUInfo 已经存在 - 先修复它,其余的可能会消失。

在 Visual Studio 选项中,转到“项目和解决方案”、“常规”并取消选中“始终显示错误列表...”

这将为您留下输出窗口 - 您可以使用 F8 导航到您通常应该关心的第一个警告/错误。

【讨论】:

  • 嗯,消除了多重初始化错误,但是前八个仍然存在。
  • 包含arch.h的文件有14个; x86.S、x86-sse.S、x86-sse.h、x86-mmx.S、x86-mmx.h、x86-any.h、x86-64.S、x86-64.h、内存.h、 DES_std.h、DES_bs.h、DES_bs.c 和 common.h 对于 Hans:此 arch.h 是自定义 arch.h。它应该由 gcc 或 make 生成(我从来不知道是哪个)。其中一些文件我对它们的作用有所了解,例如我认为决定架构大小的 arch 文件。我不确定为什么要包括内存和通用。 DES_std.h 包含在 DES_bs.c 中
猜你喜欢
  • 2018-04-10
  • 2015-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多