2012-09-19 37 views
1

我有點卡住了。我幾乎已經完成了這段代碼,但是在嘗試使它與Windows和Linux兼容之後,我遇到了這個我無法解決的問題。我在這方面沒有太多的經驗。以下是錯誤 -c:cygwin-多重定義

$ 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 

client.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 

clientdata.c-

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

clientdata.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的東西:(但當我試圖改變它回來仍然想出了這些錯誤。

+0

這是一個完整的源代碼?客戶數據僅包含標題? – Spidey

+0

你的'clientdata.c'只是兩行嗎?它有沒有任何定義?如果是的話,你不應該把它作爲'#include「clientdata.c包含在'client.c'中。 – Mahesh

+0

對不起,我剛添加'includes'。對應的.c文件中有所有的函數,從 – RileyVanZeeland

回答

3

看看你編譯命令行:

gcc client.c client.h clientdata.c clientdata.c -o client.exe 
  1. 您不需要將頭文件傳遞給編譯器。
  2. 您不希望兩次傳遞相同的源代碼(導致多重定義錯誤,因爲clientdata.c中的所有extern函數都定義了兩次)。

你需要的僅僅是:

gcc client.c clientdata.c -o client.exe 
+0

嗯,這似乎已經修復了它。我甚至沒有考慮控制檯....我正在尋找所有,雖然我的代碼超過了一個小時:/謝謝! – RileyVanZeeland

+0

這是一個熟悉的問題,當你在錯誤的地方尋找解決方案時,很難找到解決方案,這是一個新的眼睛的優勢(我們都在錯誤的地方尋找解決問題的方法)。 –

1

看起來像你包括多個相同的標題,我建議你使用這樣的事情在你的所有頭文件:

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

PS:小心你不應該包括.c文件如果文件沒有代碼,使其.h文件

+0

我只是這樣做,因爲我coudldn't讓它與Windows編譯,因爲我無法弄清楚如何手動編譯.c文件在命令propmt。我很新的這個窗口部分。此外,我確實有#ifndef的所有頭文件。 – RileyVanZeeland