The dependencies are everything whose change requires recompiling the source code. That includes not only your #include
-d headers, but also the indirectly included system headers, and even (in principle) the compiler and build chain itself (when you upgrade your C++ compiler, you should recompile all your software). If some of your C++ code is generated from some source (e.g. by tools like GNU bison or Qt moc, or by your own script), both the sources and the generating tools are dependencies. Read also about package managers.
Practically speaking, the GCC compiler is able to output most make
dependencies, notably with -M
and related processor options. Read also about auto dependencies generation. See also this.
(in practice, you generally don't code in your Makefile
some explicit dependency on the compiler itself; but you should not forget to make clean
when the compiler has been upgraded)
Unless your main.cpp
is including my_strings.cpp
(which is not conventional and is very bad taste), your make
rule won't have a dependency from my_strings.cpp
to main.o
. But probably your main.cpp
is #include
-ing (directly or indirectly) my_strings.h
so main.o
should depend not only on main.cpp
but also on my_strings.h
As a rule of thumb, your object file my_strings.o
depends on the source file my_strings.cpp
and all the header files which are directly or indirectly #include
-d in it. Your main program
executable depends on all its object files and the libraries you are linking into it. Order of program arguments to g++
matters a lot.
It uses it like a library, right?
From what you are showing, you don't have any own libraries (but you probably use the standard C++ library, and perhaps some other system libraries). On Linux these are lib*.a
files (static libraries) or lib*.so
files (shared libraries). A library is an organized agglomeration of object code -and sometimes other resources.
I'm a bit lost on dependencies and how linking works.
Understand the difference between source code files, object files (they contain relocation information) and executables (on Linux, object files and executable files and shared libraries are using the ELF format). Read also about the role of compilers, linkers (the g++
program can run both) & build automation (for which you are using make
).
Read Program Library HowTo and much more about translation units and linkers (& name mangling), notably Levine's book on Linkers & loaders.
See also this & that & this (examples about Makefile
for C++ programs).
BTW, you should use g++
(not gcc
) when compiling C++ code. There are significant differences (even if gcc
is sometimes able to compile C++ or Fortran code, you'll mostly use gcc
to compile C code). And (assuming you use specifically GNU make) your Makefile
should mention $(CXX)
(not g++
). You need to understand the builtin rules of make
(run once make -p
to get them) and you'll better take advantage of them (e.g. use $(COMPILE.cc)
or $(COMPILE.cpp)
etc...). You certainly should pass -Wall -Wextra
(to get all warnings, and even more), and -g
(to get debugging information) to g++
. Practically speaking, you should set your CXXFLAGS
variable in your Makefile
.
Take time to carefully read GNU make documentation and Invoking GCC.
Look into the Makefile
-s of existing free software projects. For various reasons, some projects are generating their Makefile
-s with tools like autoconf
or cmake
. But most simple projects don't need that generality, and you should be able to write your own Makefile
for your C++ projects. Of course, take inspiration from existing code.