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个exes。是否可能以及创建此类项目的指示是什么?
6 个解决方案
#1
25
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.
但在“正常”情况下,一个项目意味着一个输出。为了获得两个可执行文件,您通常会创建两个项目。
#2
14
You need a solution which includes two projects. Have a read of the Visual Studio documentation on solutions and projects.
您需要一个包含两个项目的解决方案。阅读有关解决方案和项目的Visual Studio文档。
#3
5
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 main
source file in each configuration.
在项目中,为每个所需的输出创建配置。然后,每个配置中只包含一个主源文件。
In VS, this means for each source file with main
: right-click -> Properties -> Excluded From Build = Yes. So, once you're done, only one main
source 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:右键单击 - >属性 - >从构建中排除=是。因此,一旦完成,每个配置只构建一个主要源。然后,您可以在“项目属性”中为每个配置指定不同的输出。我在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.
我正在使用这种方法,因此我可以为一个项目进行多次测试,而不会使测试项目比实际代码项目更混乱。
#4
3
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 step I two Link-cmds (copied form original link-cmdline and modified it a bit) taking as input main1.obj resp. main2.obj and outputting App1.exe resp. App2.exe. It's executed after Compiling and before linking.
在自定义构建步骤中,我将两个Link-cmds(复制形式为原始链接-cmdline并稍作修改)作为输入main1.obj resp。 main2.obj并输出App1.exe resp。 App2.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.
缺点是我无法阻止(还)链接orinal exe(重复主函数的错误)。解决这个问题的方法是让一个lib项目从构建中排除main()来源,并在custum-step中构建它们。
So the answer to the question should : Yes ,it can be done!
所以问题的答案应该是:是的,它可以做到!
#5
0
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个可执行文件,唯一的办法是在同一个解决方案中制作两个不同的项目
#6
0
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文件位于一个位置,则可以指定自定义的构建后操作:对于两个项目:查看项目属性页面,在构建事件中选择Post-Build事件,然后在命令行字段中输入命令将输出复制到您想要的位置,例如:copy $(TargetPath)c:\ yourlocation / Y然后在构建解决方案后,输出文件将被复制到该位置。
#1
25
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.
但在“正常”情况下,一个项目意味着一个输出。为了获得两个可执行文件,您通常会创建两个项目。
#2
14
You need a solution which includes two projects. Have a read of the Visual Studio documentation on solutions and projects.
您需要一个包含两个项目的解决方案。阅读有关解决方案和项目的Visual Studio文档。
#3
5
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 main
source file in each configuration.
在项目中,为每个所需的输出创建配置。然后,每个配置中只包含一个主源文件。
In VS, this means for each source file with main
: right-click -> Properties -> Excluded From Build = Yes. So, once you're done, only one main
source 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:右键单击 - >属性 - >从构建中排除=是。因此,一旦完成,每个配置只构建一个主要源。然后,您可以在“项目属性”中为每个配置指定不同的输出。我在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.
我正在使用这种方法,因此我可以为一个项目进行多次测试,而不会使测试项目比实际代码项目更混乱。
#4
3
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 step I two Link-cmds (copied form original link-cmdline and modified it a bit) taking as input main1.obj resp. main2.obj and outputting App1.exe resp. App2.exe. It's executed after Compiling and before linking.
在自定义构建步骤中,我将两个Link-cmds(复制形式为原始链接-cmdline并稍作修改)作为输入main1.obj resp。 main2.obj并输出App1.exe resp。 App2.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.
缺点是我无法阻止(还)链接orinal exe(重复主函数的错误)。解决这个问题的方法是让一个lib项目从构建中排除main()来源,并在custum-step中构建它们。
So the answer to the question should : Yes ,it can be done!
所以问题的答案应该是:是的,它可以做到!
#5
0
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个可执行文件,唯一的办法是在同一个解决方案中制作两个不同的项目
#6
0
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文件位于一个位置,则可以指定自定义的构建后操作:对于两个项目:查看项目属性页面,在构建事件中选择Post-Build事件,然后在命令行字段中输入命令将输出复制到您想要的位置,例如:copy $(TargetPath)c:\ yourlocation / Y然后在构建解决方案后,输出文件将被复制到该位置。