Recently, I wrote a small program with many analogisms. For the convenience of compilation, I wrote a Makefile specially for source code and test code.
In the process of debugging, a “Bus error: 10” suddenly appeared, which was quite unexpected. Why did such a spectacular problem occur?
The starting address of type int data must be a multiple of 4, otherwise it will cause the bus error mentioned above. With this in mind, I searched for a long time but couldn’t find any address misaligned.
When I compiled again, I found something fishy about the compilation process. The Makefile compiles the source file “.c/.cpp “into”.o “files (binaries), and then links these”.o “files to target files (executables, libraries, etc.). If some of these files are modified, the Makefile reduces compilation time by detecting the timestamp and compiling only the files that are modified later. So the question is, what happens if you change the header file?Examples are as follows:
Suppose you are going to modify two files, classA.h classa.cpp, add a member variable to the header file, and do the processing in the source file.
The two files classb.cpp classC.cpp will include the classA.h file. Because of the include type, the actual code in the file has been written to the source file and compiled into a “.o “file.
When the modification is completed, there is no change in classb. CPP classc. CPP, so when comparing the timestamp, determine that classb. CPP classc. CPP does not need to be compiled again. However, the header file classA.h of the classb. CPP classC.cpp file has really changed, so when the data is accessed, the address offset of its members will be greatly changed, no error can be found at compile time, but the execution will explode…
Solution: To sum up, it is not difficult to find the problem. The header files held before and after the code are scattered, resulting in running errors. It is only necessary to compile the classes of the modified header files that depend on, and the simple solution is to compile the current project after clean.
This blog is just for the sake of documenting a childish problem you’ve committed: if the header file changes, recompile it all or die…
The above!
So the question is, can the Makefile add dependencies on the source file’s head file?Detects the timestamp of the dependency header file and the “.o “file and compiles them. Make a note of it and study it later.