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.
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 namedmain
linked together.
If you instead want them to be separate programs each, you will have to link them separately.
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.
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.
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 main
s 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.
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.
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:
- 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.
- 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.
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.
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.
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.