Compiling DCMTK with C++11/14/17 Features with MSVC

Compilation and installation of DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
nbeck
Posts: 12
Joined: Fri, 2022-10-28, 12:56

Compiling DCMTK with C++11/14/17 Features with MSVC

#1 Post by nbeck »

Hello,
I managed to compile the toolkit with Visual Studio 2022. However, I noticed that CMake disabled the features of newer C++ versions:

Code: Select all

Checking whether the compiler supports C++11
Checking whether the compiler supports C++11 -- no
Info: C++11 features disabled
Info: C++14 features disabled
Info: C++17 features disabled
I tried setting the variable CMAKE_CXX_STANDARD = 17 as mentioned in the INSTALL document, but that didn't seem to change anything. Is this a problem with the Visual Studio compiler or am I missing something?

nbeck
Posts: 12
Joined: Fri, 2022-10-28, 12:56

Re: Compiling DCMTK with C++11/14/17 Features with MSVC

#2 Post by nbeck »

After some digging I found that the problem occurs in dcmtk\config\tests\cxx11.cc. The code checks for the macro __cplusplus:

Code: Select all

#elif __cplusplus < 201103L

#error "This is not a C++11 compiler"
Unfortunately, MSVC doesn't set this macro correctly. From https://learn.microsoft.com/en-us/cpp/b ... w=msvc-170:
... Because a lot of existing code appears to depend on the value of this macro matching 199711L, the compiler doesn't change the value of the macro unless you explicitly opt in by using the /Zc:__cplusplus compiler option.

I changed line 1453 in the function DCMTK_CHECK_CXX_STANDARD in dcmtk\CMake\GenerateDCMTKConfigure.cmake to

Code: Select all

try_compile(COMPILE_RESULT "${CMAKE_BINARY_DIR}" "${DCMTK_SOURCE_DIR}/config/tests/cxx${STANDARD}.cc" COMPILE_DEFINITIONS  "/Zc:__cplusplus")
CMake now enables the C++ features:

Code: Select all

Info: C++11 features enabled
Info: C++14 features enabled
Info: C++17 features enabled
I also added the following to dcmtk\CMakeLists.txt

Code: Select all

set(CMAKE_CXX_STANDARD 17)
if(MSVC)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /Zc:__cplusplus")
endif()
However, the resulting VS solution is configured to use the C++14 standard, and I had to add /Zc:__cplusplus to the compiler flags for each project.

Is there a way to tell CMake to generate the solution with this compiler flag?

nbeck
Posts: 12
Joined: Fri, 2022-10-28, 12:56

Re: Compiling DCMTK with C++11/14/17 Features with MSVC

#3 Post by nbeck »

I noticed that you have created a ticket for this issue: https://support.dcmtk.org/redmine/issues/1047.

Thanks for your work!

Michael Onken
DCMTK Developer
Posts: 2072
Joined: Fri, 2004-11-05, 13:47
Location: Oldenburg, Germany
Contact:

Re: Compiling DCMTK with C++11/14/17 Features with MSVC

#4 Post by Michael Onken »

Hi,

this has been fixed with commit 8f7de4df9 which should show up on public DCMTK master branch within a few days (after successfully passing various nightly builds).
Thank you for your report and suggested fix :)

Best regards,
Michael

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests