C多个定义/首先在此定义错误

时间:2014-10-24 19:21:39

标签: c multiple-definition-error modularization

我正在尝试编写一个书店程序,我的函数实现中的源代码文件中出现了“多重定义”的错误。

这是我的Book.c文件:

#include "Book.h"

void scanBook(book_t* bk) //error here
{
    //implementation here
}

这是我的Book.h文件:

#pragma once
#include <stdio.h>

typedef char* str;    
typedef enum Genre {Education, Business, Novel} genre_t;

typedef struct Book{
    str ISBN;
    str title;
    str author;
    float price;
    int quantity;
    genre_t genre;
} book_t;

void scanBook(book_t* bk);

这是我的main.c文件:

#include "Book.h"
#include "Book.c"

int main()
{
    return 0;
}

错误发生在Book.c中的scanBook函数但我不知道为什么,因为我包括头文件以及#pragma一次,并且在头文件中我声明了函数。它说'scanBook'和obj \ Debug \ Book.o ....的多个定义首先在这里定义。

非常感谢任何帮助或澄清!

2 个答案:

答案 0 :(得分:7)

不要这样做:

#include “Book.c"

在你的main.c文件中。

答案 1 :(得分:0)

解决方案是删除此行:

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => TaskData(),
      child: MaterialApp(
        home: TasksScreen(),
      ),
    );
  }
}

在C和C ++中,通常只包含头文件(.h和.hpp),因为将.c和.cpp文件直接提供给编译器,因此如果还包含它们,则模棱两可。