Qt creator ошибка lnk2019

I’m getting these three errors and I have no idea why? I am using the Qt program and and the code below goes in a Qt widget project. The mainwindow.ui is not included because I can’t change it anyways. The goal as of now is to get the age into a lineEdit as shown in the mainwindow.cpp file.

I have done things like uninstalled and reinstalled Qt program. Copy and pasted code into a brand new project. Added the header files to cpp files etc etc

Errors:

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall person::~person(void)" (??1person@@QAE@XZ) referenced in function "public: virtual __thiscall MainWindow::~MainWindow(void)" (??1MainWindow@@UAE@XZ)

and

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: __thiscall person::person(void)" (??0person@@QAE@XZ) referenced in function "public: __thiscall MainWindow::MainWindow(class QWidget *)" (??0MainWindow@@QAE@PAVQWidget@@@Z)

and

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "public: void __thiscall person::setAge(int)" (?setAge@person@@QAEXH@Z) referenced in function "private: void __thiscall MainWindow::on_lineEdit_returnPressed(void)" (?on_lineEdit_returnPressed@MainWindow@@AAEXXZ)

Here is the code:

mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <string>
#include "person.h"

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

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

private slots:
    void on_lineEdit_returnPressed();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

person.h

#ifndef PERSON_H
#define PERSON_H


class person
{
public:

    int age;

    person();
    person(int);

    ~person();

    int getAge();

    void setAge(int age);
};

#endif // PERSON_H

main.cpp

#include "mainwindow.h"
#include "person.h"
#include <QApplication>
#include <string>

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

    return a.exec();
}

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "person.h"
#include <string>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_lineEdit_returnPressed()
{
    QString str = ui->lineEdit->text();
    std::string age = str.toStdString();
    x.setAge(stoi(age));
    ui->lineEdit_2->setText(str);
}

person.cpp

#include "person.h"
using namespace std;

person::person(int age)
{
    age = 0;
}

person::~person()
{

}

int getAge()
{
    return age;
}

void setAge(int age)
{ 
    age = newAge;
}

I have searched and searched and all I know is for some reason the program can’t find the setAge() method or cant find the person class. I’ve even asked other people who know c++ and have no idea why this is happening. Please help!

Deleting:

person x; //in the mainwindow.h file solves this problem but...

I get the error:

C:\Users\Adam\Documents\Body_fat\mainwindow.cpp:22: error: C3861: 'setAge': identifier not found



void MainWindow::on_lineEdit_returnPressed()
{
    QString str = ui->lineEdit->text();
    std::string age = str.toStdString();
    setAge(stoi(age));
    ui->lineEdit_2->setText(str);
}

I know this question has been asked a lot of times already but I’m sorry, I just can’t figure out what is wrong with what I’ve been doing.

What I want is to read a DICOM image (by pixel because I have to perform operations on it) and display it on a Qt GUI.

I have Windows 8 and Visual Studio 2013. I downloaded Qt opensource and also VS’s plugin for it. I built Qt using cmake-gui. Then, I followed what was said on a discussion here in stackoverflow entitled «How to use DCMTK in Qt». I downloaded DCMTK 3.6.0, configured and generated it using cmake-gui, built its ALL_BUILD and INSTALL projects in VS. So far, everything is successful. Then I tried creating a simple program that will read a DICOM image and display it.

I created a new Qt Application and named it MainWindow. Here is my mainwindow.cpp:

#include "mainwindow.h"
#include <dcmtk\config\osconfig.h>
#include <dcmtk\ofstd\ofcond.h>
#include <dcmtk\ofstd\ofstring.h>
#include <dcmtk\dcmdata\dctk.h>
#include <dcmtk\dcmdata\dcfilefo.h>
#include <dcmtk\dcmdata\dcitem.h>
#include <dcmtk\dcmdata\dcdeftag.h>
#include <dcmtk\dcmdata\dctagkey.h>

