开发者

Source Code Dependencies

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.


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


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