Click here to Skip to main content
15,905,875 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
A project with an Output type of class Library can not be started directly
in order to debug this project, add an excutable project to this solution which reference the library project. set the excutable project as the startup project
Posted
Updated 3-Jun-15 18:00pm
v2

In addition to Solution 1:

Here is the thing: in .NET and CLR, the main functional block and the center of its modular conception is assembly, not "DLL" or "EXE". Those PE (executable) files are nothing but modules. Visual Studio directly supports creation of only the assemblies which are made of only one module, but it's possible to create an assembly with more that one module; then one module is only main; it has the assembly manifest in it. Even for one-module assemblies, the concept of module is not redundant, the file ("EXE", "DLL") is always the module, not assembly, and you can always calculate the module from reflection, and thus get, for example, the file name.

Those modules should be not confused with referenced assemblies. Many assemblies reference other assemblies they depend on. Naturally, any assembly can be referenced by more than one assembly. If the assembly is referenced, all of its modules should present in the system, so, recurrently, the same goes for the assemblies it references in turn.

Now, we are coming to the main idea: the concept of "DLL" and "EXE" are totally irrelevant to .NET and CLR. They are nothing but naming conventions for the file names. By default, if you specify "class library" as the output type, ".DLL" is created, ".EXE" in all other cases. When an assembly is created, it can be referenced by other assemblies, even if it is ".EXE". Not a library? Does not matter. There are even cases when referencing the ".EXE" makes practical sense. (Let me skip this topic.) So, how "class library" assemblies are different form other output types? They can be successfully compiled without the method called entry point, but non-libraries are required to have exactly one: some static Main function of some class. If you have two such functions, you have to chose one, so the runtime system would "know" what to call. Please see: http://en.wikipedia.org/wiki/Entry_point#C.23[^].

The library assemblies may or may not have such function, of have more than one, or more. This is the only difference between "DLL" and "EXE". Moreover, you can create your own runtime system and the type of host where some "DLLs" would play the role of "application".

Now, as Solution 1 explained to you, only application assembly can be started, because the runtime system "knows" how to start them: by calling Main. As to the "startup project", this is just the artifact of Visual Studio (or other IDE), as well of the concept of "project" itself. There are no "projects" during runtime. This startup project is needed by only one trivial reason: because there is one comment "Start Debugging" menu item which is supposed to work ignoring the selection in Solution Explorer. That's why there is on additional "selected" item. And, naturally, Visual Studio should "know" how to "run" it, so a library is not suitable. This is as simple as that.

By the way, I want to use the occasion to shed some light on "mysterious" "Console Application" output type. Many mistakenly think that it means "not windows application". Wrong. This "classification" of Visual Studio is utterly misleading. This item simply means "show console". In other words, if you develop a window application, it won't show console, but if you then change the output type to "console application", it will show console but will remain a window application, with all the UI you developed. Don't let VS to confuse yourself. :-)

—SA
 
Share this answer
 
There are two types of .NET assemblies: Executables (.EXE) and Dynamic Link Libraries (.DLL). Executables can be executed ("started"), DLL's not.

As the error message implies, you have at least a DLL in your solution and it is currently set as start-up-project when starting your application from within Visual Studio (although it makes no sense for DLL's).

If you also have an executable project in your solution (which probably uses the DLL) then set that as your start-up-project by right-clicking it in the solution explorer and selecting "Set as StartUp project".

If you don't have an executable project in your solution then either add one (e.g. a console project), set it as start-up-project and make it use the functionality of your DLL; or change the project type of your DLL-project to an executable by opening the project-properties (again right-click in solution explorer) and selecting "Console Application" as the "output type". In the latter case you would also have to add a static class with a method "static void Main()" to your project as the entry-point for execution.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Jun-15 1:47am    
This is all good, but (sigh...) I voted 4 this time.
I know these kinds of confusions pretty well. This answer won't completely remove the confusion, because there are a number of close and "parallel" concepts mixed up together, they all should be explained at once in one place. One confusing point is that "Console application" output type; many develop long time and don't understand it.

Anyway, please see my Solution 2 where I tried to clarify it all at once.

—SA
Sascha Lefèvre 4-Jun-15 2:13am    
It's alright :) Thank you, Sergey. My 5 for your answer.
Sergey Alexandrovich Kryukov 4-Jun-15 2:27am    
Thank you, Sascha.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900