FAQ #47: How do I use the latest (snapshot) DCMTK libraries in my own application? [MSVC]

Frequently Asked Questions regarding DCMTK

Moderator: FAQ Team

Post Reply
Message
Author
Michael Onken
DCMTK Developer
Posts: 1813
Joined: Fri, 2004-11-05, 13:47
Location: Oldenburg, Germany
Contact:

FAQ #47: How do I use the latest (snapshot) DCMTK libraries in my own application? [MSVC]

#1 Post by Michael Onken » Fri, 2010-09-17, 11:14

:?: Howto use DCMTK snapshots in my own applications?

:!: Here's an example for the Windows platform (MSVC):
  1. Create DCMTK project files with CMake (for demonstration purposes switch all external libraries off).
  2. Open the main project file "dcmtk.dsw" or "dcmtk.sln" in Visual Studio.
  3. Select target ALL_BUILD and compile the complete DCMTK.
  4. Select target INSTALL in order to install the DCMTK files to the directory specified using CMake.
  5. Create a new directory "testapp" somewhere on your harddisk with the following files.
Example program "testapp.cxx" from the dcmdata documentation:

Code: Select all

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

int main()
{
  DcmFileFormat fileformat;
  OFCondition status = fileformat.loadFile("test.dcm");
  if (status.good())
  {
    OFString patientName;
    if (fileformat.getDataset()->findAndGetOFString(DCM_PatientName, patientName).good())
    {
      cout << "Patient's Name: " << patientName << endl;
    } else
      cerr << "Error: cannot access Patient's Name!" << endl;
  } else
    cerr << "Error: cannot read DICOM file (" << status.text() << ")" << endl;
  return 0;
}
Add minimum CMakeLists.txt file for Visual Studio and adjust path to installed DCMTK (if required). The following sample looks a bit huge compared to the 3.5.4 example in FAQ #40, however, most of the text just configures for different versions of Visual Studio. The file was generated by selecting all compile flags relevant for windows from the CMakeLists.txt file that resides in DCMTK's root directory.

Code: Select all

PROJECT(testapp)

# Minimum CMake version required
CMAKE_MINIMUM_REQUIRED(VERSION 2.4)

SET(DCMTK_DIR /Programme/DCMTK_20100916_PUBLIC_NOLIB_MTd)

# Declare project
PROJECT(testapp)

#-----------------------------------------------------------------------------
# Platform-specific settings
#-----------------------------------------------------------------------------

# set project wide flags for compiler and linker

# settings for Microsoft Visual C
IF(CMAKE_C_COMPILER MATCHES "CL|cl")
  IF(NOT WITH_SHARED_LIBS)
    IF(CMAKE_GENERATOR STREQUAL "Visual Studio 6")
      SET(CMAKE_C_FLAGS "/nologo /W3 /GX /Gy /YX")
      SET(CMAKE_C_FLAGS_DEBUG "/MTd /Z7 /Od")
      SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
      SET(CMAKE_C_FLAGS_MINSIZEREL "/MT /O2")
      SET(CMAKE_C_FLAGS_RELWITHDEBINFO "/MTd /Z7 /Od")
    ENDIF(CMAKE_GENERATOR STREQUAL "Visual Studio 6")
    IF(CMAKE_GENERATOR STREQUAL "Visual Studio 7" OR CMAKE_GENERATOR STREQUAL "Visual Studio 7 .NET 2003")
      SET(CMAKE_C_FLAGS "/nologo /W3 /Gy")
      SET(CMAKE_C_FLAGS_DEBUG "/MTd /Z7 /Od")
      SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
      SET(CMAKE_C_FLAGS_MINSIZEREL "/MT /O2")
      SET(CMAKE_C_FLAGS_RELWITHDEBINFO "/MTd /Z7 /Od")
    ENDIF(CMAKE_GENERATOR STREQUAL "Visual Studio 7" OR CMAKE_GENERATOR STREQUAL "Visual Studio 7 .NET 2003")
    IF(CMAKE_GENERATOR STREQUAL "Visual Studio 8 2005" OR CMAKE_GENERATOR STREQUAL "Visual Studio 9 2008" OR CMAKE_GENERATOR STREQUAL "Visual Studio 10")
      SET(CMAKE_C_FLAGS "/nologo /W3 /Gy /EHsc")
      SET(CMAKE_C_FLAGS_DEBUG "/MTd /Z7 /Od")
      SET(CMAKE_C_FLAGS_RELEASE "/MT /O2")
      SET(CMAKE_C_FLAGS_MINSIZEREL "/MT /O2")
      SET(CMAKE_C_FLAGS_RELWITHDEBINFO "/MTd /Z7 /Od")
    ENDIF(CMAKE_GENERATOR STREQUAL "Visual Studio 8 2005" OR CMAKE_GENERATOR STREQUAL "Visual Studio 9 2008" OR CMAKE_GENERATOR STREQUAL "Visual Studio 10")
  ENDIF(NOT WITH_SHARED_LIBS)
ENDIF(CMAKE_C_COMPILER MATCHES "CL|cl")

# settings for Microsoft Visual C++
IF(CMAKE_CXX_COMPILER MATCHES "CL|cl")
  IF(NOT WITH_SHARED_LIBS)
    IF(CMAKE_GENERATOR STREQUAL "Visual Studio 6")
       SET(CMAKE_CXX_FLAGS "/nologo /W3 /GX /Gy /YX /Zm500") # /Zm500 increments heap size which is needed on some system to compile templates in dcmimgle
      SET(CMAKE_CXX_FLAGS_DEBUG "/MTd /Z7 /Od")
      SET(CMAKE_CXX_FLAGS_RELEASE "/MT /O2")
      SET(CMAKE_CXX_FLAGS_MINSIZEREL "/MT /O2")
      SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MTd /Z7 /Od")
    ENDIF(CMAKE_GENERATOR STREQUAL "Visual Studio 6")
    IF(CMAKE_GENERATOR STREQUAL "Visual Studio 7" OR CMAKE_GENERATOR STREQUAL "Visual Studio 7 .NET 2003")
      SET(CMAKE_CXX_FLAGS "/nologo /W3 /Gy")
      SET(CMAKE_CXX_FLAGS_DEBUG "/MTd /Z7 /Od")
      SET(CMAKE_CXX_FLAGS_RELEASE "/MT /O2")
      SET(CMAKE_CXX_FLAGS_MINSIZEREL "/MT /O2")
      SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MTd /Z7 /Od")
    ENDIF(CMAKE_GENERATOR STREQUAL "Visual Studio 7" OR CMAKE_GENERATOR STREQUAL "Visual Studio 7 .NET 2003")
    IF(CMAKE_GENERATOR STREQUAL "Visual Studio 8 2005" OR CMAKE_GENERATOR STREQUAL "Visual Studio 9 2008" OR CMAKE_GENERATOR STREQUAL "Visual Studio 10")
      SET(CMAKE_CXX_FLAGS "/nologo /W3 /Gy /EHsc")
      SET(CMAKE_CXX_FLAGS_DEBUG "/MTd /Z7 /Od")
      SET(CMAKE_CXX_FLAGS_RELEASE "/MT /O2")
      SET(CMAKE_CXX_FLAGS_MINSIZEREL "/MT /O2")
      SET(CMAKE_CXX_FLAGS_RELWITHDEBINFO "/MTd /Z7 /Od")
    ENDIF(CMAKE_GENERATOR STREQUAL "Visual Studio 8 2005" OR CMAKE_GENERATOR STREQUAL "Visual Studio 9 2008" OR CMAKE_GENERATOR STREQUAL "Visual Studio 10")
  ENDIF(NOT WITH_SHARED_LIBS)
ENDIF(CMAKE_CXX_COMPILER MATCHES "CL|cl")

ADD_DEFINITIONS(-D_REENTRANT) 

# directories to search for libraries
LINK_DIRECTORIES(${DCMTK_DIR}/lib)

# Specify the executable to be built
ADD_EXECUTABLE(testapp testapp)

# define include directories to be searched
INCLUDE_DIRECTORIES(${DCMTK_DIR}/include)

# define libraries that must be linked to most windows applications
TARGET_LINK_LIBRARIES(testapp dcmdata oflog ofstd ws2_32 netapi32 wsock32)

Finally, create the project files for the test application using CMake and compile it using Microsoft Visual Studio.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest