I'm very new to C++; can a program be started with anything other than int main? And what, exactly, is the purpose of the main part, anyway?

Some compilers have an option to rename the entry point, but I'd question its usefulness in all but the most niche cases.

And what, exactly, is the purpose of the main part, anyway?

To provide an entry point into the program. That's really it. The C++ runtime has to know how to call your code after initializing everything, and a function with a known signature is the simplest solution.

Well, for example, I'm working on a project in Xcode, and I get a Mach-O error whenever I create more than one file starting with int main. What's the correct course of action here?

Ah, now this is a different question. In C/C++, any given program must have exactly one main() function, but a program may span as many files as you like, and not all source files must have a main() - indeed, most source files won't, for the majority of non-trivial projects.

Can you give us a bit more information about what you're trying to do, perchance?

Edited 4 Years Ago by Schol-R-LEA

I'm trying to create a simple, high school level, math suite, with the multiple solvers (a quadratic equation solver, a Pythagorean solver, etc.), linked by a single opening menu. The whole thing would run in a command line environment. So far, Xcode, my compiler, has created a Main.cpp file in the project directory, and I've created all my solder files in the same directory as Main.cpp. Main.cpp's code looks something like this, for now:

//
//  main.cpp
//

#include <iostream>

whenever I create more than one file starting with int main.

That's the problem. You only want to create one file with an "int main" (that's not entirely true always, but it's true enough for your case).

What's the correct course of action here?

You want exactly one "int main". If you are creating a library, I would imagine that none of those library files should have an "int main". Generally there is an option to create a new C++ file with or without main in most IDEs. For the library files, you would pick the option to not have a main. Then you'd have a program with a main that includes the library header files and uses them.

For examle, in your case, you might have have at least four library files (Quadratic.h, Quadratic.cpp, Pythagorean.h, Pythagorean.cpp"), none of which would have a main function. Now create a main.cpp file with a main function that includes Quadratic.h and Pythagorean.h and calls function defined in those files. That should compile and link properly.

This article has been dead for over six months. Start a new discussion instead.