On Wed, 22 Oct 2014 07:29:05 CST
ag...@drrob1.com wrote:
> I am mostly finished w/ a free
edx.org course on c++. I have found
> this worthwhile, but my question is this: how can I write a program
> with dividing my code into separate files.
There is a difference between a language and a compiler. Your C++
class taught you how to express algorithms in the C++ language. Your
compiler is a tool and, like any tool, the best place to learn how to
use it is from its documentation. Not to say that's easy!
In the old days there were two tools, compiler and linker. The
compiler converted source code to object code, and the linker knitted
the object code modules and libraries together to form an executable.
Nowadays many tools combine these two functions into one. gcc is an
example.
The gcc manpage indicates the final argument as "infile...", meaning it
will process more than one input file at a time. These input files may
be any combination of C++ source code, object (.o), and libraries.
Absent options to the contrary, gcc will produce (if it can) an
executable file. The gcc option "-c" limits the invocation to
compilation, producing a ".o" object file.
Once a project has more than a few files, it's usually quicker to
compile each source file separately to object form, and link them as a
final step, because many changes require re-compilation of only one
source file.
Note of caution: GNU makes a linker, too, known as "ld". Last I
checked, you can't reliably use ld to link C++ object modules into an
executable or shared object. C++ imposes quite a bit of work on the
linker in the form of logic that executes before main() is run. That
was one motivation behind combining compiler and linker functionality
into one tool.
> Does this have something to do with make?
Since about 1975, yes. :-)
make is a tool much maligned but exceedingly useful, likely the only
declarative rule processor in your toolbox.
The problem make tackles is dependency tracking. After you edit and
save your source code, which object files (plural, it might have been a
header file you changed), libraries and executables are affected?
What, as we say, has to be rebuilt? make lets you define that
dependency graph logically, as a set of rules, and applies them based
on the files' timestamps.
Articles worthy of your time:
The Art of Unix Programming:
http://www.catb.org/esr/writings/taoup/html/ch15s04.html
(The whole TAOUP is worth reading.)
PMake ? A Tutorial
http://docs.freebsd.org/44doc/psd/12.make/paper.pdf
The pmake tutorial is ancient and describes a version of make you're
unlikely ever to use. Why point it out? Because it was written when
make was young, and assumes you've never seen such a tool before. If
you ignore some of the specifics, you'll get a nice introduction to the
basic ideas. That background will make your definitive resource, "info
make" (assuming you're using GNU make), easier to understand.
HTH.
--jkl