CassiePray
CassiePray

Reputation: 43

C Mulitple definition/first defined here error

I'm trying to write a bookstore program, and I'm getting an error saying "multiple definition" in my source code file at my function implementation.

Here is my Book.c file:

#include "Book.h"

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

Here is my Book.h file:

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

And here is my main.c file:

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

int main()
{
    return 0;
}

The error occurs at the scanBook function in Book.c but I don't know why, since I included the header file as well as #pragma once, and in the header file I declared the function. It says multiple definition of 'scanBook' and obj\Debug\Book.o .... first defined here.

Any help or clarification would be greatly appreciated!

Upvotes: 3

Views: 12316

Answers (2)

Sohrab Vafa
Sohrab Vafa

Reputation: 1

the solution is to delete this line:

#include "Book.c"

In C and C++ usually you just include the header files (.h and .hpp), because you give the .c and .cpp files directly to the compiler, so it is ambiguous if you also include them.

Upvotes: 0

Jeremy
Jeremy

Reputation: 4381

Don’t do:

#include “Book.c"

in your main.c file.

Upvotes: 7

Related Questions