C 链接库错误

标签 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: In function isWordGuessed': hangman.c:(.text+0x0): multiple definition ofisWordGuessed' 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/

相关文章:

c - 在这个用 C 编写的 super 简单的程序中,不会让我读取 "char"类型的变量?

c - 快速调用短函数中动态内存请求的最佳方式

c++ - 如何使用 g++ 正确链接静态库?

c - 将重复出现的相邻字符替换为其他字符

c++ - 我应该只对无效输入使用异常处理吗?

java - SimpleXML 解析不适​​用于 @ElementList

java - OptaPlanner:java.lang.IllegalArgumentException

haskell - GHC 链接器错误(堆栈)

c - 如何从C获取ld链接程序脚本中定义的变量的值

c - Regexc - 段错误