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# - argument null异常和invalid异常怎么写?

exception - 在取消引用之前检查 null 或仅通过异常捕获 null 引用

linker - 我怎么知道ld使用的库的路径?

iphone - 包含框架时链接器错误?

c++ - c/c++中perl的hash变量替换

c - 有没有办法将 C 预处理器定义作为宏传递给外部工具?

c# - 将 C++ .dll 函数用于 C# 时出现 BadImageFormatException 错误?

c++ - 从可执行文件中提取静态链接库

c# - IndexOutOfRangeException - 无法使用 PInvoke 查看调用堆栈

java - 你怎么知道机器的堆栈在内存中是向上还是向下增长? (Java)