How to resolve LNK2019 error in Visual Studio when

2019-08-07 01:11发布

问题:

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.