The first specifies that each object file depends on its corresponding source file, and defines a reasonably sensible rule to make the object file ($@
, the first target) from the source file ($^
, all the dependencies). It should use $<
, just the first dependency, since there will usually be other dependencies - headers included by the source file. You can often leave this rule out altogether; there's an implicit rule for compiling a single C++ file.
The second specifies that all the object files depend on all the source files, and defines a dysfunctional rule which will fail to compile anything. For example, defining SRC = a.cpp b.cpp
, this expands to
g++ -c a.cpp b.cpp -o a.o
which fails since -c
only compiles a single source file:
g++: fatal error: cannot specify -o with -c, -S or -E with multiple files