使用declspec宏为一个函数的C2059语法错误; 没有它就编好了

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

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

这与SECURESTRING_API宏有关。 它没有抱怨strlcpy和strlcat中的两个用法,但是当试图将它用于’str_from_last’时它会产生上述错误。 如果我删除它,它编译得很好,但该函数然后不从DLL导出,使其无用!

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

头文件:

#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 

你确定要进入吗? 我会:

 #if defined(WIN32) 

编辑:看起来不错,但我想是这样的:在返回类型(char *)之前移动SECURESTRING_API:

 SECURESTRING_API char* 

通过此更改,您的代码将在VS2010下为我编译, MSDN确认这是所需的顺序:

除其他外,decl-specifier-seq应包含基类型(例如int,float,typedef或类名),存储类(例如static,extern)或__declspec扩展。 init-declarator-list应包含声明的指针部分等。