I encountered an error.
undefined reference to 'get_file_modify_d' collect2.exe: error: ld returned 1 exit status
This error is quite simple. It occurs when a variable or function is declared but not defined in the source file, or the corresponding library is not linked.
However, when I defined functionb in the source file, declared it in the header file, and then used it in a.cpp, I still encountered this error during link stage.
I later discovered that the parameter of functionb declared in the header file was const string, which did not match the string in the source file. I modified the parameter type in the header file, and the a.cpp that references it did not need to be changed. I continued to compile, but the error still persisted.
Because only the header file changed, the makefile will not recompile the other .o files.
Unless I write the header file dependencies in the makefile, it will only recompile when the header files are modified.
So the previous declarations of the functions are still included in old .o files. It still links according to the previously declared symbols, which causes an error.
Cleaning all .o files and recompiling solves the problem.
In fact, link errors caused by not cleaning .o files are not limited to this situation.
For example, if .o files compiled in another environment are used in a new build environment without cleaning .o files, the build will continue to use these files. If the new compiler version or library version does not match the old compilation environment, it can cause these link errors.