链接库中的C错误

时间:2015-11-26 18:14:52

标签: c exception linker libs

我在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 of中isWordGuessed'      main.o:main.c :(。text + 0x0):首先在这里定义

1 个答案:

答案 0 :(得分:2)

#include "hangman.h"
#include "hangman.c"

不要在程序中包含.c源文件。 include指令只会复制hangman.c中的main.c