views:

118

answers:

4

Suppose I have a bunch of C++ files: A.cc, B.cc, C.cc, and their associated header files. A.cc makes use of classes in B.cc and so on.

Now say I want to build the source files. After the preprocessing phase, can I theoretically compile (not link) all the files simultaneously? (A.cc -> A.obj, ...)

I'm just wondering if there is ever a time when I would have to wait until I finish compiling A.cc before compiling B.cc.

+8  A: 

No, unless you're doing something weird indeed, the compilation of B.cc will not depend on the result of compiling A.cc (and vice versa). That's why make -j (running multiple "jobs", i.e., processes, in parallel, each compiling a file at the same time) is a popular usage, especially of course on multi-core machines (but not those only, since even without multiple cores a small number of simultaneous jobs may in the end finish faster than the same set of jobs arbitrarily serialized -- one may be blocked waiting for disk I/O while the other is churning a CPU-intensive part of the compilation...)... as long as you do have enough available physical RAM, that is;-).

Alex Martelli
A: 

That is what the headers are for, right? make -j N will do this for you, although it does it based on fallible user-generated Makefiles.

Matt Kane
A: 

The extensions on the end of the files are more or less meaningless. What matters is that you've got a full definition to all of the classes you're trying to compile, even if they aren't implemented yet. Because the .h and .cc or .cpp extensions are arbitrary, what matters most is the content of the files.

Generally speaking, if you are able to fully describe an object of the class, then you won't run into issues. If the class definition doesn't exist yet in the chain that you've set up (which can happen with circularly-dependent headers), then you've got to do some magic.

The point is that it's really up to you as the designer/developer if you'll run into this problem

San Jacinto
Its hard to find fault with anything you've said specifically, since nothing you said was false. But in the end, you've also said nothing.
John Dibling
@John If you answer the question as "yes," then it sounds like a very common problem that can never be resolved. If you answer it as "no," then you are wrong because you can inject a dependency into a source file build (Alex calls it "something weird indeed") which would cause the problem. Thus, I gave a very, very brief explanation of how to avoid the dependencies but focused on the fact that it's up to the developer, not the compiler, to work this issue out. Note, I'm not looking for anyone to change their downvote, I'm just explaining where I'm was coming from.
San Jacinto
FWIW It's not my downvote
John Dibling
A: 

There is only one case where you actually want such dependence: when one file generates C++ code that is compiled later. Make is flexible enough to support this. But when you think of your regular projects, no, you don't want and should not have such dependencies.

Alex Emelianov