1

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?

cha0site
  • 10,517
  • 3
  • 33
  • 51
Chaithu
  • 39
  • 1
  • 3

3 Answers3

2

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.

wallyk
  • 56,922
  • 16
  • 83
  • 148
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • That I understood, I have multiple source files in each source file one main function is there, is that ok to have only one main function for multiple source files – Chaithu Jan 30 '12 at 17:45
  • I have only one main function in each source file, but still am getting multiple definition error – Chaithu Jan 30 '12 at 17:55
  • @Chaithu: Yes, You should have only one `main()` function for your project.Can you please post the files you have and their contents in the Question? I believe they must not be too much to post here. – Alok Save Jan 30 '12 at 17:55
1

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.

Community
  • 1
  • 1
David Nehme
  • 21,379
  • 8
  • 78
  • 117
  • 1
    according to my understanding, I should create header file (.h) which contain #include and main function and to call them in each source file... is that right – Chaithu Jan 30 '12 at 17:49
0

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

Community
  • 1
  • 1
Martin Dvorak
  • 790
  • 8
  • 15