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?

1

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
I have only one main function in each source file, but still am getting multiple definition error
@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.
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.

1 Comment

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
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.