void MainWindow::tryDCMTK() {
    DcmFileFormat fileformat;
    OFCondition status = fileformat.loadFile("C:/Users/Kriselle/Documents/000004.dcm");
    if (status.good())
    {
        OFString patientsName;
        if (fileformat.getDataset()->findAndGetOFString(DCM_PatientName, patientsName).good())
        {
            printf("Patient's Name: %s",patientsName);
        }
        else
            printf("Error: cannot access Patient's Name!");
    }
    else
        printf("Error: cannot read DICOM file (%s)", status.text());
}

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) {
    ui.setupUi(this);
}

MainWindow::~MainWindow() {}

mainwindow.h:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QtWidgets/QMainWindow>
#include "ui_mainwindow.h"

class MainWindow : public QMainWindow {
    Q_OBJECT

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

private:
    Ui::MainWindowClass ui;
    void tryDCMTK();
};

#endif // MAINWINDOW_H

MainWindow.pro

# ----------------------------------------------------
# This file is generated by the Qt Visual Studio Add-in.
# ------------------------------------------------------

TEMPLATE = app
TARGET = MainWindow
DESTDIR = ../Win32/Debug
QT += core widgets gui
CONFIG += debug console
DEFINES += WIN64 QT_DLL QT_WIDGETS_LIB _REENTRANT
INCLUDEPATH += ./GeneratedFiles \
    . \
    ./GeneratedFiles/Debug \
    "C:/Program Files (x86)/DCMTK/include"
DEPENDPATH += .
MOC_DIR += ./GeneratedFiles/debug
OBJECTS_DIR += debug
UI_DIR += ./GeneratedFiles
RCC_DIR += ./GeneratedFiles
HEADERS += ../../../../../../../DICOMSDL/include/dicom.h \
    ./dicomcfg.h \
    ./mainwindow.h
SOURCES += ./main.cpp \
    ./mainwindow.cpp
FORMS += ./mainwindow.ui
RESOURCES += mainwindow.qrc

QMAKE_CFLAGS_RELEASE -= -MD
QMAKE_CFLAGS_RELEASE = -MT
QMAKE_CFLAGS_DEBUG -= -MDd
QMAKE_CFLAGS_DEBUG = -MTd
QMAKE_CXXFLAGS_RELEASE -= -MD
QMAKE_CXXFLAGS_RELEASE += -MT
QMAKE_CXXFLAGS_DEBUG -= -MDd
QMAKE_CXXFLAGS_DEBUG += -MTd

#a example: LIBS += -L"../../../test_dcmtk/DCMTK/lib" \
LIBS += -L"C:/Program Files (x86)/DCMTK/lib" \
-lconfig \
-lofstd \
-ldcmdata \
-loflog \
-lws2_32 \
-lnetapi32 \
-lwsock32 \
-ladvapi32 

When I tried to run it, the following 7 LNK2019 errors occurred:

1>mainwindow.obj : error LNK2019: unresolved external symbol "public: __cdecl OFString::OFString(void)" (??0OFString@@QEAA@XZ) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK@MainWindow@@AEAAXXZ)
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: __cdecl OFString::~OFString(void)" (??1OFString@@QEAA@XZ) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK@MainWindow@@AEAAXXZ)
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: class OFCondition __cdecl DcmItem::findAndGetOFString(class DcmTagKey const &,class OFString &,unsigned long,bool)" (?findAndGetOFString@DcmItem@@QEAA?AVOFCondition@@AEBVDcmTagKey@@AEAVOFString@@K_N@Z) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK@MainWindow@@AEAAXXZ)
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: __cdecl DcmFileFormat::DcmFileFormat(void)" (??0DcmFileFormat@@QEAA@XZ) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK@MainWindow@@AEAAXXZ)
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: virtual __cdecl DcmFileFormat::~DcmFileFormat(void)" (??1DcmFileFormat@@UEAA@XZ) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK@MainWindow@@AEAAXXZ)
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: class DcmDataset * __cdecl DcmFileFormat::getDataset(void)" (?getDataset@DcmFileFormat@@QEAAPEAVDcmDataset@@XZ) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK@MainWindow@@AEAAXXZ)
1>mainwindow.obj : error LNK2019: unresolved external symbol "public: virtual class OFCondition __cdecl DcmFileFormat::loadFile(char const *,enum E_TransferSyntax,enum E_GrpLenEncoding,unsigned long,enum E_FileReadMode)" (?loadFile@DcmFileFormat@@UEAA?AVOFCondition@@PEBDW4E_TransferSyntax@@W4E_GrpLenEncoding@@KW4E_FileReadMode@@@Z) referenced in function "private: void __cdecl MainWindow::tryDCMTK(void)" (?tryDCMTK@MainWindow@@AEAAXXZ)
1>C:\Users\Kriselle\documents\visual studio 2013\Projects\MainWindow\x64\Debug\\MainWindow.exe : fatal error LNK1120: 7 unresolved externals

