When I run a Java Program on Eclipse, each time I run a file, Compiler will check does it has main, if it has, I can run. And if not, I cannot.
But when I run a C++ Program (CDT) on Eclipse using MinGW, at Compiler doesn’t work like that. After I compile, don’t matter I run from which file, Compiler will search whole project to find ONE MAIN So, if I has many main file, I will receive error.
Please tell me in Eclipse, how to treat C++ Compiler to have multi main file and work like on Java: it just find main on which file I run from.
@: I have multi main file just for debugging purpose. !!!
Thanks 🙂
unlike java in a C/C++ you can only have one main:
( source )
But: As a workaround you can use preprocessor to decide which of your
main‘s you want to use.Example:
Now you can decide wich one you want to use by setting proper flag at compiletime. If none is set you’ll use the default one (
#elsepart).Alternative: Exclude all your files containing a
main-function from compilation and include only the one you need. However, this will only work if those files dont contain more than themain.