.net Visual Studio 项目构建为可执行文件和 DLL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2466492/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:06:05  来源:igfitidea点击:

Visual Studio project build as an executable AND a DLL

.netdllvisual-studio-2008msbuild

提问by East of Nowhere

In Visual Studio 2008 project properties, Application tab, I can set the Output type to Windows Application, Console Application, or Class Library. I have a project that I want to build as a stand-alone tool (console app) andbe available to a couple other tools I'm working on as a class library.

在 Visual Studio 2008 项目属性的应用程序选项卡中,我可以将输出类型设置为 Windows 应用程序、控制台应用程序或类库。我有一个项目,我想将它构建为一个独立工具(控制台应用程序),并且可供我作为类库使用的其他几个工具使用。

The VS GUI only lets me choose one or the other, and building the proj twice all the time is inconvenient.

VS GUI 只让我选择其中之一,并且一直构建项目两次很不方便。

How can I set it up to build both output types in a single build job? Do I write some custom MSBuild .targets file or what?

如何设置它以在单个构建作业中构建两种输出类型?我要写一些自定义的 MSBuild .targets 文件还是什么?

回答by Cheeso

If I'm not mistaken, you can use the EXE as a class library.
Just add a reference to it, in the other projects. A .NET EXE is just an assembly.

如果我没记错的话,您可以将 EXE 用作类库。
只需在其他项目中添加对它的引用。.NET EXE 只是一个程序集。

回答by codenheim

You could build the dll by default, and make another dependant target that is simply a wrapper console app that uses the dll.

您可以默认构建 dll,并创建另一个依赖目标,它只是一个使用 dll 的包装控制台应用程序。

回答by JaredPar

I think the simplest solution is to build as a EXE and then have a post build action which copies the EXE into a DLL. There is no real difference between the two in .Net.

我认为最简单的解决方案是构建为 EXE,然后进行构建后操作,将 EXE 复制到 DLL 中。.Net 中的两者之间没有真正的区别。

回答by Jay Riggs

Create two separate projects, one for your Console app and one for the Class library. Set the appropriate Output type for each.

创建两个单独的项目,一个用于您的控制台应用程序,另一个用于类库。为每个设置适当的输出类型。

Don't forget to add a reference to your Class library to your Console app project though.

不过,不要忘记将类库的引用添加到控制台应用程序项目中。

回答by adrianbanks

You cannot compile to both an exe and a dll. Whether an assembly is treated as an exe or a dll is determined by a single bit flag in the portable executable header of the file (see http://msdn.microsoft.com/en-us/magazine/cc301805.aspxfor more info). This flag cannot have both values.

您不能同时编译为 exe 和 dll。程序集被视为 exe 还是 dll 由文件的可移植可执行标头中的单个位标志确定(有关详细信息,请参阅http://msdn.microsoft.com/en-us/magazine/cc301805.aspx)。此标志不能同时具有两个值。

What you can do to satisfy your need is to add a reference to your exe. You cannot do this in some versions of Visual Studio (2005 and below) as the UI will not let you, but you can hand edit the project file to add the reference. Later versions of Visual Studio do allow you to add references to exe files using the UI.

为了满足您的需要,您可以做的是添加对您的 exe 的引用。您无法在某些版本的 Visual Studio(2005 及更低版本)中执行此操作,因为 UI 不允许您这样做,但您可以手动编辑项目文件以添加引用。更高版本的 Visual Studio 允许您使用 UI 添加对 exe 文件的引用。