Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 776
CassiePray Avatar asked Mar 03 '26 06:03

CassiePray


1 Answers

Don’t do:

#include “Book.c"

in your main.c file.

like image 52
i40west Avatar answered Mar 05 '26 19:03

i40west



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!