I have looked into other discussions:
*Link 1** said that the libraries must be linked in properties->linker->additional library dependencies. I already did that. Screenshots of my additional library dependencies and environment variables are available in the zip file attached below.

*Link 2** has a different error output. (I was already past that).
*Link 3** said «look for linker flags/settings inside a project configuration dialog» but I don’t know what’s wrong with my linker settings. I even listed the libraries inside the lib directory in my Additional Dependencies. (A screenshot of my Additional Dependencies is also included in the zip file below.)
I have also done what was suggested at *Link 4**.
Furthermore, I believe I didn’t commit the same mistake as displayed in *Link 5** because I have no parameters for my function.

The images are here: https://db.tt/CmpJndan
The links of online discussions I have looked into are here: https://db.tt/AOsewqUg

As much as I would like to make it easier for you to see the images and navigate to the links, I am very sorry, I can only post a maximum of two links due to my lack of reputation.

Please help me. Thank you very much for your time!

EDIT: I changed the order of my libraries according to their dependencies and added NetAPI32.lib and WSock32.lib to my libraries thanks to the link Hans gave in the comments. But I still get the same errors.

@SGaist I don’t know what’s wrong with it.

The mistake is easy to reproduce.

Two custom designer widgets,examples:

The First custom designer widget code as follows:
[ FirstCustomWidget.pro]
CONFIG += plugin release
TARGET = $$qtLibraryTarget(firstcustomwidgetplugin)
TEMPLATE = lib

HEADERS = firstcustomwidgetplugin.h
SOURCES = firstcustomwidgetplugin.cpp
RESOURCES = icons.qrc
LIBS += -L.

greaterThan(QT_MAJOR_VERSION, 4) {
QT += designer
} else {
CONFIG += designer
}

target.path = $$[QT_INSTALL_PLUGINS]/designer
INSTALLS += target

include(firstcustomwidget.pri)

lib_out_path = $${PWD}/../../LnkTester/build/lib
include_out_path = $${PWD}/../../LnkTester/build/include

$$[QMAKE_SPEC] {
CONFIG(release, debug|release) {
QMAKE_POST_LINK += copy /y release\.dll «$${target.path}» &
QMAKE_POST_LINK += copy /y release\
.dll «$${lib_out_path}» &
QMAKE_POST_LINK += copy /y release\*.lib «$${lib_out_path}»
}
}

[FirstCustomWidget.h]
#ifndef FIRSTCUSTOMWIDGET_H
#define FIRSTCUSTOMWIDGET_H

#include <QWidget>
#include <QLabel>
#include <QPixmap>
#include <QtUiPlugin/QDesignerExportWidget>

class QDESIGNER_WIDGET_EXPORT FirstCustomWidget : public QWidget
{
Q_OBJECT
public:
FirstCustomWidget(QWidget * parent = 0);
private:
QLabel * _label;
};
#endif

[ FirstCustomWidget.cpp]
#include «firstcustomwidget.h»

FirstCustomWidget::FirstCustomWidget(QWidget * parent) :
QWidget(parent), _label(new QLabel(this))
{
}

[firstcustomwidgetplugin.h] [firstcustomwidgetplugin.cpp]
These two files are generic

The Second custom designer widget code as follows:
[SecondCustomWidget.pro]
CONFIG += plugin release
TARGET = $$qtLibraryTarget(secondcustomwidgetplugin)
TEMPLATE = lib

