username_4567
username_4567

Reputation: 4923

Function already defined error in C

I have header file called helper.h which needed to be included in main1.c and main2.c so I included helper.h file in both files (without inclusion I was getting error) but still I'm getting error that functions in helper.h are already defined. How to get rid of this error using #ifndef?

Upvotes: 0

Views: 345

Answers (2)

Ramesh K
Ramesh K

Reputation: 41

In helper.h file add following lines at start

#ifndef HELPER_H
#define HELPER_H

I hope this should solve your problem.

Upvotes: 1

unwind
unwind

Reputation: 400079

That indicates that you have defined a function in the header, i.e. implemented it. You can't do that, you should only have declarations i.e. prototypes.

Add a new C file helper.c which implements the functions.

Then compile each C file and link them together (main1.o + helper.o and main2.o + helper.o) to form the two programs.

Upvotes: 3

Related Questions