C++ 未解析的外部符号 - Qt creator

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14069041/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:56:58  来源:igfitidea点击:

Unresolved external symbols - Qt creator

c++qtunresolved-external

提问by chuckieDub

I must be missing a basic concept with headers and includes because when I attempt to call even the simplest of a function from a separate source file I get an error:

我一定缺少带有标题和包含的基本概念,因为当我尝试从单独的源文件调用即使是最简单的函数时,也会出现错误:

main.obj:-1: error: LNK2019: unresolved external symbol "void __cdecl buildDeck(int,int)" (?buildDeck@@YAXHH@Z) referenced in function _main

main.obj:-1: 错误: LNK2019: 未解析的外部符号“void __cdecl buildDeck(int,int)” (?buildDeck@@YAXHH@Z) 在函数 _main 中引用

deck.h

甲板.h

#ifndef DECK_H
#define DECK_H
#include <QString>


void buildDeck(int deckSize, int jokers);


struct card
{
    QString suit;
    QString color;
    int rank;
};

#endif // DECK_H

deck.cpp

甲板.cpp

#include"mainwindow.h"
#include "deck.h"

void buildDeck(int deckSize, int jokers)
{
    int blackRed = deckSize-=jokers;
}

main.cpp

主程序

#include "mainwindow.h"
#include "deck.h"
#include <QApplication>
#include <QPushButton>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    buildDeck(10,20);

    return a.exec();
}

And this gives me an error. However, If I move the function definition from deck.cpp to the bottom of main.cpp, then the application will build.

这给了我一个错误。但是,如果我将函数定义从deck.cpp 移动到main.cpp 的底部,则应用程序将构建。

All of the files are included in the same project, and stored in the same directory.

所有文件都包含在同一个项目中,并存储在同一个目录中。

Other files:

其它文件:

.pro file

。轮廓

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = carddeck
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp \
    deck.cpp

HEADERS  += mainwindow.h \
    deck.h

FORMS    += mainwindow.ui

not sure if you need it, but here's mainwindow.h

不确定你是否需要它,但这里是 mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QPushButton>
#include <QTextEdit>
#include <QCheckBox>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void runButtonClicked();

private:
    Ui::MainWindow *ui;
    QPushButton *runButton;
    QTextEdit * runText;

    QCheckBox * betHearts;
    QCheckBox * betDiamonds ;
    QCheckBox * betClubs;
    QCheckBox * betSpades ;
    QCheckBox * betFlush ;
    QCheckBox * betAllRed;
    QCheckBox * betAllBlack ;

};

#endif // MAINWINDOW_H

回答by benjarobin

It looks like the Makefile was not regenerated when you edited the .pro file. Run qmake and try again. You could also check if the deck.cpp is compiled or not; is there a deck.o in the build directory ?

当您编辑 .pro 文件时,看起来 Makefile 没有重新生成。运行 qmake 并重试。您还可以检查deck.cpp 是否已编译;构建目录中是否有deck.o?

回答by Kartik Maheshwari

yes, some time Makefile file is not updated while you change .pro file. So you have to run qmake.

是的,有时更改 .pro 文件时 Makefile 文件不会更新。所以你必须运行qmake。

Follow this steps:

请按照以下步骤操作:

  • Right click on project Clean / Build menu -> cleanAll
  • Right click on project Run qmake / Build menu -> runQmake
  • Right click on project Build / Build menu -> build
  • 右键单击项目 Clean / Build 菜单 -> cleanAll
  • 右键单击项目 Run qmake / Build 菜单 -> runQmake
  • 右键单击项目构建/构建菜单 -> 构建

Pro Advice:

专业建议:

I don't know but sometime Qt is not updating Makefile. so i recomanded to all whenever you add/removing any resource in project or if any changes occur in your .pro file, just Run qmake and build your project(Running qmake do manually to update the path of project, which help to find the mainwindow.obj file).

我不知道,但有时 Qt 不会更新 Makefile。因此,每当您添加/删除项目中的任何资源或 .pro 文件中发生任何更改时,我都会向所有人推荐,只需运行 qmake 并构建您的项目(手动运行 qmake 来更新项目路径,这有助于找到主窗口.obj 文件)。