Sabina Yasmin
Sabina Yasmin

Reputation: 47

Can I use multiple main function under a project on Visual Studio 2019?

I am using Visual studio 2019 for learning C++. Every time I am crating project for each program. Is it possible to crate and run multiple main source file on Visual Studio like Java on Eclipse/Netbeans/IntellijIdea? Thank you.

Upvotes: 0

Views: 3331

Answers (2)

Abdelbaki Boukerche
Abdelbaki Boukerche

Reputation: 1692

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.

C++ doesn't work like this because at the end every thing get linked to a single executable.

If you want to have multiple entry points You need to:

  1. Create seperate *.cpp file for every entry point, each file should define its own main() function.
  2. Each file should have a separate binary target in your build system(Cmake...etc)
  3. Build & Run target as needed.

Upvotes: 1

BalGu
BalGu

Reputation: 53

There is two ways of doing it. Either you use cmake, more info here

or

you put the main functions in separate namespaces and then define, which one do you want to run. example here

Upvotes: 1

Related Questions