Tiny fix for compilation with newer gcc versions

Compilation and installation of DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
marlam
Posts: 2
Joined: Wed, 2010-06-16, 06:10

Tiny fix for compilation with newer gcc versions

#1 Post by marlam »

Hi!

To compile dcmtk-3.5.4 with newer gcc versions (I tried 4.5.0) for MinGW, the following tiny fix is necessary:

Code: Select all

--- dcmtk-3.5.4.orig/ofstd/libsrc/ofthread.cc	2005-12-08 16:49:02.000000000 +0100
+++ dcmtk-3.5.4/ofstd/libsrc/ofthread.cc	2010-06-15 21:34:29.234201660 +0200
@@ -139,7 +139,7 @@
   theThreadHandle = _beginthreadex(NULL, 0, thread_stub, (void *)this, 0, &tid);
   if (theThreadHandle == 0) return errno; else
   {
-    theThread = tid;
+    theThread = (void *)tid;
     return 0;
   }
 #elif defined(POSIX_INTERFACE)
@@ -195,7 +195,7 @@
 #endif
 {
 #ifdef WINDOWS_INTERFACE
-  if (theThread == tID) return OFTrue; else return OFFalse;
+  if (theThread == (void *)tID) return OFTrue; else return OFFalse;
 #elif defined(POSIX_INTERFACE)
 #ifdef HAVE_POINTER_TYPE_PTHREAD_T
   // dangerous - we cast an unsigned long back to a pointer type and hope that it is still valid
This patch merely makes an implicit cast explicit.

This is the same issue reported in this post from february:
viewtopic.php?t=2421

This issue is not yet fixed in the latest snapshot version.

Please consider applying the patch.

Best regards,
and thanks for your work

Martin

Jörg Riesmeier
ICSMED DICOM Services
ICSMED DICOM Services
Posts: 2217
Joined: Fri, 2004-10-29, 21:38
Location: Oldenburg, Germany

#2 Post by Jörg Riesmeier »

Thank you for the report. However, we would propose the following patch (making sure that "theThread" is really of type "void *").

Code: Select all

--- libsrc/ofthread.cc  4 Jun 2010 14:18:20 -0000       1.19
+++ libsrc/ofthread.cc  18 Jun 2010 07:44:30 -0000
@@ -1,6 +1,6 @@
 /*
  *
- *  Copyright (C) 1997-2010, OFFIS
+ *  Copyright (C) 2000-2010, OFFIS
  *
  *  This software and supporting documentation were developed by
  *
@@ -137,17 +137,21 @@ int OFThread::start()
 {
 #ifdef WINDOWS_INTERFACE
   unsigned int tid = 0;
-  theThreadHandle = _beginthreadex(NULL, 0, thread_stub, (void *)this, 0, &tid);
+  theThreadHandle = _beginthreadex(NULL, 0, thread_stub, OFstatic_cast(void *, this), 0, &tid);
   if (theThreadHandle == 0) return errno; else
   {
+#ifdef HAVE_POINTER_TYPE_PTHREAD_T
+    theThread = OFstatic_cast(void *, tid);
+#else
     theThread = tid;
+#endif
     return 0;
   }
 #elif defined(POSIX_INTERFACE)
   pthread_t tid=0;
   int result = pthread_create(&tid, NULL, thread_stub, OFstatic_cast(void *, this));
 #ifdef HAVE_POINTER_TYPE_PTHREAD_T
-  if (0 == result) theThread = tid; else theThread = 0;
+  if (0 == result) theThread = OFstatic_cast(void *, tid); else theThread = 0;
 #else
   if (0 == result) theThread = OFstatic_cast(unsigned long, tid); else theThread = 0;
 #endif
@@ -196,7 +200,11 @@ OFBool OFThread::equal(unsigned long /*
 #endif
 {
 #ifdef WINDOWS_INTERFACE
+#ifdef HAVE_POINTER_TYPE_PTHREAD_T
+  if (theThread == OFstatic_cast(void *, tID)) return OFTrue; else return OFFalse;
+#else
   if (theThread == tID) return OFTrue; else return OFFalse;
+#endif
 #elif defined(POSIX_INTERFACE)
 #ifdef HAVE_POINTER_TYPE_PTHREAD_T
   // dangerous - we cast an unsigned long back to a pointer type and hope that it is still valid
@@ -416,7 +424,7 @@ OFSemaphore::OFSemaphore(unsigned int /*
 : theSemaphore(NULL)
 {
 #ifdef WINDOWS_INTERFACE
-  theSemaphore = (void *)(CreateSemaphore(NULL, numResources, numResources, NULL));
+  theSemaphore = OFstatic_cast(void *, CreateSemaphore(NULL, numResources, numResources, NULL));
 #elif defined(POSIX_INTERFACE)
   sem_t *sem = new sem_t;
   if (sem)
@@ -553,7 +561,7 @@ OFMutex::OFMutex()
 : theMutex(NULL)
 {
 #ifdef WINDOWS_INTERFACE
-  theMutex = (void *)(CreateMutex(NULL, FALSE, NULL));
+  theMutex = OFstatic_cast(void *, CreateMutex(NULL, FALSE, NULL));
 #elif defined(POSIX_INTERFACE)
   pthread_mutex_t *mtx = new pthread_mutex_t;
   if (mtx)
I've tested the above patch (based on the current DCMTK snapshot) on a Linux system with gcc 4.1 and under Windows with VS 2008. Could you please test it with your MinGW system and let us know whether it also works. Thanks in advance!

marlam
Posts: 2
Joined: Wed, 2010-06-16, 06:10

#3 Post by marlam »

With the latest snapshort plus your patch, I get the following error:

Code: Select all

ofthread.cc: In member function 'int OFThread::start()':
ofthread.cc:144:17: error: invalid static_cast from type 'unsigned int' to type 'void*'
ofthread.cc: In member function 'bool OFThread::equal(long unsigned int)':
ofthread.cc:204:20: error: invalid static_cast from type 'long unsigned int' to type 'void*'
I think reinterpret_cast instead of static_cast is required here, if you want to avoid traditional casts.

The gcc version I used is 4.5.0.

Martin

PS: Sorry for the delay, I forgot to check "notify me when a reply is posted"...

Jörg Riesmeier
ICSMED DICOM Services
ICSMED DICOM Services
Posts: 2217
Joined: Fri, 2004-10-29, 21:38
Location: Oldenburg, Germany

#4 Post by Jörg Riesmeier »

Martin, thank you for the feedback. We've modified the patch accordingly and also updated the git repository.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests