如何在 ubuntu 中运行 gdcm 示例?

How to run gdcm examples in ubuntu?

本文关键字:gdcm 示例 运行 ubuntu      更新时间:2023-10-16

我正在尝试在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.

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

使用位于"普通"文件不同位置的文件时,必须指示编译器和链接器如何查找它们。

您的代码具有#include <someFile.h>命令。
<>用法的意思是"在其他路径中"。编译器已经知道公共库的"stdio"等常见的"其他路径"。
在"不正常"的情况下,您可以通过在命令行中添加-Imydir来告诉 g++ 在哪里可以找到标头(将"mydir"替换为正确的路径(

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

您的命令行可能如下所示

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

你没有告诉你你是如何安装 gdcm 库的,我假设使用apt系统。有两种类型的库,"普通"和"开发人员"库。为了能够编译自己的软件,您需要后者。因此,例如在 Ubuntu 16.04 中,键入apt-get install libgdcm2-dev.然后所有必要的标头都将安装在/usr/include/gdcm-2.6中。