如何在 C 中编写适当的包含和模块化我的应用程序?
How do I write a proper include and modulize my app in C?
我再次请求您的帮助 :) 在#includes 中找不到错误,检查了几次,与 geekstogeeks 示例和类似问题在这里进行了比较。所以我有:
/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
用
编译后
gcc -rdynamic -std=c11 `pkg-config --cflags gtk+-3.0` cardmethods.c main.c -o main `pkg-config --libs gtk+-3.0` -lX11
objects.obj
#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
cardmethods.h
#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
cardmethods.c
#include "cardmethods.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
void addCard(card *pile, card *cardAdded){
...
}
void printAll(card *pile){
...
}
...
main.c
#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;
}
您已经在头文件中定义了变量 RankNames
和 SuitNames
。因此,它们在 main.c 和 cardmethods.c 中都有定义。然后当链接这些文件时,链接器找到多个定义。
更改头文件以包含这些变量的外部声明(和 Deck
):
extern card *Deck;
extern char *RankNames[];
extern char *SuitNames[];
并将定义放在一个源文件中,可能是cardmethods.c:
card *Deck;
char *RankNames[] = {" 6 ", " 7 ", " 8 ", ...};
char *SuitNames[] = {"Hearts", "Spades", "Diamonds", "Clubs"};
我再次请求您的帮助 :) 在#includes 中找不到错误,检查了几次,与 geekstogeeks 示例和类似问题在这里进行了比较。所以我有:
/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
用
编译后
gcc -rdynamic -std=c11 `pkg-config --cflags gtk+-3.0` cardmethods.c main.c -o main `pkg-config --libs gtk+-3.0` -lX11
objects.obj
#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
cardmethods.h
#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
cardmethods.c
#include "cardmethods.h"
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
void addCard(card *pile, card *cardAdded){
...
}
void printAll(card *pile){
...
}
...
main.c
#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;
}
您已经在头文件中定义了变量 RankNames
和 SuitNames
。因此,它们在 main.c 和 cardmethods.c 中都有定义。然后当链接这些文件时,链接器找到多个定义。
更改头文件以包含这些变量的外部声明(和 Deck
):
extern card *Deck;
extern char *RankNames[];
extern char *SuitNames[];
并将定义放在一个源文件中,可能是cardmethods.c:
card *Deck;
char *RankNames[] = {" 6 ", " 7 ", " 8 ", ...};
char *SuitNames[] = {"Hearts", "Spades", "Diamonds", "Clubs"};