C++ QtCreator 构建返回 collect2:ld 返回退出状态 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1485239/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
QtCreator build returns collect2: ld returned exit status 1
提问by Austin Hyde
While building several different projects in QtCreator, I have run across the following build error:
在 QtCreator 中构建几个不同的项目时,我遇到了以下构建错误:
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:
在我当前的学校项目程序中,我正在尝试编译rock03.cpp。它是构建中的唯一文件,并且具有 main() 方法。我刚刚成功运行它,然后回去更改一些if()
s的顺序,现在,我只收到两个相关警告:
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.
有谁知道为什么会发生这种情况?我似乎无法以任何合理的确定性重现错误,并且 QtCreator 在我构建之前没有抱怨任何事情。
Thanks
谢谢
采纳答案by Austin Hyde
Checking the "Compile Output" pane reveals that the .pro file was trying to link the same .cpp file twice.
检查“编译输出”窗格显示 .pro 文件试图链接同一个 .cpp 文件两次。
回答by Patrice Bernassola
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.
如果唯一的消息错误是关于链接器的这个错误,原因可能是您的程序仍在运行并且链接器无法访问二进制文件。确保您的应用程序已停止,如果仍在运行,则将其杀死。Qtcreator 在编译前从不检查上次运行是否停止。
回答by Geore Shg
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...
这发生在我身上,因为我在头文件中做了一个声明,然后删除了 cpp 文件中的函数,我忘记了删除头文件中的声明。例如...
//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
回答by David Burton
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
如果您只是将其视为错误,则编译器输出确实很有帮助,但第一个候选对象可能是您仍然打开了输出程序,并且它无法写入文件,因为那会给您像这样的一个单独的 collect2 错误
回答by Roman
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.
链接问题也可能导致此错误,例如,您忘记使用“extern”指令从头文件中声明一些静态变量。
回答by Baranovskiy Dmitry
In my case it was declaring the clear virtual function.
就我而言,它声明了 clear 虚函数。
void virtual Func(MouseEvent*); // Error.
void virtual Func(MouseEvent*) = 0; // Well!
回答by Alex
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.
就我而言,文件夹权限是问题所在。检查“编译输出”窗口对于找出问题究竟是什么至关重要。(在这方面,QtCreator 与 Visual Studio 正好相反,所以需要一些时间来适应。)我尝试正确设置权限,但之后似乎没有用,最后我停用了影子构建,然后我去了“Tools/Options/Build&Run/General/Projects Directory”并将“Directory”设置为“.”。然后它终于编译了。“它”是 Qt 5.11 中的 kmap2qmap 项目。
Just my 2 cents in case anyone might find them useful.
只是我的 2 美分,以防有人发现它们有用。
回答by Cristian P.
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.
解决方案是始终创建主窗口并确保关闭它。
回答by ilya iz
I had the same problem. My resolution is - implement all virtual functions and all slots declarations.
我有同样的问题。我的决议是 - 实现所有虚函数和所有插槽声明。
回答by kamlesh410106
There could be many more reasons for the error. But for me, on removing of unused SLOTS from the class the problem was solved.
错误可能有更多原因。但对我来说,从班级中删除未使用的 SLOTS 问题就解决了。