Skip to content

Instantly share code, notes, and snippets.

@Experiment5X
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Experiment5X/a39d86fea28c0a1a4b36 to your computer and use it in GitHub Desktop.
Save Experiment5X/a39d86fea28c0a1a4b36 to your computer and use it in GitHub Desktop.
An interesting bug I found in one of my programs

Today I came across a bug in my project that originally had me thinking I was experiencing yet another bug in Qt Creator, but it turns out I was wrong. In my project I had three main files:

ClipboardSynchronizer.h
ClipboardSynchronizer.cpp
main.cpp

The buggy version of my code in main.cpp was this:

#include <QApplication>
#include "ClipboardSynchronizer"

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    return a.exec();
}

When building the application in QtCreator I got thousands of errors and warnings, so many in fact that the number didn't stop climbing and I had to kill the entire process. I have had similar things happen to me before, in which case I would copy all the code in the file I was working on, delete the file contents, paste it back, and then rebuild. For some reason that would usually work. Unfortunately for me, I was not so lucky. I was stumped for a while. I tried a bunch of different things I found on google but nothing was working. Eventually I went back and looked at the files I was including, and found that I was including the wrong one.

#include "ClipboardSynchronizer"

Notice how the .h extension is missing from the file name. QtCreator didn't give me a warning that the file doesn't exist because that file actually does exist. It's the path to the executable file that it compiled the previous time I built the project. The preprocessor was reading the contents of that binary in as a text file, and then the compiler was treating it like C++ code which explains why there were so many errors. If we look at the error string it was giving us, it makes sense.

stray '\177' in program

'\177' is the UTF8 character ± which has no place in a C++ program, explaining the compiler's decision to label it as stray.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment