4 ms·
Might go a bit off topic but i have to bring this up since 9 out of 10 make tutorials on the internet do the same horrific mistake as you just did, 11 out of 10
by Too 13y ago
Might go a bit off topic but i have to bring this up since 9 out of 10 make tutorials on the internet do the same horrific mistake as you just did, 11 out of 10 code bases out in the wild as well.
In your make file example the .o files are just depending on the .cpp files, not the header files they include, the header files those included header files include and the files they include etc etc. This means nothing will be recompiled/relinked if a constant in a header file changes for example! Changed function signatures will give you cryptic linker errors with the standard solution "just try make clean first".
To solve this you can either manually update the make file every time any file changes the files it includes, which almost defeats the purpose of having an automatic build system. Or you can use automatic dependency generation by invoking your compiler with a special flag (-MMD for GCC), and suddenly make isn't as simple anymore as you laid it out to be. In conclusion your build tool must be aware of ALL inclusion rules as your compiler(preprocessor) has, or be given the information somehow. Maybe it's better to just use something designed for your particular toolchain that can come bundled with this knowledge?
- humanrebar 13y agoRight. Make is mostly a kludge around the nonexistent module system in C and C++. It's so bad (specifically due to the way file preprocessing works), that you need to have large parts of a compiler to accurately determine what the dependencies of a source file are. This is why a decent module system should be the top priority for C++17, though it doesn't look likely so far.
- conradev 13y agoHave you seen what Clang is doing[1]? [1] http://clang.llvm.org/docs/Modules.html http://clang.llvm.org/docs/Modules.html
- erikb 13y agoIs that solved with autotools? I'm not an expert, but I wonder why people talk about pure Make where a lot of bigger projects actually use autotools.
- endgame 13y agoYes, it is. automake will generate makefiles that track dependencies as a side-effect of compilation ( http://www.gnu.org/software/automake/manual/automake.html#Dependency-Tracking http://www.gnu.org/software/automake/manual/automake.html#De... ). What this means is that whenever a source file is compiled it will update the dependencies for the .o file. It has to be done then because different platforms could have different header dependencies (if you're having fun with #define).
- ArkyBeagle 13y agoThis is eminently possible with make-plus-GCC - add the line: .depend : gcc -M *.c > .depend then at the bottom: source .depend
- dmytrish 13y agoThank you for that useful option. Just a nitpick: did you mean `include .depend`?
- ArkyBeagle 13y agoGreat nit to pick. I was typing , not testing. I believe you are 100% correct. You may need to do a little finagling to bootstrap .depend when adding to an existing make file: echo "garp" > .depend; make .depend : may suffice.