8

While building several different projects in QtCreator, I have run across the following build error:

collect2: ld returned 1 exit status

After only changing a few things (that should not change anything significant in the build), it will go away if it has already appeared, or it will appear if it's not there.

In my current program for a school project, I am trying to compile rock03.cpp. It's the only file in the build, and has the main() method. I had just run it successfully, and went back to change the order of some if()s, now, I get only two relevant warnings:

overriding commands for target 'rock03.o'

and

ignoring old commands for target 'rock03.o'

along with the error in question.

Does anyone know why this would happen? I cannot seem to reproduce the error with any reasonable certainty, and QtCreator is not complaining about any thing before I build.

Thanks

Austin Hyde
  • 26,347
  • 28
  • 96
  • 129
  • 1
    `collect2: ld returned 1 exit status` is not the error message itself. It's just a note that linker failed. Could you post previous lines of error output? – P Shved Sep 28 '09 at 03:40
  • There are no other lines of errors. That's all there is to it. – Austin Hyde Sep 28 '09 at 04:14
  • 4
    If you click the "Compile Output" button you should get linker output, it doesn't show up in the normal build errors output in qt creator. – Dan O Sep 28 '09 at 04:39
  • 1
    "Compile Output" button is at the bottom of the qt creator window, next to Build Issues, Search Results, etc. – Dan O Sep 28 '09 at 04:40
  • Thanks, that did it. Apparently I was building the same file twice, causing duplicate symbol errors... I will now know where to look in the future. – Austin Hyde Sep 28 '09 at 13:36
  • i had that problem to. i got the output that windows denied permission to write files. so i started qt creator as admin and all's fine. – mkind Nov 18 '09 at 22:06

10 Answers10

5

If the only message error is this one concerning linker, the reason can be that your program is still running and linker can not access to the binary file. Be sure your application was stopped or kill it if still running. Qtcreator never checks if previous run was stopped before compiling.

Patrice Bernassola
  • 14,136
  • 6
  • 46
  • 59
5

This happens to me because I make a declaration in the header file, then delete the function in the cpp file and I forget to delete the decleration in the header. For example...

 //header file
class CLASS : public Q_OBJECT
{
...
protected:
void mouseMoveEvent(QMouseEvent*);
}

//source file

void CLASS::mouseMoveEvent(QMouseEvent*e)
{
...
}
    //I'll delete this, then forget to delete "void mouseMoveEvent(QMouseEvent*);" in the header file
Geore Shg
  • 1,299
  • 5
  • 23
  • 38
  • SUPER annoying bug. Was trying to figure out this error for 30 mins. Couldnt find it, and this was exactly it (same function and everything lol). +++++++ – Toadums Oct 01 '12 at 02:30
2

The compiler output is really helpful if you're just getting this as an error, but the first candidate is probably that you've still got the output program open, and it can't write to the file, because that'll give you a solitary collect2 error like this

David Burton
  • 1,130
  • 10
  • 12
2

This error may also occur because of problems with linkage, for example, you forgot to declare some static variables from header file using 'extern' directive.

Roman
  • 21
  • 1
2

In my case, folder permissions were the problem. Checking the "Compile Output" window is crucial for finding out what exactly the problem is. (QtCreator is the opposite of Visual Studio in that regard, so it takes some getting used to.) I tried setting the permissions properly, but after that didn't seem to work, in the end I deactivated shadow build and then I went to "Tools/Options/Build&Run/General/Projects Directory" and set "Directory" to ".". Then it finally compiled. "It" being the kmap2qmap project in Qt 5.11.

Just my 2 cents in case anyone might find them useful.

Alex
  • 1,198
  • 2
  • 12
  • 26
1

In my case it was declaring the clear virtual function.

void virtual Func(MouseEvent*); // Error.
void virtual Func(MouseEvent*) = 0; // Well!
Baranovskiy Dmitry
  • 463
  • 2
  • 5
  • 23
1

This happens when you do not close your main app (so the output executable is still running, but without any visible window). An example:

int main() {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

This app ends fine when you close the main window, but this code

int main() {
    QApplication a(argc, argv);
    QDialog w;
    w.exec();
    return a.exec();
}

doesn't close the app when you close the dialog (at least for me).

A solution is to create always your main window and make sure you close it.

0

There could be many more reasons for the error. But for me, on removing of unused SLOTS from the class the problem was solved.

Austin Hyde
  • 26,347
  • 28
  • 96
  • 129
0

I had the same problem. My resolution is - implement all virtual functions and all slots declarations.

ilya iz
  • 470
  • 1
  • 6
  • 19
-1

Checking the "Compile Output" pane reveals that the .pro file was trying to link the same .cpp file twice.

Austin Hyde
  • 26,347
  • 28
  • 96
  • 129