[英]How do I write a proper include and modulize my app in C?
I am once again asking for your help :) Cannot find a mistake in #includes, checked several times, have compared with geekstogeeks example and similar question here.我再次寻求您的帮助:) 在#includes 中找不到错误,检查了几次,与 geekstogeeks 示例和类似问题进行了比较。 So I've got:
所以我有:
/tmp/ccWUaJkV.o:/home/felix/Programming/dypak/objects.obj:27: multiple definition of `RankNames'
/tmp/ccA7hhxl.o:/home/felix/Programming/dypak/objects.obj:27: first defined here
/tmp/ccWUaJkV.o:/home/felix/Programming/dypak/objects.obj:28: multiple definition of `SuitNames'
/tmp/ccA7hhxl.o:/home/felix/Programming/dypak/objects.obj:28: first defined here
/tmp/ccWUaJkV.o:(.bss+0x0): multiple definition of `__odr_asan.SuitNames'
/tmp/ccA7hhxl.o:(.bss+0x0): first defined here
/tmp/ccWUaJkV.o:(.bss+0x1): multiple definition of `__odr_asan.RankNames'
/tmp/ccA7hhxl.o:(.bss+0x1): first defined here
collect2: error: ld returned 1 exit status
After compiling with编译后
gcc -rdynamic -std=c11 `pkg-config --cflags gtk+-3.0` cardmethods.c main.c -o main `pkg-config --libs gtk+-3.0` -lX11
#include <stdbool.h>
#ifndef OBJECTS_O_
#define OBJECTS_O_
typedef struct myCard{
bool trump;
...
} card;
typedef struct {
...
} widgetsPtrs;
card *Deck;//pointer to the deck.
/************************************HERE****************************/
char *RankNames[] = {" 6 ", " 7 ", " 8 ", ...};
char *SuitNames[] = {"Hearts", "Spades", "Diamonds", "Clubs"};
#endif
#include <stdbool.h>
#include <gtk/gtk.h>
#include "objects.obj"
#ifndef CARDMETHODS_H_
#define CARDMETHODS_H_
void addCard(card *pile, card *cardAdded);
void printAll(card *pile);
...
#endif
#include "cardmethods.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
void addCard(card *pile, card *cardAdded){
...
}
void printAll(card *pile){
...
}
...
#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
#include "cardmethods.h"
...
int main (int argc,
char **argv)
{
...
return 0;
}
You've defined the variables RankNames
and SuitNames
in your header file.您已经在头文件中定义了变量
RankNames
和SuitNames
。 Because of this, they are defined in both main.c and cardmethods.c.因此,它们在 main.c 和 cardmethods.c 中都有定义。 Then when these files are linked, the linker finds multiple definitions.
然后当这些文件被链接时,链接器会找到多个定义。
Change the header file to have external declarations of these variables (and Deck
):更改头文件以具有这些变量(和
Deck
)的外部声明:
extern card *Deck;
extern char *RankNames[];
extern char *SuitNames[];
And put the definitions in one source file, probably cardmethods.c:并将定义放在一个源文件中,可能是 cardmethods.c:
card *Deck;
char *RankNames[] = {" 6 ", " 7 ", " 8 ", ...};
char *SuitNames[] = {"Hearts", "Spades", "Diamonds", "Clubs"};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.