【发布时间】:2015-11-26 18:14:52
【问题描述】:
我是 C 的初学者,在将 .c 和 .h 文件链接到 main.c 时遇到问题
我尝试链接它,它在 main.c 中看起来像这样:
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "hangman.h"
#include "hangman.c"
#include <string.h>
#include <time.h>
#include <ctype.h>
int main(void)
{
char secret[20];
srand(time(NULL));
getWord(secret);
hangman(secret);
return 0;
}
在 .c 文件中像这样
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include "hangman.h"
int isWordGuessed(const char secret[], const char lettersGuessed[]){
int pom = 0;
int i,j;
for(i = 0; i < strlen(secret); i++){
for (j= 0; j < strlen(lettersGuessed); j++)
{
if(secret[i] == lettersGuessed[j])
pom = 1;
}
if(pom == 1){
pom = 0;
}
else{
return 0;
}
}
return 1;
}
在 .h 文件中像这样
#define WORDLIST_FILENAME "words.txt"
int isWordGuessed(const char secret[], const char lettersGuessed[]);
程序只是抛出一个异常。它告诉我:
hangman.o:在函数
isWordGuessed': hangman.c:(.text+0x0): multiple definition ofisWordGuessed' main.o:main.c:(.text+0x0): 首先定义在这里
【问题讨论】: