I am new to C, and now read some textbook and going to apply its examples.
The problem is, whenever I creates a new project and try to put more than one file that contains a main function, the linker (as I thougt0 explains saying:
/home/mohammed/tmp/abcd/main.c:4: multiple definition of `main'
(BTW, I used many IDEs, MonoDevelop, QT creator, VS2010, Codebloks, ...) I am currently uses QT Creator, It seems to be a very nice IDE.
So, there's not a workaround to solve such problem??
EDIT:
I am asking because I am in the learning stage, and not do real programming right now. I just need a simple way to create programs in C without have to create a separate project per book example. At the same time, I don't want to use Gedit/VI + commandline.
So, Isn't there any way such as cleaning the project, then compile the - just - one file that I need to run ??? BTW, In JAVA we can run a program that cotains more than one main (the IDE give me the choice among them)
it's not possible in c to have more than 1 main-function. you could use preprocessor directives like "#ifdef" to compile just one main-function at the time.
The short answer "You can't and you can". main() is called an entry point. In Java you can have main() in every class because each class can be self-contained in theory.
We can't have more than one main() function in a project. Hint: Function can return only one value. If you want to return group of values then create a structure and return it.
The Main Function in C Programming By default, the return type of the main function is int. There can be two types of main() functions: with and without parameters.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With