Build two VS2010 C++ projects into the same output

2019-07-15 22:52发布

问题:

I have two projects in the same VS 2010 solution. They are reusing same code and I want VS2010 to build both projects' Executables into the same output folder.

Apparently this is not trivial even though http://msdn.microsoft.com/en-us/library/ms165411.aspx says it is.

One build process deletes previously build one (or doesn't even copy first build executable into the folder) so I just have one executable from the project built last.

If built separately, both compile and output respective executables separately. One project is dependent on the other one (to resolve access conflicts to the reused OBJ files, since one probably holds the handle for too long while the other tries to build).

A mystery I would appreciate a bit of help.

回答1:

You can redirect visual studio to any folder you want by going to the project property pages and then changing the output directory field of both folders to the same location (either in absolute or relative location). The two project cannot have the same name (actually the name of the executable must be different as perhaps some other auxiliary files generated) or else generating one project will erase the other.

Above I answered your question, but I believe that you are approaching things from the wrong point. if you have common code, create a library project/folder. Place it somewhere and add the source code itself to each project, so each project can use it, or you can actually generate the lib file itself with a header containing prototypes of the functions in the lib file, and then use a header file and the lib file in each project. Use Visual Studio's VC++ Directories tab to point to the lib folder and to the header file.

This is usually the right approach which can avoid headaches (if you accidentally change source code in one project and it affects the other one. With lib file you're forced to make a bit more thoughtful changes).

Another benefit is that you don't have to recompile it each time, even if you clean it (you will always rebuild if you clean and use source code).