简体   繁体   中英

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

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?

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.

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

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.

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!

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.

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

Another option you have is to use conditional compilation with sth like

main()
{
#ifdef VERSION1
   call_main_logic();
#else
   call_main2_logic();
#endif
}

and then define different configurations within the project. For each configuration you will then need to define preprocessor symbols appropriately (in: project settings -> configuration properties -> C/C++ -> preprocessor).

Please note, that it will be only one executable created at a time, but with switching configurations you'll get the one that does what you want at the moment. ... which may suit your needs or not, depending on more specific situation that you are in.

Edit: In addition, since different configurations create their separate output folders, you will have your both execs as outputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM