Search code examples
lnk2019dcmtk

How to solve error LNK2019 by myself in Visual Studio series


During my study in dcmtk, I come across hundreds of error "LNK2019". Everytime I could only google it, searching for someone telling me which lib should I add. I wonder if I could find the lib I need by myself.

My work IDE is Visual Studio 2015, and today's code is below.

#include <dcmtk\config\osconfig.h>
#include <dcmtk\dcmdata\dcdatset.h>
#include <dcmtk\dcmdata\dctk.h>
#include <dcmtk\dcmjpeg\djcodecd.h>
#include <dcmtk\dcmjpls\djdecode.h>
#include <dcmtk\dcmjpeg\djdecode.h>



int main() {
    DJDecoderRegistration::registerCodecs(); // register JPEG codecs
    DcmFileFormat fileformat;
    if (fileformat.loadFile("1").good())
    {
        DcmDataset *dataset = fileformat.getDataset();
        // decompress data set if compressed
        dataset->chooseRepresentation(EXS_LittleEndianExplicit, NULL);
        // check if everything went well
        if (dataset->canWriteXfer(EXS_LittleEndianExplicit))
        {
            fileformat.saveFile("2", EXS_LittleEndianExplicit);
        }
    }
    DJDecoderRegistration::cleanup(); // deregister JPEG codecs
    return 0;
}

error in this code this is my additional Linker

Accutually this problem has been solved in confusion, I just add plenty of additional lib in code.

#pragma comment(lib,"ofstd")
#pragma comment(lib,"dcmdata")
#pragma comment(lib,"dcmtls")
#pragma comment(lib,"dcmnet")
#pragma comment(lib,"dcmqrdb")
#pragma comment(lib,"dcmimgle")
#pragma comment(lib,"dcmimage")
#pragma comment(lib,"dcmjpeg")
#pragma comment(lib,"ijg8")
#pragma comment(lib,"ijg12")
#pragma comment(lib,"ijg16")
#pragma comment(lib,"dcmdsig")
#pragma comment(lib,"dcmsr")
#pragma comment(lib,"dcmpstat")
#pragma comment(lib,"dcmwlm")
#pragma comment(lib,"netapi32")
#pragma comment(lib,"wsock32")

I once find someone in stackoverflow proposed a Method to solve this. However his method is using the bash or something else that Visual Studio can't do. So that's why I am here for help. thanks for anyone could come to help!!


Solution

  • Depending on which version of the DCMTK you use and which of the DCMTK modules your program includes, you have to link the required DCMTK libraries. In your case, "ofstd, oflog, dcmdata, dcmimgle, dcmimage, dcmjpeg, ijg8, ijg12, ijg16" would be required.

    For DCMTK 3.6.0, there is a diagram showing the dependencies graphically: https://support.dcmtk.org/redmine/projects/dcmtk/wiki/Modules

    Also FAQ #26 might be helpful: https://forum.dcmtk.org/viewtopic.php?f=4&t=36

    For newer DCMTK versions (e.g. 3.6.2 or 3.6.3), I would rather recommend to use the CMake Export feature. After installing the DCMTK into a separate directory, the following CMakeLists.txt file could be used for your sample program:

    # DCMTK Demo Project
    
    # declare project
    PROJECT(DCMTK_DEMO)
    
    # minimum CMake version required
    CMAKE_MINIMUM_REQUIRED(VERSION 2.8.3)
    
    # specify DCMTK's (default) installation directory
    SET(DCMTK_DIRECTORY "C:/Users/dicom/dcmtk-3.6.3-install" CACHE PATH "Directory where DCMTK library is installed.")
    
    # approach #1: use FIND_PACKAGE() to search for installed DCMTK
    #FIND_PACKAGE(DCMTK REQUIRED CONFIG PATHS ${DCMTK_DIRECTORY} NO_DEFAULT_PATH)
    
    # approach #2: include DCMTK's CMake configuration directly
    INCLUDE(${DCMTK_DIRECTORY}/cmake/DCMTKConfig.cmake)
    
    # declare include directories
    INCLUDE_DIRECTORIES(${DCMTK_INCLUDE_DIRS})
    
    # declare executable and link required libraries
    ADD_EXECUTABLE(demo demo.cc)
    TARGET_LINK_LIBRARIES(demo ${DCMTK_LIBRARIES})