1

Hi guys trying to use two main() and getting this error multiple definition of main(). I renamed my main functions then why is this error and also first defined here for my print(). header file:

#ifndef TOP_H_
#define TOP_H_

#include <stdio.h>
#include <string.h>
#define onemain main
#define twomain main
inline void print();


#endif /* TOP_H_ */

c file one:

#include "top.h"
void print();
int onemain()
{
    print();
    return 0;
}
void print()
{
printf("hello one");
}

c file two:

#include "top.h"
void print();
int twomain()
{
    print();
    return 0;
}
void print()
{
printf("hello two");
}

Error snapshot

jeevanreddymandali
  • 395
  • 3
  • 8
  • 23
  • 1
    Please don't post screen prints without reason. Better work on the description of your problem, this one is not clear at all. What do you want to achieve? – Jens Gustedt Feb 02 '14 at 09:01
  • Rather than having two mains why not have two plain functions, `int mymain1(int argc, char *argv[])` and `int mymain2(int argc, char *argv[])`. The actual `main` calls whichever one is appropriate. – Brandin Feb 02 '14 at 09:41

5 Answers5

10

Basically any C (or even C++) program is a bunch of functions calling each other.
To begin a program execution, you have to pick one of these functions and call it first.
By convention, this initial function is called main.

When you include several source files in a project, the IDE compiles them all, then calls the linker which looks for one single function called main and generates an executable file that will call it.

If, for any reason, you defined two "main" functions inside all these files, the linker will warn you that it cannot choose on its own which one you intended to be the starting point of your program.

kuroi neko
  • 8,479
  • 1
  • 19
  • 43
3

The macro substitution of onemain and twomain occurs before the compiler proper sees the program, so that doesn't make a difference. The functions are both named main.

C++ allows different functions with the same name, but does not allow two definitions of the exact same function signature. There would be no way to form a function call expression that would reach either overload. Additionally, the functions are the same entity, and one thing cannot have two definitions.

In addition, in C++ main cannot be overloaded, because the program is supposed to start when the unique main function is called, and any given system detects what format of main the particular program uses, out of a variety of allowed formats. (This auto-detection feature also applies to C.)

But you are not asking about C++; in C, without function overloading, there are no redefinitions of the same name, even for different signatures. Each name of extern linkage in C uniquely identifies an entity, so you cannot have two.

It's unclear what you want the resulting program to do. Most likely you need to build two separate programs.

Potatoswatter
  • 134,909
  • 25
  • 265
  • 421
  • ... and C doesn't allow them as well. That is relevant to this question. – glglgl Feb 02 '14 at 08:52
  • @glglgl Adjusted the answer. I didn't notice the language was C… makes the question even more strange. – Potatoswatter Feb 02 '14 at 08:53
  • But I read somewhere I can rename my main() and use them(in C too) and also more than one like above. And also can't I declare a function in header and use it wherever i want to? What I actually want here is i want to use two mains(renaming them or however) and use the same print(or some other) function declared in my header in my c source files. – jeevanreddymandali Feb 02 '14 at 09:08
  • @jeevanreddymandali Macros do not accomplish renaming anything. They are just a text substitution engine, like smart search-and-replace, taking effect before the program gets compiled. It sounds like you want two different programs, made by two different project files in your IDE. These separate programs may share a common print function by using a library. See your IDE/platform documentation on libraries for details, or just link the C file defining `print()` (and not `main`) into both projects. – Potatoswatter Feb 02 '14 at 09:24
2

I don't get what you ask - your error messages are quite clear:

  1. You have 2 definitions of print(), which will collide. Remove one.
  2. You as well have 2 definitions of main() - your #defines will replace your onemain and twomain functions, naming them effectively as main.
glglgl
  • 89,107
  • 13
  • 149
  • 217
0

You overrode the built in print, about main, try imagining a car with two steering wheels ... it wont work ...

Your C program has two have a least one main, so the computer knows where the program starts. If you have 2 files with two main function, then you have two different programs.

oz123
  • 27,559
  • 27
  • 125
  • 187
0

It's not possible for a C program to have more than one main() in it. Also, main() should be declared as an int and return an integer value (usually 0).

samiam
  • 553
  • 3
  • 6
  • This is not true - in C (Not c++) it can be void – Ed Heal Feb 02 '14 at 08:47
  • 1
    @EdHeal While it is essentially right what you say, the main claim of this answer is fine. So +1 from me. – glglgl Feb 02 '14 at 08:49
  • void main() can run but gives a compiler warning with GCC and Clang (I am mainly a UNIX/Linux guy). More discussion on void main(): http://stackoverflow.com/questions/3156423/why-dont-we-use-void-in-main – samiam Feb 02 '14 at 08:52
  • 2
    @EdHeal: *Only* if the implementation explicitly documents it as a legal signature. Otherwise the behavior is undefined. See the [online 2011 standard](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf), 5.1.2.2.1, para 1. – John Bode Feb 02 '14 at 08:52
  • @John Bode ... This may be a UNIX vs. Windows thing. Apparently, Microsoft *does* make compilers where `main()` has to be `void` (see the discussion I linked to in the above comment). – samiam Feb 02 '14 at 08:54
  • @samiam: And that's fine, as long as they document it as such. – John Bode Feb 02 '14 at 08:55