Choosing which main function to use in Visual C++ 2010

11,925

Solution 1

Multiple main functions mean that the original code does not create a single executable, but rather a set of them. You should figure out what parts belong to each one of the executables (read the Makefile) and then create different projects inside the solution one for each one of the executables (do the same for the libs). Then you can use the IDE to select which executable you want to compile/run.

Solution 2

in the Configuration Properties for each source file (right-click in Solution Explorer) you can select 'Excluded From Build'. As this is a per-configuration setting, you can add some configurations and mutually exclude the files with main(). For instance for configuration 'MainA' you include maina.cpp and exclude mainb.cpp and mainc.cpp, for 'MainB' include mainb.cpp and exclude maina.cpp and mainc.cpp, etc.

Another option would be to have only one main() and select the appropriate source using arguments or a configuration file. Or, maybe the best solution, create one project for each main file and put the common parts in a static or shared library.

Share:
11,925
CVertex
Author by

CVertex

"There's a risk I'll OD on imagination like my friend Chauncey" - Wonder Showzen.

Updated on August 21, 2022

Comments

  • CVertex
    CVertex over 1 year

    I'm porting a C++ application from Unix and the original developer created several files with main() functions, then use Makefile to choose which main() to use.

    How do choose which file contains the main() function in Visual C++ 2010?

    Currently, when I compile I get a linker error due to duplicate main() symbols.

    The only thing I can think of is macro conditional.

    Any other ideas?