c: cygwin- 多重定义

标签 c cygwin definitions

我有点卡住了。我几乎完成了这段代码,但在尝试使其与 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 个文件- 客户端.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

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

最佳答案

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

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

关于c: cygwin- 多重定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12494928/

相关文章:

javascript - 破解 "undefined"对象的 Javascript 定义

c - C 中简单 while 循环提前终止的问题

c - 将 Linux 内核升级到最新的 64 位是否会更改 glibc?

cygwin - 两个xwin-xdg-menu进程,CPU消耗较高

git - 如何控制 Handlebars 如何生成换行符?

javascript - "Good"实现 declare() 函数的实践

C union 体和结构体问题

c - 需要编写一个程序,该程序采用汇编语言程序并生成相应的机器语言

c - 如何自动合并C源文件?

c++ - cmath 中 sqrt、sin、cos、pow 等的定义