可视化C++中的结构定义错误

Error for structure definition in Visual C++

本文关键字:定义 错误 结构 C++ 可视化      更新时间:2023-10-16

我在结构定义中遇到问题,使用 gcc 工作正常 现在我想在Visual Studio中编译相同的程序,但某些约定不匹配。我已经对代码进行了一些更改,但我无法弄清楚我出错的地方。

下面的代码来自brg_types.h

#ifndef _BRG_TYPES_H
#define _BRG_TYPES_H
#if defined(__cplusplus)
extern "C" {
#endif
#include <limits.h>
#if defined( _MSC_VER ) && ( _MSC_VER >= 1300 )
#  include <stddef.h>
#  define ptrint_t intptr_t
#elif defined( __GNUC__ ) && ( __GNUC__ >= 3 )
#  include <stdint.h>
#  define ptrint_t intptr_t
#else
#  define ptrint_t int
#endif
#ifndef BRG_UI8
#  define BRG_UI8
#  if UCHAR_MAX == 255u
typedef unsigned char uint_8t;
#  else
#    error Please define uint_8t as an 8-bit unsigned integer type in brg_types.h
#  endif
#endif
#ifndef BRG_UI16
#  define BRG_UI16
#  if USHRT_MAX == 65535u
typedef unsigned short uint_16t;
#  else
#    error Please define uint_16t as a 16-bit unsigned short type in brg_types.h
#  endif
#endif
#ifndef BRG_UI32
#  define BRG_UI32
#  if UINT_MAX == 4294967295u
#    define li_32(h) 0x##h##u
typedef unsigned int uint_32t;
#  elif ULONG_MAX == 4294967295u
#    define li_32(h) 0x##h##ul
typedef unsigned long uint_32t;
#  elif defined( _CRAY )
#    error This code needs 32-bit data types, which Cray machines do not provide
#  else
#    error Please define uint_32t as a 32-bit unsigned integer type in brg_types.h
#  endif
#endif
#ifndef BRG_UI64
#  if defined( __BORLANDC__ ) && !defined( __MSDOS__ )
#    define BRG_UI64
#    define li_64(h) 0x##h##ui64
typedef unsigned __int64 uint_64t;
#  elif defined( _MSC_VER ) && ( _MSC_VER < 1300 )    /* 1300 == VC++ 7.0 */
#    define BRG_UI64
#    define li_64(h) 0x##h##ui64
typedef unsigned __int64 uint_64t;
#  elif defined( __sun ) && defined( ULONG_MAX ) && ULONG_MAX == 0xfffffffful
#    define BRG_UI64
#    define li_64(h) 0x##h##ull
typedef unsigned long long uint_64t;
#  elif defined( __MVS__ )
#    define BRG_UI64
#    define li_64(h) 0x##h##ull
typedef unsigned int long long uint_64t;
#  elif defined( UINT_MAX ) && UINT_MAX > 4294967295u
#    if UINT_MAX == 18446744073709551615u
#      define BRG_UI64
#      define li_64(h) 0x##h##u
typedef unsigned int uint_64t;
#    endif
#  elif defined( ULONG_MAX ) && ULONG_MAX > 4294967295u
#    if ULONG_MAX == 18446744073709551615ul
#      define BRG_UI64
#      define li_64(h) 0x##h##ul
typedef unsigned long uint_64t;
#    endif
#  elif defined( ULLONG_MAX ) && ULLONG_MAX > 4294967295u
#    if ULLONG_MAX == 18446744073709551615ull
#      define BRG_UI64
#      define li_64(h) 0x##h##ull
typedef unsigned long long uint_64t;
#    endif
#  elif defined( ULONG_LONG_MAX ) && ULONG_LONG_MAX > 4294967295u
#    if ULONG_LONG_MAX == 18446744073709551615ull
#      define BRG_UI64
#      define li_64(h) 0x##h##ull
typedef unsigned long long uint_64t;
#    endif
#  endif
#endif
#if !defined( BRG_UI64 )
#  if defined( NEED_UINT_64T )
#    error Please define uint_64t as an unsigned 64 bit type in brg_types.h
#  endif
#endif
#ifndef RETURN_VALUES
#  define RETURN_VALUES
#  if defined( DLL_EXPORT )
#    if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
#      define VOID_RETURN    __declspec( dllexport ) void __stdcall
#      define INT_RETURN     __declspec( dllexport ) int  __stdcall
#    elif defined( __GNUC__ )
#      define VOID_RETURN    __declspec( __dllexport__ ) void
#      define INT_RETURN     __declspec( __dllexport__ ) int
#    else
#      error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
#    endif
#  elif defined( DLL_IMPORT )
#    if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
#      define VOID_RETURN    __declspec( dllimport ) void __stdcall
#      define INT_RETURN     __declspec( dllimport ) int  __stdcall
#    elif defined( __GNUC__ )
#      define VOID_RETURN    __declspec( __dllimport__ ) void
#      define INT_RETURN     __declspec( __dllimport__ ) int
#    else
#      error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
#    endif
#  elif defined( __WATCOMC__ )
#    define VOID_RETURN  void __cdecl
#    define INT_RETURN   int  __cdecl
#  else
#    define VOID_RETURN  void
#    define INT_RETURN   int
#  endif
#endif

#define ALIGN_OFFSET(x,n)   (((ptrint_t)(x)) & ((n) - 1))
#define ALIGN_FLOOR(x,n)    ((uint_8t*)(x) - ( ((ptrint_t)(x)) & ((n) - 1)))
#define ALIGN_CEIL(x,n)     ((uint_8t*)(x) + (-((ptrint_t)(x)) & ((n) - 1)))

#define UI_TYPE(size)               uint_##size##t
#define UNIT_TYPEDEF(x,size)        typedef UI_TYPE(size) x
#define BUFR_TYPEDEF(x,size,bsize)  typedef UI_TYPE(size) x[bsize / (size >> 3)]
#define UNIT_CAST(x,size)           ((UI_TYPE(size) )(x))  
#define UPTR_CAST(x,size)           ((UI_TYPE(size)*)(x))

#define u8 uint_8t
#define u32 uint_32t
#define u64 uint_64t

#if defined(__cplusplus)
}
#endif
#endif

