1

我有点卡住了。我几乎完成了这段代码,但是在尝试使其与 Windows 和 linux 兼容之后,我遇到了这个我无法解决的问题。我在这方面没有太多经验。以下是错误-

$ gcc client.c client.h clientdata.c clientdata.c -o client.exe
/tmp/ccHpxeKs.o:clientdata.c:(.text+0x0): multiple definition of `_handleSendingData'
/tmp/cclpyPee.o:clientdata.c:(.text+0x0): first defined here
/tmp/ccHpxeKs.o:clientdata.c:(.text+0xa9): multiple definition of `_handleRecievingData'
/tmp/cclpyPee.o:clientdata.c:(.text+0xa9): first defined here
/tmp/ccHpxeKs.o:clientdata.c:(.text+0xabb): multiple definition of `_replaceNewLineChar'
/tmp/cclpyPee.o:clientdata.c:(.text+0xabb): first defined here
/tmp/ccHpxeKs.o:clientdata.c:(.text+0xb1c): multiple definition of `_getMyTime'

............

我有 4 个文件-client.c-

#include "client.h"
#ifdef _WIN32
#include "clientdata.c"
#endif //win32

客户端.h-

#ifndef CLIENT
#define CLIENT

#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <time.h>

#define MAXSIZE 2048
#define MILLION  1000000.0

#ifdef _WIN32

#include <winsock.h>
#pragma comment(lib, "wsock32.lib")


#else

#include <sys/socket.h>
#include <netdb.h>
#include <dirent.h>
#include <unistd.h>

#endif //win32

typedef enum {false = 0, true = 1, maybe = 2} bool;

struct messageNode
{
    struct timeval  *time1;
    struct timeval  *time2;
    int idNumber;
    char string[MAXSIZE];
    char stringClientArgs[MAXSIZE];
    struct messageNode* next;
    struct messageNode* moreNext;
    char redirectArgs[MAXSIZE];

} *head, *current;


int handleSendingData(struct messageNode *Node);
int handleRecievingData(struct messageNode *Node);
void printNode(struct messageNode *Node);
int setArgs(struct messageNode *Node, int command);
int setNode(struct messageNode* Node, int id);
int getArgs(struct messageNode* Node, char **newArgs);


#endif //CLIENT

客户端数据.c-

#include "client.h"
#include "clientdata.h"

客户端数据.h-

#ifndef CLIENTDATA
#define CLIENTDATA

#ifdef _WIN32
#include <process.h>
#endif //win32


void replaceNewLineChar(int count, ...);
void copyArray(char* str1, char* str2);
int addId(char *string, int id);
int getId(char *string);
int getReceivedArgs(char *string, char **newArgs, int number);
int getCommand(char* string);
int addToString(char *string, char *text);
int execute(char *cmd, char **args);
int getRedirectArgs(char *string, char **newArgs);
void getMyTime(struct timeval  *time);
double timeDifference(struct timeval  *start, struct timeval  *end);

#endif //CLIENTDATA

我对这个领域非常陌生,并且已经查看了其他问题,但我没有在标题中声明和值,它们都是函数原型。所以迷路了!在我添加#ifdef win32 的东西之前一切正常 :( 但是当我试图将它改回来时,它仍然出现了这些错误。

4

2 回答 2

3

仔细查看你的编译命令行:

gcc client.c client.h clientdata.c clientdata.c -o client.exe
  1. 您不需要将头文件传递给编译器。
  2. 您不想两次传递相同的源代码(这会导致多次定义错误,因为其中的所有extern函数clientdata.c都定义了两次)。

你需要的只是:

gcc client.c clientdata.c -o client.exe
于 2012-09-19T12:51:13.277 回答
1

看起来您多次包含相同的标题。我建议你在所有头文件中使用这样的东西:

#ifndef UNIQUE_ID_FOR_THIS_HEADER
// All your header code here
#endif

PS:你永远不应该包含 .c 文件。如果该文件没有代码,请将其设为 .h 文件。

于 2012-09-19T12:45:46.927 回答