【问题标题】:Error C2059: syntax error : 'string'错误 C2059:语法错误:“字符串”
【发布时间】:2013-07-19 08:19:45
【问题描述】:

我查看了其他帖子,老实说,我仍然不确定是什么导致了问题。我在 Visual Studio 中编程,并且

我有以下代码:(这是一个 C 主程序)

int main(int arc, char **argv) {
       struct map mac_ip;
       char line[MAX_LINE_LEN];

       char *arp_cache = (char*) calloc(20, sizeof(char));   //yes i know the size is wrong - to be changed
       char *mac_address = (char*) calloc(17, sizeof(char));
       char *ip_address = (char*) calloc(15, sizeof(char));

       arp_cache = exec("arp -a", arp_cache);

它使用以下cpp代码:

#include "arp_piping.h"

extern "C" char *exec(char* cmd, char* arp_cache, FILE* pipe) {
    pipe = _popen(cmd, "r");
    if (!pipe) return "ERROR";
    char buffer[128];
    while(!feof(pipe)) {
        if(fgets(buffer, 128, pipe) != NULL) {
              strcat(arp_cache, buffer);
        }
    }
    _pclose(pipe);
    return arp_cache;
}

与匹配的头文件:

#ifndef ARP_PIPING_H
#define ARP_PIPING_H
#endif

#ifdef __cplusplus
#define EXTERNC extern "C"
#else
#define EXTERNC
#endif

#include <stdio.h>
#include <string.h>

extern "C" char *exec(char* cmd, char* arp_cache, FILE* pipe);

#undef EXTERNC

但我不断收到以下错误:

1>d:\arp_proto\arp_proto\arp_piping.h(14): error C2059: syntax error : 'string'
1>main.c(22): warning C4013: 'exec' undefined; assuming extern returning int
1>main.c(22): warning C4047: '=' : 'char *' differs in levels of indirection from 'int'

请帮我一下,我看过其他关于 c2059 的帖子,但还是一无所获

【问题讨论】:

  • #include &lt;unistd.h&gt; 丢失
  • @GrijeshChauhan 我正在使用 Visual Studios,我们没有那个头文件
  • 如果你为 C++ 定义了一个EXTERNC 宏,你为什么不使用它呢?此外,如果#ifndef ARP_PIPING_H 应该是包含保护,则#endif 需要位于标头的末尾。就像现在一样,您可以防止 ARP_PIPING_H 的双重定义,但这无济于事
  • 您向我们展示了 12 行 arp_piping.h 并且在第 14 行报告了编译错误。定义您自己的“exec”是令人困惑的,因为有类似名称的标准库函数,尽管它不应该造成任何特别的麻烦。您没有向我们展示 main.c#include 语句。
  • @Tony D uhm arp_piping.h 确实有 16 行,空行也算

标签: c++ c visual-studio-2010 error-handling


【解决方案1】:

更改您的 exec 声明以使用您费力定义的 EXTERNC 宏。

EXTERNC char *exec(char* cmd, char* arp_cache, FILE* pipe);

【讨论】:

  • 我不是windows程序员,但没搞懂EXTERNC是什么,这是什么?
  • @GrijeshChauhan EXTERNC 这里解析为extern "C",这使得函数名具有 C 链接,因此可以在 C 程序中使用。这不是 Windows 特有的功能!在这里阅读更多:stackoverflow.com/questions/1041866/…
  • @GrijeshChauhan:EXTERNC 在使用 C++ 编译器编译时扩展为 extern "C",否则扩展为空。因此,C 代码看到的是常规函数声明,而 C++ 看到的是特殊声明,表明该声明是针对具有 C 链接的符号(即,在尝试解析符号时不要破坏名称)。
  • @jxh 实际上这是我不明白的 while C++ sees a special declaration that indicates the declaration is for a symbol with C linkage 吗?这是什么意思?在c ++中EXTERNC char *exec(ch将扩展为extern "C" char *exec(chextern "C"是什么意思?它对 c++ 编译器/代码有何用处
  • @GrijeshChauhan:这就是 C++ 代码调用 C 库函数的方式。它有时也用于为 C 代码提供与 C++ 实现的功能接口。
【解决方案2】:

我在将enum 添加到项目时遇到了这个编译错误。事实证明,enum 定义中的一个值与预处理器 #define 发生名称冲突。

enum 如下所示:

// my_header.h enum Type { kUnknown, kValue1, kValue2 };

然后在其他地方有一个#define,内容如下:

// ancient_header.h #define kUnknown L"Unknown"

然后,在项目其他地方的 .cpp 中,这两个标头都包含在内:

// some_file.cpp #include "ancient_header.h" #include "my_header.h" // other code below...

由于名称kUnknown 已经是#define'd,当编译器在我的enum 中找到kUnknown 符号时,它会产生错误,因为该符号已用于定义字符串。这导致了我看到的神秘的syntax error: 'string'

这非常令人困惑,因为 enum 定义中的所有内容似乎都是正确的,并且可以自行编译。

这是在一个非常大的 C++ 项目中,并且 #define 被传递包含在一个完全独立的编译单元中并由 15 年前的某人编写,这并没有帮助。

显然,从这里开始正确的做法是将糟糕的 #define 重命名为比 kUnknown 不常见的名称,但在那之前,只需将 enum 的值重命名为其他名称即可解决问题,例如: // my_header.h enum Type { kSomeOtherSymbolThatIsntDefined, kValue1, kValue2 };

无论如何,希望这个答案对其他人有帮助,因为这个错误的原因困扰了我一天半。

【讨论】:

    【解决方案3】:

    extern "C" 用于告诉编译器将其设为 C 语法,但您的意思是清除名为 exec 的 extern 函数。你只是融合到不同的地方。所以在 arp_piping.h 中像这样重写你的代码:

    /*extern "C"*/ char *exec(char* cmd, char* arp_cache, FILE* pipe);
    

    然后删除cpp文件中extern "C"的前缀。 如果你想用 C 语法对它们进行编译,只需在调用函数 exec 的 cpp 中设置,这样写:

    extern "C" {
       #include "arp_piping.h"
    }
    

    【讨论】:

    • extern "C" is used to tell the compiler to make it as C grammer 不,不是。这是关于调用约定的,仅此而已。
    • 调用约定是什么意思?你能再解释一下吗?首先感谢您的提示
    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 2017-09-02
    • 1970-01-01
    • 2016-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多