tags:

views:

86

answers:

4

I just found out recently that you can configure Visual Studio (but this question is the same for any compiler) to dump the intermediate .o files into a separate folder outside of the source tree, instead of alongside each individual project. That makes it easy to clean the project up to archive to zip or something along those lines.

Why is that kind of configuration not more common? Are there any significant downsides?

+1  A: 

This is probably a question with a history answer. The first C compilers (and thus the first C++ compilers) Were written on and for Unix. There is no 'standard' place for much of anything in unix, with only a few exceptions. As such, the normal practice is to just put everything in the current working directory, unless instructed otherwise.

TokenMacGuy
Visual Studio doesn't output the object files in the current working directory though, it dumps them in generated folders in each project; the question is why it doesn't just group them by solution. Plus Microsoft isn't exactly well known for doing things a certain way because it's historically how others have done it
Michael Mrozek
Grouping them by solution would actually make the problem described in the question (of name clashes) much worse. Admittedly I've never seen file names clashing within a single project (seriously, why??), but across projects - possibly maintained by different people - in a single solution, that is perfectly possible.
Pavel Minaev
@Pavel: Why would that make the problem worse? Put obj files in `$(SolutionDir)\obj\$(ProjectName)` or something like that and they won't clash across projects, while still being outside the source tree. (also, where did the OP ask anything about name clashes?)
jalf
+3  A: 

This is the default in Visual Studio, and has been for quite some time (at least as far back as VC++ 6). The intermediate directory defaults to the same as the output directory, not the source directory. This means that all object files are placed alongside the final output.

In fact, it requires some jiggery-pokery if you're working with projects that expect compiler output to be placed alongside source files. Since VC++ defaults to giving object files the same name as their corresponding source file (but with a different extension), if you have several source files with the same name (but different paths), then compilation of each source file will overwrite the corresponding object file. The last file to be compiled "wins".

This, naturally, breaks the build.

Requiring source filenames to be globally unique across a project is actually pretty annoying. You can alter the output location so that it for example includes a path; the linker still does the Right Thing.

DrPizza
Huh? By default, it creates a Debug/Release subfolder under the project folder, which contains all the intermediary files. It certainly doesn't place "all object files alongside the final output". Not what I would call "out of source".
jalf
Yeah, but it doesn't place them alongside the source files either, but in a subdirectory.
Alex
@drPizza: You solve the "last compiled wins" scenario by doing a `$(SolutionDir)\Build\$(ProjectName)\$(PlatformToolset)\$(Platform)\$(Configuration)` for `IntDir`.
Billy ONeal
@jalf: The Debug/Release folder by default contains both compiler output (.obj) and linker output (.exe or .dll or whatever). In other words, $(IntDir) is the same as $(OutDir).This was explicit in older versions of VS, with both set to $(Configuration) or something.In VS2010 (and maybe 2008?) they tried to change this, by making $(OutDir) default to $(SolutionDir)$(Configuration) and $(IntDir) be just $(Configuration). In projects that are part of a solution this makes them use different directories; however, in standalone projects, they still stick everything in the same place.
DrPizza
@Billy ONeal: No, that's not actually enough. Identically named source files within a particular project will still conflict. For example, I'm working on porting Apple's Grand Central Dispatch to Windows. The GCD source has two files named time.c, with different paths (src/time.c and src/shims/time.c). VC++ writes both of them out as time.obj, so the last file to be compiled "wins". To prevent this, I set the "Object File Name" property to be $(IntDir)%(RelativeDir). This stops the overwriting from happening.
DrPizza
A: 

If you use Qt Creator as your development IDE then version 2 now enables 'shadow builds' by default which works in exactly the way you describe and is very useful indeed. It's also a single checkbox to turn it on or off, which is slightly less complicated than VS.

Rob
+1  A: 

I'm going to go with "because VS is developed in a vacuum, where ideas from the outside world don't often intrude. Organizing build files in this way worked acceptably in the first versions of Visual Studio (or its non-Studio precursors), and since that's the way it's always been done in-house, and no one ever came in from the outside and said "you know, the rest of the world would really like to separate intermediate junk files and their actual source code", the VS team never figured it was an issue.

It's just a guess, but I can't think of a better explanation.

jalf