C++ Visual Studio:如何创建一个可以编译 2 个 exe 文件的项目?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4775187/
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-08-28 16:26:01  来源:igfitidea点击:

Visual Studio: how to create a project that would compile 2 exe files?

c++visual-studiovisual-studio-2008executable

提问by Rella

So I have main.cpp and main2.cpp with int main in each. I want to get 2 exes out of it. Is it possible and what would be instruction to create such project?

所以我有 main.cpp 和 main2.cpp ,每个都有 int main 。我想从中取出 2 个 exe。是否有可能创建这样的项目?有什么指示?

回答by jalf

Nope, Visual Studio's project model is rigidly built around the assumption that "one project generates one output".

不,Visual Studio 的项目模型严格围绕“一个项目生成一个输出”的假设构建。

If you need two executables, you have to create two projects. You can keep them in the same solution to make things easier for yourself, but they have to be separate projects.

如果需要两个可执行文件,则必须创建两个项目。您可以将它们保留在同一个解决方案中,以使您自己更轻松,但它们必须是单独的项目。

Edit
Ok, as other answers have pointed out, it can of course be done, if you're desperate. You can add a custom build step, which does anything you like, including building another executable. (However, the build system won't understand that this file should be considered project output, and so some scenarios may fail: for example the file won't be automatically copied to the output folder, and when checking dependencies before a rebuild, it might not be able to understand which files to check, and what (or how) to rebuild.)

编辑
好的,正如其他答案所指出的那样,如果您不顾一切,当然可以做到。您可以添加自定义构建步骤,它可以执行您喜欢的任何操作,包括构建另一个可执行文件。(但是,构建系统不会理解该文件应该被视为项目输出,因此某些情况可能会失败:例如文件不会自动复制到输出文件夹,并且在重建之前检查依赖项时,它可能无法理解要检查哪些文件,以及要重建什么(或如何)。)

Visual Studio (at least up to 2008, not sure about 2010) also allows the use of nmake files, but then I think you're stretching the definition of "a Visual Studio project".

Visual Studio(至少到 2008 年,不确定 2010 年)也允许使用 nmake 文件,但是我认为您正在扩展“Visual Studio 项目”的定义。

But under "normal" circumstances, one project implies one output. And in order to get two executables, you'd normally create two projects.

但在“正常”情况下,一个项目意味着一个产出。为了获得两个可执行文件,您通常会创建两个项目。

回答by David Heffernan

You need a solutionwhich includes two projects. Have a read of the Visual Studio documentation on solutions and projects.

您需要一个包含两个项目解决方案。阅读有关解决方案和项目的 Visual Studio 文档。

回答by Electo

Here's my solution, since nobody in a Google search seems to suggest this method. It's quite simple and I've used/seen it used in other IDEs (like Code::Blocks).

这是我的解决方案,因为在 Google 搜索中似乎没有人建议使用这种方法。它非常简单,我已经在其他 IDE(如 Code::Blocks)中使用过/看到它使用过。

Within your project, create a configuration for each output that you want. Then, only include one mainsource file in each configuration.

在您的项目中,为您想要的每个输出创建一个配置。然后,main在每个配置中只包含一个源文件。

In VS, this means for each source file with main: right-click -> Properties -> Excluded From Build = Yes. So, once you're done, only one mainsource is built for each configuration. You can then specify a different output for each configuration in the Project Properties. I did this on VS 2010, but it should probably work with other versions.

在 VS 中,这意味着对于每个源文件main:右键单击 -> 属性 -> 从构建中排除 = 是。因此,一旦完成,main每个配置只构建一个源。然后,您可以在“项目属性”中为每个配置指定不同的输出。我在 VS 2010 上做了这个,但它可能适用于其他版本。

I'm using this approach so that I can have several tests for one project, without cluttering the solution with more test projects than actual code projects.

我正在使用这种方法,以便我可以对一个项目进行多个测试,而不会用比实际代码项目更多的测试项目来混淆解决方案。

回答by engf-010

I don't know if it can be done ,but the only change you have ,to do this ,is with custom build step.

我不知道它是否可以完成,但是要做到这一点,您唯一要做的更改是使用自定义构建步骤

EDIT: Since someone downvoted this ,i did a test making a dummy configuration.

编辑:由于有人对此表示反对,我做了一个测试,制作了一个虚拟配置。

In the custom build stepI two Link-cmds (copied form original link-cmdline and modified it a bit) taking as input main1.objresp. main2.objand outputting App1.exeresp. App2.exe. It's executed after Compiling and before linking.

自定义构建步骤中,我将两个 Link-cmds(从原始链接-cmdline复制并对其进行了一些修改)作为输入main1.objresp。main2.obj和输出App1.exeresp。应用程序2.exe。它在编译之后和链接之前执行。

It worked!

奏效了

The downside is I cannot prevent (yet) the linking ot the orinal exe (which errors on duplicate main function). Solution to this could be to have a lib project excluding the sources with main()from build and build them in the custum-step too.

不利的一面是我无法阻止(还)原始 exe 的链接(在重复的主函数上出现错误)。解决这个问题的方法可能是让一个 lib 项目排除main()来自 build的源代码,并在 custum-step 中构建它们。

So the answer to the question should : Yes ,it can be done!

所以这个问题的答案应该是的,可以做到!

回答by steg

You can't have more than one main() function in a single visual studio project. So you can't compile 2 executables, the only way is to make two different project in the same solution

在一个 Visual Studio 项目中不能有多个 main() 函数。所以你不能编译2个可执行文件,唯一的方法是在同一个解决方案中制作两个不同的项目

回答by BALASEM AL-ISAWI

You can create a solution with two project one for each output you want. Then head to Build menu and select Batch Build.. and select both projects. If you want both exe files to be in one place you can specify a custom Post-build action: For both project: view the project properties page and in Build events select Post-Build events, then in the Command line field enter the command that will copy the output to the location you want, something like: copy $(TargetPath) c:\yourlocation /Y Then after you build the solution the output files will be copied to that location.

您可以为您想要的每个输出创建一个包含两个项目的解决方案。然后前往 Build 菜单并选择 Batch Build.. 并选择两个项目。如果您希望两个 exe 文件都在一个地方,您可以指定自定义构建后操作:对于两个项目:查看项目属性页面并在构建事件中选择构建后事件,然后在命令行字段中输入命令将输出复制到您想要的位置,例如: copy $(TargetPath) c:\yourlocation /Y 然后在构建解决方案后,输出文件将复制到该位置。