Visual Studio: how to create a project that would

2019-01-23 23:34发布

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?

6条回答
可以哭但决不认输i
2楼-- · 2019-01-23 23:59

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.

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.

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

查看更多
▲ chillily
3楼-- · 2019-01-24 00:04

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).

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.

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楼-- · 2019-01-24 00:09

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

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".

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

查看更多
ゆ 、 Hurt°
5楼-- · 2019-01-24 00:10

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

查看更多
放荡不羁爱自由
6楼-- · 2019-01-24 00:17

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.

查看更多
家丑人穷心不美
7楼-- · 2019-01-24 00:21

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

查看更多
登录 后发表回答