Chaithu
Chaithu

Reputation: 39

C programming multiple definition error

I am using Eclipse for C programming.

While building the program I am getting a multiple definition of 'main' error.

I have multiple source files in the same project, so why I am getting a multiple definition error, how can I avoid it?

Is it correct to have more than one source file in the same project?

Upvotes: 1

Views: 5371

Answers (3)

Martin Dvorak
Martin Dvorak

Reputation: 812

Check how to define and use build configurations in Eclipse to switch main() in multiple definition of main error in eclipse using C

Upvotes: 0

David Nehme
David Nehme

Reputation: 21572

Yes, it's correct to have multiple files in the same project. Almost all non-trivial C projects have multiple files. If you are getting the "multiple definitions of main" at the link time, that means the main function was defined in more than one link objects. That can happen if

  1. you actually have multiple mains defined,

  2. you are linking to a library that has main defined

  3. you have multiple files that #include a file with main.

    #include of a .c file is normally not what you want to do. If you want to use functions defined in other .c files, you generally want to create header files (.h) that have prototypes of the functions.

Upvotes: 1

Alok Save
Alok Save

Reputation: 206518

The error:

multiple definition of main'` error.

Tells that:
You have more than one function named main() defined across your multiple files.
Remove the redundant main() function and keep only one.
Defining multiple definitions for a function breaks the One Definition Rule and hence the error.

Upvotes: 2

Related Questions