HEADERS = secondcustomwidgetplugin.h
SOURCES = secondcustomwidgetplugin.cpp
RESOURCES = icons.qrc
LIBS += -L.

greaterThan(QT_MAJOR_VERSION, 4) {
QT += designer
} else {
CONFIG += designer
}

target.path = $$[QT_INSTALL_PLUGINS]/designer
INSTALLS += target

include(secondcustomwidget.pri)

lib_out_path = $${PWD}/../../LnkTester/build/lib

INCLUDEPATH += $${PWD}/../../LnkTester/FirstCustomWidget

$$[QMAKE_SPEC] {
CONFIG(release, debug|release) {
LIBS += -L$${lib_out_path} -lfirstcustomwidgetplugin
}
}

[secondcustomwidget.h]
#ifndef SECONDCUSTOMWIDGET_H
#define SECONDCUSTOMWIDGET_H

#include <QWidget>
#include <QtUiPlugin/QDesignerExportWidget>
#include «firstcustomwidget.h»

class QDESIGNER_WIDGET_EXPORT SecondCustomWidget : public QWidget
{
Q_OBJECT
public:
SecondCustomWidget(QWidget * parent = 0);
~SecondCustomWidget();
private:
FirstCustomWidget *_cwidget;
};

#endif

[secondcustomwidget.cpp]
#include «secondcustomwidget.h»

SecondCustomWidget::SecondCustomWidget(QWidget *parent) :
QWidget(parent),_cwidget(new FirstCustomWidget(this))
{
}

SecondCustomWidget::~SecondCustomWidget()
{
delete _cwidget;
}

[secondcustomwidgetplugin.h] [secondcustomwidgetplugin.cpp]
These two files are generic

the lnk error is as follows:
Generating Code…
link /NOLOGO /DYNAMICBASE /NXCOMPAT /INCREMENTAL:NO /DLL /SUBSYSTEM:WINDOWS /OUT:release\secondcustomwidgetplugin.dll @E:\TEMP\nm4C1A.tmp
Creating library release\secondcustomwidgetplugin.lib and object release\secondcustomwidgetplugin.exp
secondcustomwidgetplugin.obj : error LNK2019: unresolved external symbol «public: static struct QMetaObject const FirstCustomWidget::staticMetaObject» (?staticMetaObject@FirstCustomWidget@@2UQMetaObject@@B) referenced in function «public: static class QString __cdecl FirstCustomWidget::tr(char const *,char const *,int)» (?tr@FirstCustomWidget@@SA?AVQString@@PBD0H@Z)
secondcustomwidget.obj : error LNK2001: unresolved external symbol «public: static struct QMetaObject const FirstCustomWidget::staticMetaObject» (?staticMetaObject@FirstCustomWidget@@2UQMetaObject@@B)
moc_secondcustomwidget.obj : error LNK2001: unresolved external symbol «public: static struct QMetaObject const FirstCustomWidget::staticMetaObject» (?staticMetaObject@FirstCustomWidget@@2UQMetaObject@@B)
release\secondcustomwidgetplugin.dll : fatal error LNK1120: 1 unresolved externals

Здравствуйте, уважаемые программисты C++. Программирую игру на Qt. Собирался было уже делать управление игроком, но тут ошибка : LNK2019. Вот сама ошибка: player.obj:-1: error: LNK2019: « __imp_GetAsyncKeyState «public: void __cdecl Player::moving(void)» (?moving@Player@@QEAAXXZ). Пробовал найти решение, там говорят что нужно перезапустить qmake, но не помогает. Вот код игры:

Player.h:

#pragma once

#include <QObject>
#include <QGraphicsItem>
#include <QTimer>
#include <QPixmap>
#include <QPainter>
#include <QLabel>



class Player: public QObject, public QGraphicsItem
{
    Q_OBJECT

public:
    Player(QObject *parent = 0);
    int pWidth = 110;
    int pHeigth = 110;
    int x = 205;
    int y = 400;


public slots:
    void moving();

private:
    void paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget) override;
    QRectF boundingRect() const override;
    QPixmap  *spriteImage; // В данный объект pixmap будет помещен спрайт



};

