Radovan Kočík
Radovan Kočík

Reputation: 71

C error in linking library

I am begginer in C and I have a problem with linking .c and .h files into main.c

I tried to link it and it looks like this in 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;
    }

and like this in .c file

#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;
}

and like this in .h file

#define WORDLIST_FILENAME "words.txt"
int isWordGuessed(const char secret[], const char lettersGuessed[]);

Program just throws an exception. It tells me:

hangman.o: In function isWordGuessed': hangman.c:(.text+0x0): multiple definition ofisWordGuessed' main.o:main.c:(.text+0x0): first defined here

Upvotes: 0

Views: 102

Answers (1)

ouah
ouah

Reputation: 145829

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

Do not include .c source files in your program. The include directive will simply copy hangman.c in your main.c.

Upvotes: 2

Related Questions