我是 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: In function
isWordGuessed': hangman.c:(.text+0x0): multiple definition of
isWordGuessed' main.o:main.c:(.text+0x0): first defined here
最佳答案
#include "hangman.h"
#include "hangman.c"
不要在您的程序中包含.c
源文件。 include 指令将简单地将 hangman.c
复制到您的 main.c
中。
关于C 链接库错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33944882/