下面的代码在表中。

#ifndef __tables_h
#define __tables_h
#include "brg_endian.h"
#define NEED_UINT_64T
//#include "brg_types.h"
#if (PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN)
const u32 T[8*256] __attribute__((aligned(64))) {
/*some code*/
};
#endif /* IS_BIG_ENDIAN */
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
const u32 T[8*256]  __attribute__((aligned(64))) = {
/*some code*/
};
#endif /* IS_LITTLE_ENDIAN */
#endif /* __tables_h */

上面的代码是用gcc编译器编译的

在Visual Studio中使用brg_types.h是按原样完成

的和表.h 更改如下

#pragma once
#ifndef __tables_h
#define __tables_h
#include "brg_endian.h"
#include "brg_types.h"
#if defined(_MSC_VER)
#define ALIGNED_(x) __declspec(align(x))
#else
#if defined(__GNUC__)
#define ALIGNED_(x) __attribute__ ((aligned(x)))
#endif
#endif
#define ALIGNED_TYPE_(t,x) typedef t ALIGNED_(x)
#pragma pack(1)
#define int8_t __int8              
#define int16_t __int16             
#define int32_t __int32             
#define int64_t __int64             
#define uint8_t unsigned __int8     
#define uint16_t unsigned __int16    
#define uint32_t unsigned __int32    
#define uint64_t unsigned __int64    
#if (PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN)
const uint32_t T[8*256] ALIGNED_(64) = {
/*some code*/
};
#endif /* IS_BIG_ENDIAN */
#if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
const uint32_t T[8*256] ALIGNED_(64) = {
};
#endif /* IS_LITTLE_ENDIAN */
#endif /* __tables_h */

视觉工作室抛给我的错误是

Error   3   error C1903: unable to recover from previous error(s); stopping compilation 
Error   1   error C2144: syntax error : 'int' should be preceded by ';' 
Error   2   error C2513: 'int' : no variable declared before '='    
near: const uint32_t T[8*256] ALIGNED_(64) = {
/*some code*/
};

您的问题是ALIGNED_宏。 正如目前定义的那样,它需要在 VS 中的声明之前出现,但在GCC 中之后。 要实现的语法是:

const uint32_t __declspec(align(64)) T[8*256] = // MSVC
const uint32_t T[8*256] __attribute__ ((aligned(64))) = // GCC

我认为解决方案是将变量声明传递给ALIGNED_,这样用法如下所示:

const uint32_t ALIGNED_(T[8*256], 64) = ...

定义应如下所示:

#if defined(_MSC_VER)
#    define ALIGNED_(v, x) __declspec(align(x)) v
#elif defined(__GNUC__)
#    define ALIGNED_(v, x) v __attribute__ ((aligned(x)))
#else
#    error "Don't know how to define ALIGNED_"
#endif

警告:任何编译器都未看到此代码。 假设它包含错误。

我不知道如何处理ALIGNED_TYPE的定义,因为我看不到它的使用示例。