View.h:

#pragma once

#include <QGraphicsView>
#include <QGraphicsScene>
#include <QObject>

#include "player.h"

class View :public QGraphicsView
{
public:
    View();
    void view_elements();
    void gameStarted();


private:
    QGraphicsScene mainScene;
    Player pl;
    QTimer     *timer; // Таймер


};

main.cpp:

#include "view.h"
#include <QtWidgets>

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

    View v;
    v.show();

    return a.exec();
}

View.cpp:

#include "view.h"
#include "player.h"

#include <QObject>
#include <QTimer>

View::View()
{

    setWindowTitle("Space Rangers");

    setRenderHint(QPainter::Antialiasing);

    setFrameStyle(0);
    setSceneRect(0, 0, 500,600);
    setFixedSize(500, 600);

    mainScene.setSceneRect(0, 0, 500,600);
    mainScene.setItemIndexMethod(QGraphicsScene::NoIndex);
    setScene(&mainScene);

    timer = new QTimer();
    connect(timer, &QTimer::timeout, &pl, &Player::moving);
    view_elements();


}

void View::view_elements(){

    pl.setPos(205, 400);
    mainScene.addItem(&pl);


}

Player.cpp:

#include "player.h"

#include <QtWidgets>

using namespace Qt;

Player::Player(QObject *parent):QObject(parent), QGraphicsItem()
{
    spriteImage = new QPixmap();
    spriteImage->load(":/Player.png");

}

QRectF Player::boundingRect() const{

    return QRectF(0, 0, pWidth, pHeigth);
}

void Player::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget){

    painter->drawPixmap(0, 0, pWidth, pHeigth, *spriteImage);
    Q_UNUSED(option);
    Q_UNUSED(widget);

}

void Player::moving()
{
//*******************Обработка нажатий клавиш на клавиатуре*********************************************

    if(GetAsyncKeyState(VK_LEFT)){
        setPos(mapToParent(-4, 0));
    }

    if(GetAsyncKeyState(VK_RIGHT)){
        setPos(mapToParent(4, 0));
    }

    if(GetAsyncKeyState(VK_UP)){
        setPos(mapToParent(0, 5));
    }

    if(GetAsyncKeyState(VK_DOWN)){
        setPos(mapToParent(0, -4));
    }
}

Буду очень благодарен за ответ.


C++, QT dev


1 Minute

If you’re developing a QT based application(which links QT libs as statically) and get LNK2019unresolved external symbol’ link errors when you try to link the other static library,

Then you probably start looking those link settings such as,

☐ whether the architecture for the binary is correct(Are you trying to link an x86 lib to the x64 binary?)

☐ the static library file name specified to Input property was correct(Configuration Properties > Linker > Input > Additional Dependencies)

☐ they both use the same platform toolset or CRT lib(MT or MD)

☐ ‘dumpbin /headers’ for looking into export symbols

☐ or looking for some name mangling exists which might have caused the issue

However, if you’re still getting the LNK2019 unresolved link errors and it started frustrating although tried every setting you could, then you really should check the following setting as well.

The QT project seems to have the ’Treat WChar_t As Built in Type’ setting as ‘Noby default(see the attachment screen capture). However, Visual C++ static library project uses the option as Yes unless you haven’t changed it intentionally. Well, for me, the link error was resolved when I changed the option to Yes from the main QT application project settings.

qt_setting_1

Set to ‘Yes’ for the QT application to link a static library. Of course, it will be different in case you use different ‘Treat WChar_t As Built in Type’ setting  for the static library.

qt_setting_2

 source: http://stackoverflow.com/questions/6987006/linking-to-a-c-static-library-from-a-qt-application

Published by Heejune


View all posts by Heejune

Published

Понравилась статья? Поделить с друзьями:
  • Qt creator настройки android содержат ошибки
  • Python ошибка импорта модуля
  • Python ошибка dataframe object is not callable
  • Qsx15 cummins коды ошибок
  • Python ошибка времени исполнения