"A project with an Output type of Class Library cannot be started directly"

Viewed 430471

I downloaded a C# project and I wish to debug the project to see how an algorithm implementation works.

The project has come in a Folder, inside this folder there are -

  1. .sln file and
  2. a folder which has source files and a .csproj file.

I installed Visual Studio and opened the .sln file present in the main folder. I built the project successfully, but when I try to debug the project I get this message:

A project with an Output type of Class Library cannot be started directly In order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project.

The strange part is that I don't see a main function anywhere.

What should I do to get round this hiccup?

15 Answers
    Right Click on "Solution Explorer" -> "Properties"
    Expand "Common Properties"
    Select "Start Up Project"
    click the radio button "Single Start_up Project"
    select your Project name from the drop down list.

If still not working after the above steps, then try this.

    Expand solutions explorer.
    Right click on project name -> "Properties"
    Go to "Application" tab
    Select "Output type" 
 From the drop down list select the appropriate type according to your application.
    "Windows application" or
    "Console application"

Then save (ctrl + S)

Try debugging (F5)

You can right click the Class Library project and from the drop-down choose Initialize Interactive C# which will load your project context and you can work it in the interactive session.

In my case, the cause was that one of my projects in the solution wasn't loaded. The reason it couldn't load properly was that the file path length of one of the files was too long. Upon deleting this long file, I could reload the project, and build the solution.

If the question involves an Azure project, make sure you have the "Azure development" tool set installed, or when you go to run a solution you may get this same error.

Tools > Get Tools and Features... > Tick the box next to Azure development > Click install

Accepted answer works if your solution has a project that compiles to an exe. If your solution does not have any projects that compile to an exe, then you have to use 'Start external program'.

VS2019 instructions:

  1. right click -> properties on the main solution
  2. debug, start external program, and add command line arguments

VS2022 instructions:

  1. right click -> properties on the main solution
  2. scroll down to Debug
  3. Debug > General > Open debug launch profiles UI
  4. left click the 'new' icon in the top left, select 'executable'
  5. fill it out as per VS2019 (pick the exe and add command line arguments)
  6. when clicking the start button, first select the profile you made
Related