【问题标题】:C2059 syntax error using declspec macro for one function; compiles fine without it对一个函数使用 declspec 宏时出现 C2059 语法错误;没有它编译得很好
【发布时间】:2011-10-30 22:38:10
【问题描述】:

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

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

这是关于 SECURESTRING_API 宏的。它不会抱怨 strlcpy 和 strlcat 中的两种用法,但是当尝试将其用于 'str_from_last' 时,它会产生上述错误。 如果我删除它,它编译得很好,但是函数不会从 DLL 中导出,这使它毫无用处!

Google 搜索未产生(相关)结果;有没有人遇到过这个?我在 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 */

【问题讨论】:

  • BOOL 宏看起来很可疑,可能它定义为不是类型的东西?
  • 遗憾的是,它在处理时是未定义的。删除定义显示该类型不存在。我刚刚用'int'替换了所有'BOOL',同样的错误又回来了。

标签: c visual-studio dll


【解决方案1】:
#if _WIN32

你确定要输入这个吗?我会:

#if defined(WIN32)

编辑:看起来不错,但我认为是这样: 将 SECURESTRING_API 移到返回类型 (char*) 之前:

SECURESTRING_API char*

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

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

【讨论】:

  • 肯定输入了 - 两个 IDE 都没有将代码变灰,并且智能感知将其显示为“1”。这样,将编译后的 DLL 加载到 Dependency Walker 中就会显示两个导出的函数!
  • 完美!很惊讶我在以前的几年里从来没有遇到过这种情况。该链接也完全清除了这一点。干杯
猜你喜欢
  • 2015-07-01
  • 1970-01-01
  • 2021-11-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多