【发布时间】: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 <unistd.h>丢失 -
@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