How did OFFIS build libxml2?

Compilation and installation of DCMTK

Moderator: Moderator Team

Post Reply
Message
Author
zzzhhh
Posts: 12
Joined: Tue, 2018-07-03, 11:08

How did OFFIS build libxml2?

#1 Post by zzzhhh » Wed, 2018-07-04, 05:42

I am trying to build libxml2 myself and integrate it into building DCMTK, but encountered errors.

Environment:
- Windows 10 version 1803
- Visual Studio 2015 Update 3
- CMake 3.11.4

Building steps and configurations:
- I downloaded the source of libxml2 "libxml2-2.9.8.tar.gz" from ftp://xmlsoft.org/libxml2/ and uncompress it.
- Open Visual Studio 2015 -> Visual Studio Tools -> Developer Command Prompt,run:

Code: Select all

cd C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC
vcvarsall x64
cd C:\...\libxml2-2.9.8\win32
cscript configure.js zlib=yes compiler=msvc prefix=C:\...\libxml2-2.9.8\install_ include=C:\...\zlib-1.2.11\install\include;C:\...\iconv-1.14-win32-x86_64\include lib=C:\...\3rd-parties\zlib-1.2.11\install\lib;C:\...\iconv-1.14-win32-x86_64\lib
nmake /f Makefile.msvc
nmake /f Makefile.msvc install
- In normal command line window, type:

Code: Select all

cd C:\...\dcmtk-3.6.3\build
cmake -DCMAKE_CONFIGURATION_TYPES=Release -DBUILD_SHARED_LIBS=ON -DCMAKE_INSTALL_PREFIX=C:\...\dcmtk-3.6.3\install_ -DDCMTK_ENABLE_STL=ON -DDCMTK_ENABLE_CXX11:BOOL=ON -DDCMTK_WITH_DOXYGEN=1 -DDCMTK_WITH_PNG=ON -DWITH_LIBPNGINC=C:\...\lpng1632\install_ -DDCMTK_WITH_TIFF=ON -DWITH_LIBTIFFINC=C:\...\libtiff-master\install -DDCMTK_WITH_ZLIB=ON -DWITH_ZLIBINC=C:\...\zlib-1.2.11\install -DDCMTK_WITH_ICONV=ON -DWITH_LIBICONVINC=C:\...\iconv-1.14-win32-x86_64 -DDCMTK_WITH_XML=ON -DWITH_LIBXMLINC=C:\...\libxml2-2.9.8\install_ -DDCMTK_WITH_OPENSSL=ON -DWITH_OPENSSLINC=C:\OpenSSL-Win64 -G"Visual Studio 14 2015 Win64" ..
- Open DCMTK.sln and build xml2dcm

Errors:
1>------ Build started: Project: xml2dcm, Configuration: Release x64 ------
1>xml2dcm.obj : error LNK2019: unresolved external symbol xmlFree referenced in function "class OFCondition __cdecl createNewElement(struct _xmlNode *,class DcmElement * &)" (?createNewElement@@YA?AVOFCondition@@PEAU_xmlNode@@AEAPEAVDcmElement@@@Z)
1>C:\...\dcmtk-3.6.3\build\bin\Release\xml2dcm.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 4 up-to-date, 0 skipped ==========
This error is caused by the invocation of xmlFree in line 213 of dcmdata\apps\xml2dcm.cc. I checked the libxml2 library I built using dumpbin, xmlFree is in it. Although I omitted my own paths, it should not cause the error because if a lib file could not be openned, VS would only report "can not open ....lib". Also, I think xmlFree is not the only xml function called by DCMTK, but only xmlFree is unresolved. If I use xml2 library in the provided pre-compile binary "dcmtk-3.6.3-win64-support_MD-msvc-14.0.zip", DCMTK builds well. So I don't know what is wrong with my building of xml2. Can you help me troubleshoot? Or, if it is allowed, could you please tell me how OFFIS built libxml2 for Windows 10 + VS2015 + /MD? Please let me know if you need other info to reproduce the problem. Thanks a lot.

Jan Schlamelcher
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 318
Joined: Mon, 2014-03-03, 09:51
Location: Oldenburg, Germany

Re: How did OFFIS build libxml2?

#2 Post by Jan Schlamelcher » Wed, 2018-07-04, 10:05

Our third party libs build script (that I still want to make available officially after finishing cleanup and documentation) does it the following way:

The configure step is done this way:

Code: Select all

execute_process(COMMAND "cscript" "configure.js" "compiler=msvc" "prefix=${PREFIX}" "include=${INCLUDE}" "lib=${LIB}" "cruntime=@CRUNTIME@" "debug=@DEBUG@"
    WORKING_DIRECTORY "${SOURCE_DIR}/win32"
)
With:

Code: Select all

