C2059使用declspec宏的一个函数的语法错误;没有它就可以编译 [英] C2059 syntax error using declspec macro for one function; compiles fine without it

查看:141
本文介绍了C2059使用declspec宏的一个函数的语法错误;没有它就可以编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个共享库(跨平台),但是在尝试编译Windows版本时遇到错误:

I've making a shared library (cross-platform), but when trying to compile the Windows build I'm encountering the error:


secure_string.h(43):错误C2059:语法错误:'type'

secure_string.h(43) : error C2059: syntax error : 'type'

这与SECURESTRING_API宏有关。它并没有抱怨strlcpy和strlcat中的两种用法,但是当尝试将其用于 str_from_last时会产生上述错误。
如果我删除它,它可以很好地编译,但是该函数不会从DLL中导出,因此变得非常无用!

This is in regards to the SECURESTRING_API macro. It doesn't complain about the two usages in strlcpy and strlcat, but when trying to use it for 'str_from_last' it generates the above error. If I remove it, it compiles fine, but the function then isn't exported from the DLL, making it quite useless!

Google搜索产生了没有(相关)结果;有人遇到过这个吗?我已经在Visual Studio 2008和2010上进行了测试,结果是相同的。源文件包括 string.h ,然后是此文件-没有其他内容。

A Google search has yielded no (relevant) results; has anyone encountered this before? I've tested on both Visual Studio 2008 and 2010, and the result is identical. The source file includes string.h and then this file - nothing else.

头文件:

#ifndef SECURE_STRING_H
#define SECURE_STRING_H


/* Provide MS Builds with import/export functionality
 * BUILD_SECURE_STRING should be added to project preprocessor macros */
#if _WIN32
#   if defined(BUILD_SECURE_STRING)
#       define SECURESTRING_API __declspec(dllexport)
#   else
#       define SECURESTRING_API __declspec(dllimport)
#   endif
#else
#   define SECURESTRING_API
#endif


/* Windows on the whole, and glibc do not have/support strlc[at|py]
 * This will almost certainly need revision for proper cross-platform checks */
#if _WIN32 || __GLIBC__ || !defined(HAVE_STRLCPY)
    size_t SECURESTRING_API strlcat(char* dst, const char* src, size_t size);
    size_t SECURESTRING_API strlcpy(char* dst, const char* src, size_t size);
#else
#   define HAVE_STRLCPY     1
#endif


/* In case the active project has yet to include headers for 'BOOL' */
#ifndef BOOL
#   define BOOL     int
#   define TRUE     1
#   define FALSE    0
#endif


/*
 | Locates 'search' within 'source', and if found, returns either the
 | character itself, or the character after it if 'return_from_after_found'
 | is TRUE.
 | If it is not found, or any parameter is invalid, a NULL pointer is returned.
 */
char* SECURESTRING_API
    str_from_last(char* source,
                  char search,
                  BOOL return_from_after_found);



#endif  /* SECURE_STRING_H */


推荐答案

#if _WIN32

您确定要输入吗?我会:

Are you sure you're entering this? I would:

#if defined(WIN32)

编辑:看起来还可以,但是我想是这样的:
将SECURESTRING_API移动到返回类型(char *)之前:

That looks OK but this I think is it: move the SECURESTRING_API before the return type (char*):

SECURESTRING_API char*

通过此更改您VS2010和 MSDN 确认这是必需的订单:

With this change your code compiles for me under VS2010 and MSDN confirms that is the required order:


decl-specifier-seq除其他外应包含基本类型
(例如,int,float,typedef或类名),存储类(例如,
static) ,extern)或__declspec扩展名。初始化声明者列表
应该包含
声明的指针部分

这篇关于C2059使用declspec宏的一个函数的语法错误;没有它就可以编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