Reputation: 39
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
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
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
you actually have multiple mains defined,
you are linking to a library that has main defined
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
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