#     ``INCLUDE``
#       as provided via the ``<INCLUDE>`` argument, additional include
#       directories (i.e. for including libiconv).
#
#     ``LIB``
#       as provided via the ``<LIB>`` argument, additional linker search paths
#       (i.e. for linking libiconv).
#
#     ``CRUNTIME``
#       as provided via the ``<CRUNTIME>`` argument, the Visual Studio runtime
#       option to use, e.g. ``/MD`` or ``/MT``.
#
#     ``DEBUG``
#       as provided via the ``<DEBUG>`` argument, whether to create a debug
#       build or not. Possible values are ``yes`` and ``no``.
Then we effectively do:

Code: Select all

nmake "win32" /f Makefile.msvc
nmake "win32" /f Makefile.msvc install
So essentially, it seems you need the cruntime option.

zzzhhh
Posts: 12
Joined: Tue, 2018-07-03, 11:08

Re: How did OFFIS build libxml2?

#3 Post by zzzhhh » Wed, 2018-07-04, 11:35

According to the help info of configure.js (cscript configure.js help),
Win32 build options, default value given in parentheses:

compiler: Compiler to be used [msvc|mingw|bcb] (msvc)
cruntime: C-runtime compiler option (only msvc) (/MD)
dynruntime: Use the dynamic RTL (only bcb) (true)
cruntime is by default /MD, which is exactly what I plan to set, so I did not include this option. However, I added it to build xml2 again to make sure it is not the culprit. Another difference is that I added zlib support. I removed it too to rebuild xml2, so our nmake command are now the same:

Code: Select all

cscript configure.js compiler=msvc cruntime=/MD prefix=C:\...\libxml2-2.9.8\install_ include=C:\...\iconv-1.14-win32-x86_64\include lib=C:\...\iconv-1.14-win32-x86_64\lib
, and the output of nmake configuration confirmed that I am using /MD:
Win32 build configuration
-------------------------
Compiler: msvc
C-Runtime option: /MD
Embed Manifest: no
Debug symbols: no
Static xmllint: no
I checked with dumpbin that the output library libxml2-2.9.8\install_\lib\libxml2.lib is x64.
Then, with the same DCMTK cmake command, I got the same error in Visual Studio 2015. As I said in the original post, only xmlFree is unresolved, so if /MD caused the error, all functions in xml2 called by DCMTK would have caused the same error. Now, what else can I check?

PS: OFFIS did not build xlm2 with any 3rd party libraries like zlib or lzma, does it?

Jan Schlamelcher
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 318
Joined: Mon, 2014-03-03, 09:51
Location: Oldenburg, Germany

Re: How did OFFIS build libxml2?

#4 Post by Jan Schlamelcher » Wed, 2018-07-04, 13:01

No, we only include the iconv library in the libxml2.

zzzhhh
Posts: 12
Joined: Tue, 2018-07-03, 11:08

Re: How did OFFIS build libxml2?

#5 Post by zzzhhh » Thu, 2018-07-05, 05:55

Jan Schlamelcher wrote:No, we only include the iconv library in the libxml2.
I have removed zlib and lzma from building xml2 library, but still got the same linking error. Would you please help me troubleshoot the problem? You can contact me via email if you cannot reproduce the error on your side.

Jan Schlamelcher
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 318
Joined: Mon, 2014-03-03, 09:51
Location: Oldenburg, Germany

Re: How did OFFIS build libxml2?

#6 Post by Jan Schlamelcher » Thu, 2018-07-05, 11:34

We have a Visual Studio 2015 x64 build every night, and it does not show this issue. We configure it via:

Code: Select all

C:/Program Files/CMake/bin/cmake.exe -DCMAKE_BUILD_TYPE:string=RELEASE -DWITH_TESTING:BOOL=ON --no-warn-unused-cli -DBUILD_SHARED_LIBS:BOOL=ON -G "NMake Makefiles" -DDCMTK_WITH_ICONV:BOOL=ON -DWITH_LIBICONVINC:PATH=[...]/libiconv-1.15 -DDCMTK_WITH_PNG:BOOL=ON -DWITH_LIBPNGINC:PATH=[...]/libpng-1.6.34 -DDCMTK_WITH_TIFF:BOOL=ON -DWITH_LIBTIFFINC:PATH=[...]/libtiff-4.0.9 -DDCMTK_WITH_XML:BOOL=ON -DWITH_LIBXMLINC:PATH=[...]/libxml2-2.9.7 -DDCMTK_WITH_OPENSSL:BOOL=ON -DWITH_OPENSSLINC:PATH=[...]/openssl-1.1.0g -DDCMTK_WITH_ZLIB:BOOL=ON -DWITH_ZLIBINC:PATH=[...]/zlib-1.2.11 -DDCMTK_WITH_SNDFILE:BOOL=OFF -DDCMTK_WITH_DOXYGEN:BOOL=OFF "C:/nightly/dcmtk-Visual-Studio-2015-_19.00.24215.1_-x64-_Shared-Libs_"

