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");
}
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.
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.
I don't get what you ask - your error messages are quite clear:
- You have 2 definitions of
print()
, which will collide. Remove one.
- You as well have 2 definitions of
main()
- your #define
s will replace your onemain
and twomain
functions, naming them effectively as main
.
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.
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).