c++ 错误:未定义对_imp_* 的引用

aurhwmvo  于 2024-01-09  发布在  其他
关注(0)|答案(3)|浏览(205)

我在项目构建中遇到了像error: undefined reference to __imp__ZN12QApplicationC1ERiPPci'error: undefined reference to __imp__ZN11QMainWindow11qt_metacastEPKc'这样的错误。我已经尝试过清理和重建,但没有任何效果。
我已经看到了许多关于这个错误的其他帖子,但没有修复方法工作.我是新的Qt creator,所以我可能错过了一些大的东西在这里,但我做了一个小部件项目.这里是文件:

main.cpp

  1. #include "mainwindow.h"
  2. #include <QApplication>
  3. int main(int argc, char *argv[])
  4. {
  5. QApplication a(argc, argv);
  6. MainWindow w;
  7. w.show();
  8. return a.exec();
  9. }

字符串

主窗口.cpp

  1. #include "mainwindow.h"
  2. #include <QPushButton>
  3. MainWindow::MainWindow(QWidget *parent)
  4. : QMainWindow(parent)
  5. {
  6. setFixedSize(500,500);
  7. QPushButton myButton;
  8. myButton.setText("Hello world, this is a GUI app!!!");
  9. }
  10. MainWindow::~MainWindow()
  11. {
  12. }

mainwindow.h

  1. #ifndef MAINWINDOW_H
  2. #define MAINWINDOW_H
  3. #include <QMainWindow>
  4. class MainWindow : public QMainWindow
  5. {
  6. Q_OBJECT
  7. public:
  8. MainWindow(QWidget *parent = 0);
  9. ~MainWindow();
  10. };
  11. #endif // MAINWINDOW_H

myWindow.pro

  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2018-03-11T19:52:19
  4. #
  5. #-------------------------------------------------
  6. QT += core gui
  7. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
  8. TARGET = myWindow
  9. TEMPLATE = app
  10. # The following define makes your compiler emit warnings if you use
  11. # any feature of Qt which has been marked as deprecated (the exact warnings
  12. # depend on your compiler). Please consult the documentation of the
  13. # deprecated API in order to know how to port your code away from it.
  14. DEFINES += QT_DEPRECATED_WARNINGS
  15. # You can also make your code fail to compile if you use deprecated APIs.
  16. # In order to do so, uncomment the following line.
  17. # You can also select to disable deprecated APIs only up to a certain version of Qt.
  18. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
  19. QT += sql #added because I found this to try and solve my problem, doesn't fix
  20. SOURCES += \
  21. main.cpp \
  22. mainwindow.cpp
  23. HEADERS += \
  24. mainwindow.h


我的Qt版本5.10.1,操作系统是Windows,编译器是MinGW 64。

i5desfxk

i5desfxk1#

好吧,我自己修好了,所以这是我搞砸的两件事:
1.扩展QWidget而不是QMainWindow
(很简单)
1.检查我的工具包,并使用一个正确版本的MinGW -不是64或32位,它被称为完全不同的东西,但仍然有MinGW的名称;)

mqxuamgl

mqxuamgl2#

修复是通过安装您正在使用的软件包的适当版本来完成的。一些新的代码技术可能是一个难题,因此安装正确类型的软件包可能很难,但有时是必需的。

6ss1mwsb

6ss1mwsb3#

我最近偶然发现了这个问题,因为我花了很长时间才找到解决方案,所以我决定在这里添加这个答案。
在我的例子中,undefinded reference to __imp_是一个指示器,表示我的编译和链接命令中缺少链接选项
要解决这个问题(使用gcc编译器):添加-L(显示要链接的库所在的文件夹)和-l(形式为-llibraryname)。
例如,如果project位于C:/myproject中,并且其中有一个文件夹'libs'。

  1. gcc -LC:/myproject/libs -o executable_file.exe source_file.c -llibusb0 -lstcommdll

字符串
我假设有其他编译器的等价物。

相关问题