Question

I want to call main method inside project A from main method inside project B. When I write in B's main.cpp

    #include "pathToProjectA/main.cpp"

I get

    error C2084: function 'int main(int,char *[])' already has a body

Is it possible to make such call?

Was it helpful?

Solution

No you can't. Having one and only one main() is a hard design constraint.

You need to abstract your functionality into a function, possibly turn that into a library and then both A and B can use the new library.

OTHER TIPS

That is simply not possible. You can only have one main().

You cannot have two instances of main() function in one program. It would bring a big problem for the linker: Which one should be chosen as the real main function (called when launching the program)?

If you want to make two binaries (A.exe and B.exe) and call one of them from the other, you should read something about executing external processes. It is possible by using API of the OS or some platform-independent libraries (like Qt) but definitely not by calling the other process’ functions directly.

The answers above aren't entirely true.

If it's okay with you to rename the main of your current project you can do that. Just rename it and tell the compiler to use your renamed main as entry point. See this answer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top