cmake 致命错误:文件/配置/操作系统配置h:无此文件或目录

smdncfj3  于 2023-01-26  发布在  其他
关注(0)|答案(2)|浏览(314)

作为一个CMake新手,我非常想让DCMTK做一些事情。我正在编译和运行的例子如下:

#include "dcmtk/config/osconfig.h"
#include "dcmtk/dcmdata/dctk.h"
#include "dcmtk/dcmdata/dcistrmf.h"

int main(int argc, char *argv[])
{
    OFCondition status;

    /* approach 1 */
    DcmMetaInfo metainfo;
    status = metainfo.loadFile("test.dcm");
    if (status.good())
    {
        OFString sopClassUID, xferUID;
        if (metainfo.findAndGetOFString(DCM_MediaStorageSOPClassUID, sopClassUID).good())
            COUT << "SOP Class UID: " << sopClassUID << OFendl;
        if (metainfo.findAndGetOFString(DCM_TransferSyntaxUID, xferUID).good())
            COUT << "Transfer Syntax UID: " << xferUID << OFendl;
        metainfo.print(COUT);
    }

    /* approach 2 */
    DcmFileFormat fileformat;
    status = fileformat.loadFile("test.dcm", EXS_Unknown, EGL_noChange, DCM_MaxReadLength, ERM_metaOnly);
    if (status.good())
    {
        OFString sopClassUID, xferUID;
        if (fileformat.getMetaInfo()->findAndGetOFString(DCM_MediaStorageSOPClassUID, sopClassUID).good())
            COUT << "SOP Class UID: " << sopClassUID << OFendl;
        if (fileformat.getMetaInfo()->findAndGetOFString(DCM_TransferSyntaxUID, xferUID).good())
            COUT << "Transfer Syntax UID: " << xferUID << OFendl;
        fileformat.print(COUT);
    }

    /* approach 3 */
    fileformat.clear();
    DcmInputFileStream fileStream("test.dcm");
    status = fileStream.status();
    if (status.good())
    {
        /* first, read meta-header */
        fileformat.setReadMode(ERM_metaOnly);
        fileformat.transferInit();
        status = fileformat.read(fileStream);
        if (status.good())
        {
            OFString sopClassUID, xferUID;
            if (fileformat.getMetaInfo()->findAndGetOFString(DCM_MediaStorageSOPClassUID, sopClassUID).good())
                COUT << "SOP Class UID: " << sopClassUID << OFendl;
            if (fileformat.getMetaInfo()->findAndGetOFString(DCM_TransferSyntaxUID, xferUID).good())
                COUT << "Transfer Syntax UID: " << xferUID << OFendl;
            /* then read dataset if certain criteria are met */
            if (sopClassUID == UID_SecondaryCaptureImageStorage)
            {
                fileformat.setReadMode(ERM_autoDetect);
                status = fileformat.read(fileStream);
                if (status.good())
                {
                    fileformat.print(COUT);
                }
            }
        }
        fileformat.transferEnd();
    }

    return 0;
}

问题出在这个文件的上半部分。我知道,在一个特定的文件夹中,这段代码将有一个构建错误,因为它在项目目录中找不到dcmtk文件夹。我已经在我的驱动器上的其他地方的不同文件夹中构建和安装了DCMTK。因此,我有两个文件夹:dcmtk-3.6.5-builddcmtk-3.6.5-install。我知道我感兴趣的是dcmtk-3.6.5-install/include/。但在构建时,我得到以下错误:

====================[ Build | test_dcmtk | Debug ]==============================
/snap/clion/139/bin/cmake/linux/bin/cmake --build /home/project_dir/test_dcmtk/cmake-build-debug --target test_dcmtk -- -j 38
Scanning dependencies of target test_dcmtk
[ 50%] Building CXX object CMakeFiles/test_dcmtk.dir/main.cpp.o
/home/project_dir/test_dcmtk/main.cpp:1:10: fatal error: dcmtk/config/osconfig.h: No such file or directory
    1 | #include "dcmtk/config/osconfig.h"
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[3]: *** [CMakeFiles/test_dcmtk.dir/build.make:83: CMakeFiles/test_dcmtk.dir/main.cpp.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:96: CMakeFiles/test_dcmtk.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:103: CMakeFiles/test_dcmtk.dir/rule] Error 2
make: *** [Makefile:138: test_dcmtk] Error 2

据我所知,我应该将编译器定向到DCMTK的安装目录,但是当我这样做时,我得到了一个非常相似的错误:

====================[ Build | test_dcmtk | Debug ]==============================
/snap/clion/139/bin/cmake/linux/bin/cmake --build /home/project_dir/test_dcmtk/cmake-build-debug --target test_dcmtk -- -j 38
Scanning dependencies of target test_dcmtk
[ 50%] Building CXX object CMakeFiles/test_dcmtk.dir/main.cpp.o
In file included from /home/project_dir/test_dcmtk/main.cpp:2:
/home/dcmtk-3.6.5-install/include/dcmtk/dcmdata/dctk.h:25:10: fatal error: dcmtk/config/osconfig.h: No such file or directory
   25 | #include "dcmtk/config/osconfig.h"    /* make sure OS specific configuration is included first */
      |          ^~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[3]: *** [CMakeFiles/test_dcmtk.dir/build.make:83: CMakeFiles/test_dcmtk.dir/main.cpp.o] Error 1
make[2]: *** [CMakeFiles/Makefile2:96: CMakeFiles/test_dcmtk.dir/all] Error 2
make[1]: *** [CMakeFiles/Makefile2:103: CMakeFiles/test_dcmtk.dir/rule] Error 2
make: *** [Makefile:138: test_dcmtk] Error 2

这两个错误的区别在于编译器正在寻找的头文件。在第二种情况下,编译器似乎不知道osconfig.h在哪里。由于这涉及到DCMTK安装中的文件,我的理解是它们应该按原样工作。这让我想到这个问题应该用CMake来解决。但是我完全不知道如何在我的CMakeLists.txt中正确地制作thinghs。我的意思是说这个文件包含了

cmake_minimum_required(VERSION 3.16)
project(test_dcmtk)

set(CMAKE_CXX_STANDARD 20)

add_executable(test_dcmtk main.cpp)

我完全不知道该写些什么。有人能帮帮我吗?
先谢谢你!

zzwlnbp8

zzwlnbp81#

很遗憾DCMTK没有提供正确导出的CMake目标,因此您必须进行修补。我通过vcpkg安装了DCMTK,并能够使用此版本编译您的代码(在将COUT替换为std::cout并包含iostream之后):

cmake_minimum_required(VERSION 3.19)
project(test_dcmtk)

set(CMAKE_CXX_STANDARD 20)

find_package(DCMTK REQUIRED)

# Wrap the variables that DCMTK sets with a proper imported target
# These three lines ought not to be needed.
# This is a failure on the part of the DCMTK authors.
add_library(DCMTK::DCMTK INTERFACE IMPORTED)
target_link_libraries(DCMTK::DCMTK INTERFACE ${DCMTK_LIBRARIES})
target_include_directories(DCMTK::DCMTK INTERFACE ${DCMTK_INCLUDE_DIRS})

# Create your executable and link it to the library
add_executable(test_dcmtk main.cpp)
target_link_libraries(test_dcmtk PRIVATE DCMTK::DCMTK)

您应该向DCMTK库的作者抱怨他们没有提供正确的导入目标。

n8ghc7c1

n8ghc7c12#

您正在使用MSVC编译吗?请尝试改用Clang编译。

相关问题