首页 > 解决方案 > 如何在 ubuntu 中运行 gdcm 示例?

问题描述

我正在尝试在 GDCM 中运行这个简单的示例。我已经安装了库 c++ 版本并且安装工作非常好,但我无法弄清楚如何编译和运行示例。

#include "gdcmReader.h"
#include "gdcmWriter.h"
#include "gdcmAttribute.h"

#include <iostream>

int main(int argc, char *argv[])
{
  if( argc < 3 )
    {
    std::cerr << argv[0] << " input.dcm output.dcm" << std::endl;
    return 1;
    }
  const char *filename = argv[1];
  const char *outfilename = argv[2];

  // Instanciate the reader:
  gdcm::Reader reader;
  reader.SetFileName( filename );
  if( !reader.Read() )
    {
    std::cerr << "Could not read: " << filename << std::endl;
    return 1;
    }

  // If we reach here, we know for sure only 1 thing:
  // It is a valid DICOM file (potentially an old ACR-NEMA 1.0/2.0 file)
  // (Maybe, it's NOT a Dicom image -could be a DICOMDIR, a RTSTRUCT, etc-)

  // The output of gdcm::Reader is a gdcm::File
  gdcm::File &file = reader.GetFile();

  // the dataset is the the set of element we are interested in:
  gdcm::DataSet &ds = file.GetDataSet();

  // Contruct a static(*) type for Image Comments :
  gdcm::Attribute<0x0020,0x4000> imagecomments;
  imagecomments.SetValue( "Hello, World !" );

  // Now replace the Image Comments from the dataset with our:
  ds.Replace( imagecomments.GetAsDataElement() );

  // Write the modified DataSet back to disk
  gdcm::Writer writer;
  writer.CheckFileMetaInformationOff(); // Do not attempt to reconstruct the file meta to preserve the file
                                        // as close to the original as possible.
  writer.SetFileName( outfilename );
  writer.SetFile( file );
  if( !writer.Write() )
    {
    std::cerr << "Could not write: " << outfilename << std::endl;
    return 1;
    }

  return 0;
}

/*
 * (*) static type, means that extra DICOM information VR & VM are computed at compilation time.
 * The compiler is deducing those values from the template arguments of the class.
 */

它有一些它正在寻找的头文件,即 gdcmreader、gdcmwriter,我想找出用来运行这个文件的编译器标志。
我正在做 g++ a.cpp -lgdcmCommon -lgdcmDICT ,但这给了我错误

a.cpp:18:24: fatal error: gdcmReader.h: No such file or directory
compilation terminated.

你能帮帮我吗?我到处搜索,但我似乎无法弄清楚如何运行这个文件。

标签: c++compilationdicomgdcm

解决方案


当使用位于“正常”文件不同位置的文件时,您必须指示编译器和链接器如何找到它们。

您的代码有一个#include <someFile.h>命令。 用法表示“在其他路径中”
<>编译器已经知道常见的“其他路径”,就像常见库的“stdio”一样。
在“不正常”的情况下,您可以通过添加到命令行来告诉 g++ 在哪里找到标头-Imydir(将“mydir”替换为正确的路径)

对于库,静态 (.a) 或动态 (.so) 具有相同的历史记录。
告诉 g++在-Lmydir哪里寻找库。

您的命令行可能看起来像

g++ a.cpp -I/usr/include -L/usr/local/lib -lgdcmCommon -lgdcmDICT

推荐阅读