Visual Studio系列如何自己解决错误LNK2019

How to solve error LNK2019 by myself in Visual Studio series

在学习dcmtk的过程中,我遇到了数百个错误"LNK2019"。每次我只能 google 它,搜索有人告诉我应该添加哪个库。不知道能不能自己找到需要的lib

我的工作IDE是Visual Studio2015年的,今天的代码如下

#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

其实这个问题已经解决了,我只是在代码中添加了很多额外的库。

#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")

曾经在Whosebug上发现有人提出了解决这个问题的方法。然而,他的方法是使用 bash 或 Visual Studio 不能做的其他事情。所以这就是我来这里寻求帮助的原因。感谢任何人的帮助!!

根据您使用的 DCMTK 版本以及您的程序包含哪些 DCMTK 模块,您必须 link 所需的 DCMTK 库。在您的情况下,需要 "ofstd, oflog, dcmdata, dcmimgle, dcmimage, dcmjpeg, ijg8, ijg12, ijg16"。

对于 DCMTK 3.6.0,有一个图表以图形方式显示依赖关系:https://support.dcmtk.org/redmine/projects/dcmtk/wiki/Modules

FAQ #26 也可能有帮助:https://forum.dcmtk.org/viewtopic.php?f=4&t=36

对于较新的 DCMTK 版本(例如 3.6.2 或 3.6.3),我宁愿推荐使用 CMake Export 功能。将 DCMTK 安装到单独的目录后,以下 CMakeLists.txt 文件可用于您的示例程序:

# 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})