Nathan Hoad

Lesson in C++ - When in doubt, make clean && make

July 22, 2011

Something unrelated to Python today! Months back, I was doing some graphics programming when I noticed a startling problem: randomly, adding couts to my code caused segmentation faults. First thing I did, was throw the program into GDB and see where the crash was. Here’s where it crashed:

cout << "what's going on?" << endl;

Do you see the error there? Me either.

I puzzled over this for hours, then it finally dawned on me; I had forgotten to recompile some of my object files. The resulting problem was caused by the fact that I was putting these cout statements in code, which was screwing with the final binary, meaning that function calls, variables, everything, were offset in the executable binary by that call.

The story here is simple; if in doubt, make clean && make is your friend.

Naturally, a problem like this is pretty much impossible to research. I figured I must have found some creepy compiler error, or some part of my computer was dying before my eyes. After all, who ever thought a cout statement of plain text would segfault, right?