2

I have two simple, standalone, command line programs. Each is a single source file (say Program1.cs & Program2.cs), each containing a static void Main(..), and each should compile to a single, standalone binary. They're related so I want to keep in a single directory for simplicity.

With .Net pre-Core, it's easy to build with csc Program<x>.cs & out comes the exe.

Is there a way to replicate that in .Net Core? Or does it mean 2 separate projects? So far, I can't find a way to generate 2 binaries, each with a separate entry point, in one project.

Thanks.

--

  • These are simple demos. I know it's perhaps not representative of typical projects, where there would be multiple source files with a single entry point. However ability to scale down to simple cases is important too.
sfinnie
  • 9,854
  • 1
  • 38
  • 44
  • Not sure if you can do this with a single project, but you can do it with a single solution. Place your classes in different projects within a solution, Then: Solution Properties -> Common Properties -> Startup Project -> Multiple Startup Projects. – user5226582 Jun 22 '16 at 10:27
  • @user5226582 That won't build them to the same directory though. It will merely start them both when he hits F5. – RB. Jun 22 '16 at 10:35
  • You can automate that: [Copy files from one project to another using post build event](https://stackoverflow.com/questions/11001822/copy-files-from-one-project-to-another-using-post-build-event-vs2010) – user5226582 Jun 22 '16 at 10:37
  • 4
    This question is about .NET Core, solution-based methods won’t work there. – poke Jun 22 '16 at 10:38
  • My bad, missed that. – user5226582 Jun 22 '16 at 10:39

1 Answers1

2

What you can do is to use separate configuration for each program and in that configuration set which files to compile.

In your case, the relevant section of your project.json might look like this:

"configurations": {
  "Program1": {
    "buildOptions": {
      "compile": {
        "exclude": "Program2.cs"
      }
    }
  },
  "Program2": {
    "buildOptions": {
      "compile": {
        "exclude": "Program1.cs"
      }
    }
  }
}

You can then build Program1 with dotnet build -c Program1 and run it with dotnet run -c Program1 and similarly for Program2.

svick
  • 236,525
  • 50
  • 385
  • 514