zzzhhh
Posts: 12
Joined: Tue, 2018-07-03, 11:08

Re: How did OFFIS build libxml2?

#7 Post by zzzhhh » Thu, 2018-07-05, 13:11

1) I am building every night too.
2) I am using the same xml2 building script too.
3) I am using the same DCMTK building script too (except I am using Visual Studio IDE instead of nmake cli).
4) The only difference is that I have error while you don't have, magically.

I know you are as busy as I am (again, too), so I don't mean to bother you. I paid tens of thousands of dollars to my university every semester but can learn nothing useful from it. The faculties of my department pretend to be CS experts by talking about and using Linux only, but become idiots immediately and brush off my questions in an arrogant way when I ask questions about Windows. So I have no choice but to ask you for help for the troubleshooting. I won't hate if you won't help because you don't have to support Windows like those faculties, but I would greatly appreciate your help if you could give me some direction on how to troubleshoot problems like this. Thank you.

Jan Schlamelcher
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 318
Joined: Mon, 2014-03-03, 09:51
Location: Oldenburg, Germany

Re: How did OFFIS build libxml2?

#8 Post by Jan Schlamelcher » Thu, 2018-07-05, 13:49

This is strange. You might try using nmake together with the appropriate vcvars file, since it is the only thing different, but I don't really believe that this would be the issue. I am pretty much out of ideas though.

zzzhhh
Posts: 12
Joined: Tue, 2018-07-03, 11:08

Re: How did OFFIS build libxml2?

#9 Post by zzzhhh » Sat, 2018-07-07, 12:07

It turns out that the culprit is the preprocessor definition LIBXML_STATIC.

Image

I am trying to link to libxml2_o.lib which is the import library of a DLL, but LIBXML_STATIC forces the linker to use this import library as a static library, thus causing the linking errors. If I change the libxml2_o.lib to a true xml2 static library, or remove LIBXML_STATIC from preprocessor definitions, DCMTK builds successfully. But I want to link to DLL version of xml2, so I have to temporarily #undef LIBXML_STATIC in C:\...\libxml2-2.9.8\install_\include\libxml2\libxml\xmlexports.h (or C:\...\libxml2-2.9.8\include\libxml\xmlexports.h) because there are hundreds of projects in DCMTK solution. Now my problem reduces to:

1) How was the preprocessor definition LIBXML_STATIC introduced into DCMTK Visual Studio solution?
I searched the whole source package of DCMTK but couldn't find any appearance of this string.

2) How to let CMake not define LIBXML_STATIC in command line when setting up DCMTK Visual Studio solution?
Obviously this depends on an answer to 1).

Looking forward to an answer. Thank you.

Jan Schlamelcher
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 318
Joined: Mon, 2014-03-03, 09:51
Location: Oldenburg, Germany

Re: How did OFFIS build libxml2?

#10 Post by Jan Schlamelcher » Mon, 2018-07-09, 11:12

I see, now that is crazy. It seems to be my fault. Not sure why this leads to a problem for you, but not for us, though.

zzzhhh
Posts: 12
Joined: Tue, 2018-07-03, 11:08

Re: How did OFFIS build libxml2?

#11 Post by zzzhhh » Tue, 2018-07-10, 08:11

Jan Schlamelcher wrote:I see, now that is crazy. It seems to be my fault. Not sure why this leads to a problem for you, but not for us, though.
DCMTK defines LIBXML_STATIC somehow which forcibly requires linking to static library of xml2, so when linking to dll, there is an error. The pre-compiled binaries of xml2 in dcmtk-3.6.3-win64-support_MD-msvc-14.0.zip are all static, no matter Debug or Release. You will reproduce the same error on your side if you link import libraries to Dlls by removing the LIBXML_STATIC preprocessor definition. But why can't a user link dll version of xml2? Isn't this your problem?

Jan Schlamelcher
OFFIS DICOM Team
OFFIS DICOM Team
Posts: 318
Joined: Mon, 2014-03-03, 09:51
Location: Oldenburg, Germany

Re: How did OFFIS build libxml2?

#12 Post by Jan Schlamelcher » Tue, 2018-07-10, 11:35

As you see in the commit message of the commit I linked, I added the flag to suppress some warnings and seemingly also tested it with linking the DDLs, but using the .lib files that refer to the DLLs and not the DLLs directly for linking. I can retest that, perhaps it is necessary to detect whether the user tries to link the DLL version and then not add the compiler flag. As a workaround simply remove the line from the code that you can see in the commit.

Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 1 guest