【问题标题】:C error in linking library链接库中的 C 错误
【发布时间】:2015-11-26 18:14:52
【问题描述】:

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

【问题讨论】:

    标签: c exception linker libs


    【解决方案1】:
    #include "hangman.h"
    #include "hangman.c"
    

    不要在程序中包含.c 源文件。 include 指令将简单地将hangman.c 复制到您的main.c 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-02
      • 2017-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多