2

Is it possible to have in tabs and run multiple different items in the same project, for example:

enter image description here

When I press ctrl+f5 it will either run only the first item or return an error that there's more than one main().

I'm asking this because currently, in order to run multiple different programs I have to make a new project, then add an item, then set that project as start up project, this is really inefficient and annoying and I can't easily switch between tabs like that.

Note: this is for .cpp projects/items.

shinzou
  • 5,850
  • 10
  • 60
  • 124
  • Visual Studio is not set up to deal with little code-snippets as executables. Either just put the *.cpp files in a directory and execute `cl` manually on the command line on them or just change the main method for them to `maintest` and `maintest2` and then make `main` that calls either one or the other – PeterT Nov 29 '14 at 18:48
  • @PeterT Is it possible to execute .cpp files without compiling them? And making a `main` that calls the others, can you explain a bit how is it done or tell me what to google? – shinzou Nov 29 '14 at 18:54
  • 1
    If all your programs are made of a single cpp file, and it's too much effort for you to create seperate programs out of them then you have bigger problems than this. Learn how to use the tools and language you use properly? – stijn Nov 29 '14 at 18:54
  • Yes that's what I'm doing here asking this question. Very helpful @stijn, you should earn a badge for that. – shinzou Nov 29 '14 at 18:56

1 Answers1

1

You cannot have more than one main in a project.

However you can modify a projects run command, per Microsoft:

  1. Select the solution in Solution Explorer and then choose "Properties" on the context menu.
  2. Select "Common Properties", "Startup Project" on the "Properties" dialog box.
  3. For each project that you want to change, choose either "Start", "Start without debugging", or "None".

EDIT:

So you're saying rather than wanting to run in parallel you want to build in parallel, in the same project.

That's a really nasty hack as described here: https://stackoverflow.com/a/4775245/2642059

Just bear in mind if you try you're going against Visual Studio's design. Think of it like using a pistol to bring down an elephant cause you don't like how long it takes to load the elephant gun.

EDIT:

Before:

Test.cpp:

int main(){
    return 0;
}

Test2.cpp:

int main(){
    return 2;
}

After:

Test.cpp

int test(){
    return 0;
}

Test2.cpp

int test2(){
    return 2;
}

main.cpp

int main(int argc, char* argv[]){

    if(argc > 1 && atoi(argv[1]) == 2){
        return test2();
    }
    else
    {
        return test();
    }
}
Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • This only saves me from selecting each project as start up project each time, I'll still have to do the other steps. I'd rather copy paste from Notepad++... – shinzou Nov 29 '14 at 19:15
  • @kuhaku I fear what your other steps are. Can you enlighten me? Projects in Visual Studio are designed to build 1 executable. You can include the same source files in multiple projects and build similar executables. – Jonathan Mee Nov 30 '14 at 04:12
  • I wrote all of the steps in the second paragraph in the question. I wish it was possible to build multiple executables from the same project... You probably know how to call different mains() from multiple items into a single main() in one item as PeterT mentioned in the comments, this could save me some time, if it's not a hassle, can you explain how to do it please? – shinzou Nov 30 '14 at 20:01
  • @kuhaku I've added you a link in the answer, but let me ask you this, would it work to pass a command line argument to `main` and just have `main` use that to select which "executable" will run? – Jonathan Mee Dec 01 '14 at 12:55
  • I don't think I know exactly what do you mean but from what I understand, I think it could work. – shinzou Dec 01 '14 at 20:12
  • 1
    @kuhaku I've added some code to get you going on a command line switch between behaviors. – Jonathan Mee Dec 01 '14 at 20:41
  • @kuhaku If you have more questions about how to do the command line behavior switching, open up another question and add the link here and I'll come try to answer the questions. – Jonathan Mee Dec 01 '14 at 20:50