Pergunta

I'm a little stuck. I have almost finished this code but after trying to make it compatible for windows and linux, I came across this problem I can't solve. I haven't had too much experience in this area. Here are errors-

$ 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'

...........

I have 4 files- 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

I am very new to this area and have looked over other questions but I'm not declaring and values in the headers, they are all function prototypes. So lost! It was all working before I added the #ifdef win32 stuff :( but When I tried to change it back it still came up with these errors.

Foi útil?

Solução

Look carefully at your compilation command line:

gcc client.c client.h clientdata.c clientdata.c -o client.exe
  1. You don't need to pass header files to the compiler.
  2. You don't want to pass the same source code twice (that's causing the multiple definition errors, because all the extern functions in clientdata.c are defined twice).

What you need is just:

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

Outras dicas

Looks like you're including the same header multiple times. I suggest you use something like this in all your header files:

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

PS: you never should include a .c file. If that file has no code, make it a .h file.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top