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

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 のものを追加する前はすべて機能していました:(しかし、元に戻そうとすると、これらのエラーが発生しました。

4

2 に答える 2

3

コンパイル コマンド ラインを注意深く見てください。

gcc client.c client.h clientdata.c clientdata.c -o client.exe
  1. ヘッダー ファイルをコンパイラに渡す必要はありません。
  2. 同じソース コードを 2 回渡したくはありません (すべてのextern関数clientdata.cが 2 回定義されているため、複数定義エラーが発生します)。

必要なのは次のとおりです。

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 に答える