C++ Visual C+中的结构定义错误+;
我在使用gcc进行结构定义时遇到了一个问题 现在我想在VisualStudio中编译同一个程序,但有些约定不匹配。我对代码做了一些修改,但我不知道哪里出了问题 下面的代码来自brg_types.hC++ Visual C+中的结构定义错误+;,c++,c,visual-studio-2010,C++,C,Visual Studio 2010,我在使用gcc进行结构定义时遇到了一个问题 现在我想在VisualStudio中编译同一个程序,但有些约定不匹配。我对代码做了一些修改,但我不知道哪里出了问题 下面的代码来自brg_types.h #ifndef _BRG_TYPES_H #define _BRG_TYPES_H #if defined(__cplusplus) extern "C" { #endif #include <limits.h> #if defined( _MSC_VER ) &&
#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
上面的代码是用gcc编译器编译的
在VisualStudio中,brg_类型的使用是按原样完成的
和表。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 */
visual studio向我抛出的错误为
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*/
};
您的问题是
对齐的
宏。按照目前的定义,它需要在VS中声明之前,但在GCC中声明之后。您希望实现的语法是:
const uint32_t __declspec(align(64)) T[8*256] = // MSVC
const uint32_t T[8*256] __attribute__ ((aligned(64))) = // GCC
我认为解决方案是将变量声明传递给ALIGNED\uuu
,这样用法如下所示:
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
警告:此代码未被任何编译器看到。假设它包含错误
我不知道如何处理对齐类型的定义,因为我看不到它的使用示例。请从“输出”选项卡而不是“错误”选项卡复制错误,以便获得行号。我已经提到过代码行,在代码行中,我用“near:”获得错误,任何其他选项都可以尝试删除
={}
。但宏观扩张可能存在错误。只是对文件进行预处理,看看宏是如何展开的。为什么__declspec(align(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