A: 

I'm guessing one of your IDEs automatically creates a file with a main function. Check around to see if one has already been created.

Chris Cooper
+3  A: 

You can't possibly have more than one entry points in your application. When the final executable is started, the entry point function (main) is called. And this one can't be ambiguous.

So if you wanted to call them one by one you could chain them like this:

void main1() {} /* Note that these aren't called main. */
void main2() {}
...

int main(int argc, char* argv[]) {
    main1();
    main2();
    return 0;
}

You could even call them using threads (e.g. boost.Thread), so that they run parallel. But you can't have multiple functions namedmainlinked together.

If you instead want them to be separate programs each, you will have to link them separately.

abenthy
+2  A: 

Each program must have exactly one main function. However, main can call any function you want (including itself, though this can be confusing). Thus, you should break the program up into logical parts.

Matthew Flaschen
`main` can call itself in C, but *not* in C++.
Jerry Coffin
Right. The question seems to be about C, despite the C++ tag.
Matthew Flaschen
`C++ tag` to bring attentions :)please see my edit
Mohammed
@Daz: Please don't use a tag irrelevant to the question just for attention. We look at the tags when providing answers.
Georg Fritzsche
@Jerry Coffin: That's not true. Main can call itself in C++. You can write valid programs that tail recurse main, effectively restarting themselves.
DeadMG
@DeadMG: Not so -- §3.6/2: "The function "main" shall not be used (3.2) within a program." §3.2: "An object or non-overloadedfunction is used if its name appears in a potentially-evaluated expression." The name `main` cannot appear in any potentially evaluated expression, which prevents it from being called within the program.
Jerry Coffin
@Jerry Coffin: The standard says that. In reality, you can.
DeadMG
@DeadMG: No -- since the standard says you're not allowed to do it, if you do it, your code is no longer "in C++". C++ is defined by the standard; code that violates the standard isn't C++.
Jerry Coffin
@Jerry Coffin: Main is overloaded, on my compiler there are many valid forms of main, let alone the extra WinMains.
DeadMG
@DeadMG: and your point is?
Jerry Coffin
@DeadMG, plenty of compilers have proprietary extensions and differences from the standard. But those variations aren't part of C++. You can not write a valid C++ program that calls main.
Matthew Flaschen
A: 

You cannot have multiple definitions of main. The "main" function is what, in essence, defines what your program does. If you had more than one copy of main, which one would you expect to be executed?

The solution to your problem is to use libraries; if you want to reuse functionality, then you should create a library, which is basically identical to a program except that while it has functions and data (like a program), it doesn't have a special function called "main", and hence has no "entry point" where execution should begin when it would be double-clicked or otherwise loaded by the OS. Libraries come in two variants: shared/dynamic and static. Either one should do. Each program that you create will have its own main function, but you can reuse your library without any problems in different programs.

Now as to the practical element of creating a library... see my C++ Library Project Template.

Michael Aaron Safyan
Creating a library is overkill just for including multiple files in a program.
Chuck
+7  A: 

What are you trying to do with the multiple main functions?

If you are trying to compile multiple different programs at once, you need to compile each one separately (i.e. only one main per program).

If you are trying to compile one program and want the multiple main functions all to run, you can't. You need to specify only one main and rename the others to something else (and call them from the single main in the order you want them to run).

If you are trying to use just one of the main functions as the single entry point to your program and ignore the others, then you should not include the files with the other mains when you are linking. I suggest placing each main in a separate file if you wish to keep them, and only include one of these main-files when you link/compile.

If you get this error by mistake, then you are probably doing something wrong with the project in your IDE. Perhaps you are accidentally trying to compile multiple different programs into one? You might need to specify each file containing a main as a separate build product. C is not like Java where you can put a main method inside every class and specify which one to call; the main in C is a global name.

Arkku
A: 

As others have said, your project may only have a single main function.

Why are you trying to have more than one main function? Is it because you are putting multiple small example programs into one project and each of these has a main? If that is the case you may need to create a separate project for each example so that your IDE won't ask the compiler to compile/link source from multiple examples into one program. Your IDE might also support a concept like a target, that allows you to keep code for multiple, related programs in one project and than choose which program (target) to actually build. The IDE will then compile/link only the files in that target.

vkit
+2  A: 

As many have said, you can only have one main per program. You don't want to go through the hassle of creating a new project for each example as you go through a book. That's understandable, but you'll have to do basically that. I see two alternatives:

  1. Use the new project function in your IDE (like VS2010). This will do all the hard work for you. You can always delete them later.
  2. If you don't care to keep the code around, just empty the file (or even the main() function) and re-use it. With book examples, you probably will never revisit the code anyway so just deleting it should be fine.
Steve Rowe
A: 

try using static keyword e.g.:

file1.cpp:

#ifdef RUN_FILE1
#define STATIC static
#else
#define STATIC
#endif

int STATIC main(int argc, char **argv(){}

file2.cpp:

#ifdef RUN_FILE2
#define STATIC static
#else
#define STATIC
#endif

int STATIC main(int argc, char **argv(){}

for compilation add /DRUN_FILE2 or /DRUN_FILE1.

Just an idea.

XAder
I think it would be better to figure out how to get the IDE to compile just the desired program than to juggle with the preprocessor.
Arkku
Anyway it seems that solution in a above form is not allowed - http://stackoverflow.com/questions/320461/why-main-cannot-be-declared-as-a-static-in-c don't know exact reason or standard for it.
XAder
A: 

IF you're using MS linker, use the /FORCE:MULTIPLE linker option. The first main symbol encountered will win. Not sure what the option is for other linkers.

jaws
+1  A: 

Actually, I find Dev-C++ supports working on multiple main files that are not part of any project, so I can create an run as many files as I need.

Thanks all who corporate here :) Gook luck for all.

Also, for Linux/win I found Code::Blocks do that trick. thanks.

Mohammed