Tuesday, July 7, 2015

static Libxml++ hello_world in visual studio

So, how to build application, which uses libxml++.

For this moment I have directory 'D:\projects\libraries\libxmlplusplus' where libxml++ and all dependencies:
  • glib
  • glibmm
  • intl
  • libffi-3.2.1
  • libsigc++-2.4.1
  • libxml++-2.38.1
  • libxml2
  • pcre-8.37
  • zlib-1.2.8
So, I just made two property sheets - libxmlplusplus_x64_debug.props and libxmlplusplus_x64_release.props, and put them also into this directory.

libxmlplusplus_x64_debug.props:
Additional include directories:
    D:\projects\libraries\libxmlplusplus\libxml2\_build_x64_static_debug_mtd\include\libxml2
    D:\projects\libraries\libxmlplusplus\libxml++-2.38.1
    D:\projects\libraries\libxmlplusplus\glibmm\glib
    D:\projects\libraries\libxmlplusplus\glib\glib
    D:\projects\libraries\libxmlplusplus\glib
    D:\projects\libraries\libxmlplusplus\libxml++-2.38.1\MSVC_Net2010\libxml++
Additional library directories:
    D:\projects\libraries\libxmlplusplus\glibmm\MSVC_Net2010\Debug\x64\bin
    D:\projects\libraries\libxmlplusplus\libxml2\_build_x64_static_debug_mtd\lib
    D:\projects\libraries\libxmlplusplus\glib\build\win32\vs10\Debug\x64\bin
    D:\projects\libraries\libxmlplusplus\libffi-3.2.1\_build_x64_static_debug_mtd\.libs
    D:\projects\libraries\libxmlplusplus\libsigc++-2.4.1\MSVC_Net2010\x64\Debug
    D:\projects\libraries\libxmlplusplus\pcre-8.37\_build_x64_static_debug_mtd\Debug
    D:\projects\libraries\libxmlplusplus\intl\_build_x64_static_debug_mtd\lib
    D:\projects\libraries\libxmlplusplus\libxml++-2.38.1\MSVC_Net2010\libxml++\x64\Debug
input:
    glibmm.lib
    libxml2_a.lib
    ws2_32.lib
    glib.lib
    gobject.lib
    libffi_convenience.lib
    winmm.lib
    sigc-vc100-d-2_0.lib
    pcred.lib;intl.lib
    xml++-vc100-d-2_6.lib

libxmlplusplus_x64_release.props:
Additional include directories:
    D:\projects\libraries\libxmlplusplus\libxml2\_build_x64_static_release_mt\include\libxml2
    D:\projects\libraries\libxmlplusplus\libxml++-2.38.1
    D:\projects\libraries\libxmlplusplus\glibmm\glib
    D:\projects\libraries\libxmlplusplus\glib\glib
    D:\projects\libraries\libxmlplusplus\glib
    D:\projects\libraries\libxmlplusplus\libxml++-2.38.1\MSVC_Net2010\libxml++
Additional library directories:
    D:\projects\libraries\libxmlplusplus\glibmm\MSVC_Net2010\Release\x64\bin
    D:\projects\libraries\libxmlplusplus\libxml2\_build_x64_static_release_mt\lib
    D:\projects\libraries\libxmlplusplus\glib\build\win32\vs10\Release\x64\bin
    D:\projects\libraries\libxmlplusplus\libffi-3.2.1\_build_x64_static_release_mt\.libs
    D:\projects\libraries\libxmlplusplus\libsigc++-2.4.1\MSVC_Net2010\x64\Release
    D:\projects\libraries\libxmlplusplus\pcre-8.37\_build_x64_static_release_mt\Release
    D:\projects\libraries\libxmlplusplus\intl\_build_x64_static_release_mt\lib
    D:\projects\libraries\libxmlplusplus\libxml++-2.38.1\MSVC_Net2010\libxml++\x64\Release
input:
    glibmm.lib
    libxml2_a.lib
    ws2_32.lib
    glib.lib
    gobject.lib
    libffi_convenience.lib
    winmm.lib
    sigc-vc100-2_0.lib
    pcre.lib
    intl.lib
    xml++-vc100-2_6.lib


To check if everything works I've took sources from this article, cut off curl stuff and got something like that:

#include <libxml/tree.h>
#include <libxml/HTMLparser.h>
#include <libxml++/libxml++.h>

#include <iostream>
#include <string>

int main() {
   
    // Collect response
    std::string re = "bla bla fucking bla";

    // Parse HTML and create a DOM tree
    xmlDoc* doc = htmlReadDoc((xmlChar*)re.c_str(), NULL, NULL, HTML_PARSE_RECOVER | HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING);

    // Encapsulate raw libxml document in a libxml++ wrapper
    xmlNode* r = xmlDocGetRootElement(doc);
    xmlpp::Element* root = new xmlpp::Element(r);

    // Grab the IP address
    std::string xpath = "//*[@id=\"locator\"]/p[1]/b/font/text()";
    auto elements = root->find(xpath);
    if (!elements.empty())
    {
        std::cout << "Your IP address is:" << std::endl;
        std::cout << dynamic_cast<xmlpp::ContentNode*>(elements[0])->get_content() << std::endl;
    }

    delete root;
    xmlFreeDoc(doc);

    return 0;
}


And what you need to build it:
  1. Create project in visual studio
  2. Add file with aforementioned source code
  3. Choose architecture x64 & mode (Debug/Release)
  4. Set static runtime (/MT or /MTd)
  5. Add aforementioned property sheets
  6. Build & Run
And you even don't get any warnings if you did everything what I adviced. And your exe won't have any exports :)

Monday, July 6, 2015

building libxml++ by visual studio

Problem

I want to parse html as DOM - by xpath. The only adequate description found here - http://blog.laplante.io/2014/11/parsing-html-c-revisited/, and it looks like libxml++ the only variant. But here are one huge problem - I'm developing in Visual Studio on Windows, and I prefer static libraries with static runtime. So, I was forced to build all dependencies.
I've found some blog - gtkmm-installation.blogspot.com - well, actuiall I've found it's github project - cloned it, tried to understand what the fuck is going on in this project - didn't understand how does it work and built everything by myself. Maybe if I've read these articles on blogspot I was able to understand it's github content, but somewhy I din't do it. Maybe in the future) Anyway, it's great experience & practice.
Every dependency building I've moved to special blog note, and this one if the final one about libxml++ building.

 

Foreword

I've made graph of dependencies with https://www.draw.io.






Tools & versions

  • visual studio 2013
  • git client
  • Compiled in visual studio 2013 libraries:
    • Compiled intl (version 20090911) in Visual Studio 2013 - [link]
    • Compiled pcre (version 8.37) in Visual Studio 2013 - [link]
    • Compiled libffi (version 3.2.1) in Visual Studio 2013 - [link]
    • Compiled zlib (version 1.2.8) in Visual Studio 2013 - [link]
    • Compiled libsig++ (version 2.4.1) in Visual Studio 2013 - [link]
    • Compiled glib (version 2.45.3) in Visual Studio 2013 - [link]
    • Compiled glibmm (version 2.45.3) in Visual Studio 2013 - [link]
    • Compiled libxml (version 2.9.2) in Visual Studio 2013 - [link]

 

Preparing and checking

---------------------------------------------------------
1) Build libxml2 (I think it's version 2.9.2)
   
    Date:            (03.07.2015 15:58:33)
    Commit hash:    73b2d73df8981e37a03dfdcf727d8bdafb019266
   
    You can use this tutorial:
        http://hostagebrain.blogspot.com/2015/06/building-libxml2-without-dependencies.html
   
    let's assume you have directories:
        D:\projects\libraries\libxmlplusplus\libxml2\_build_x64_static_release_mt
        D:\projects\libraries\libxmlplusplus\libxml2\_build_x64_static_debug_mtd
       
---------------------------------------------------------
2) Build zlib (version 1.2.8)
   
    You can use this tutorial:
        http://hostagebrain.blogspot.com/2015/06/note-of-mt-x64-static-zlib-compiling.html
   
    let's assume you have -MT here:
        D:\projects\libraries\libxmlplusplus\zlib-1.2.8\_build_x64_static_release_mt\include\zlib.h
        D:\projects\libraries\libxmlplusplus\zlib-1.2.8\_build_x64_static_debug_mtd\include\zconf.h
        D:\projects\libraries\libxmlplusplus\zlib-1.2.8\_build_x64_static_release_mt\lib\zlib.lib
        D:\projects\libraries\libxmlplusplus\zlib-1.2.8\_build_x64_static_release_mt\lib\zlib.pdb
    and -MTd here:
        D:\projects\libraries\libxmlplusplus\zlib-1.2.8\_build_x64_static_debug_mtd\include\zlib.h
        D:\projects\libraries\libxmlplusplus\zlib-1.2.8\_build_x64_static_debug_mtd\include\zconf.h
        D:\projects\libraries\libxmlplusplus\zlib-1.2.8\_build_x64_static_debug_mtd\lib\zlib.lib
        D:\projects\libraries\libxmlplusplus\zlib-1.2.8\_build_x64_static_debug_mtd\lib\zlib.pdb
   
---------------------------------------------------------
3) Build intl (version 20090911)
   
    You can use this tutorial:
        http://hostagebrain.blogspot.com/2015/06/proxy-libintl-note.html
   
    let's assume you have -MT here:
        D:\projects\libraries\libxmlplusplus\intl\_build_x64_static_release_mt\include\libintl.h
        D:\projects\libraries\libxmlplusplus\intl\_build_x64_static_release_mt\lib\intl.lib
    and -MTd here:
        D:\projects\libraries\libxmlplusplus\intl\_build_x64_static_debug_mtd\include\libintl.h
        D:\projects\libraries\libxmlplusplus\intl\_build_x64_static_debug_mtd\lib\intl.lib
       
---------------------------------------------------------
4) Build libffi (version 3.2.1)
   
    You can use this tutorial:
        http://hostagebrain.blogspot.com/2015/06/building-libffi-on-windows-by-visual.html
   
    let's assume you have -MT here:
        D:\projects\libraries\libxmlplusplus\libffi-3.2.1\_build_x64_static_release_mt\include
        D:\projects\libraries\libxmlplusplus\libffi-3.2.1\_build_x64_static_release_mt\.libs\libffi_convenience.lib
    and -MTd here:
        D:\projects\libraries\libxmlplusplus\libffi-3.2.1\_build_x64_static_debug_mtd\include
        D:\projects\libraries\libxmlplusplus\libffi-3.2.1\_build_x64_static_debug_mtd\.libs\libffi_convenience.lib
       
---------------------------------------------------------
5) Build pcre (version 8.37)
   
    You can use this tutorial:
        http://hostagebrain.blogspot.com/2015/07/building-pcre-on-visual-studio.html
   
    let's assume you have -MT here:
        D:\projects\libraries\libxmlplusplus\pcre-8.37\_build_x64_static_release_mt\include\pcre.h
        D:\projects\libraries\libxmlplusplus\pcre-8.37\_build_x64_static_release_mt\Release\pcre.lib
    and -MTd here:
        D:\projects\libraries\libxmlplusplus\pcre-8.37\_build_x64_static_debug_mtd\include\pcre.h
        D:\projects\libraries\libxmlplusplus\pcre-8.37\_build_x64_static_debug_mtd\Debug\pcred.lib
       
---------------------------------------------------------
6) libsigc++ (version 2.4.1)
   
    You can use this tutorial:
        http://hostagebrain.blogspot.com/2015/06/build-sigc-in-visual-studio.html
   
    let's assume you have -MT here:
        D:\projects\libraries\libxmlplusplus\libsigc++-2.4.1\MSVC_Net2010\x64\Release\sigc-vc100-2_0.lib
    and -MTd here:
        D:\projects\libraries\libxmlplusplus\libsigc++-2.4.1\MSVC_Net2010\x64\Debug\sigc-vc100-d-2_0.lib
       
---------------------------------------------------------
7) glib (I think it's version 2.45.3)
   
    Date:            (02.07.2015 14:06:17)
    Commit hash:    e337fe31637fe868ab94b488caf9b4898a2a1040
   
    You can use this tutorial:
        http://hostagebrain.blogspot.com/2015/06/build-minimal-glib-in-visual-studio.html
   
    lets assume you have projects here:
        D:\projects\libraries\libxmlplusplus\glib
    and binaries -MT here (despite we built Release_ExtPCRE, binaries will be in Release directory):
        D:\projects\libraries\libxmlplusplus\glib\build\win32\vs10\Release\x64\bin
    and binaries -MTd here (despite we built Debug_ExtPCRE, binaries will be in Debug directory):
        D:\projects\libraries\libxmlplusplus\glib\build\win32\vs10\Debug\x64\bin
   
---------------------------------------------------------   
8) glibmm (2.45.3)

    Date:            (02.07.2015 22:03:00)
    Commit hash:    9c5e30622e38974e909fef60ea045d08000e33a0
   
    You can use this tutorial:
        http://hostagebrain.blogspot.com/2015/07/building-glibmm-by-visual-studio.html
       
    lets assume you have projects here:
        D:\projects\libraries\libxmlplusplus\glibmm
    and binaries -MT here
        D:\projects\libraries\libxmlplusplus\glibmm\MSVC_Net2010\Release\x64\bin
    and binaries -MTd here
        D:\projects\libraries\libxmlplusplus\glibmm\MSVC_Net2010\Debug\x64\bin
   
---------------------------------------------------------

Action

As always, I assume building is going on in directory 'D:\projects\libraries\libxmlplusplus' and all dependencies built by my instructions.

1) Take sources from http://ftp.gnome.org/pub/GNOME/sources/libxml++/

2) Open 'D:\projects\libraries\libxmlplusplus\libxml++-2.38.1\MSVC_Net2010\libxml++.sln'

3) Here are several files must be added in libxml++ project:
  • libxml++\validators\relaxngvalidator.cc
  • libxml++\validators\relaxngvalidator.h
  • libxml++\validators\schemavalidatorbase.cc
  • libxml++\validators\schemavalidatorbase.h
  • libxml++\validators\xsdvalidator.cc
  • libxml++\validators\xsdvalidator.h
  • libxml++\nodes\entitydeclaration.cc
  • libxml++\nodes\xincludeend.cc
  • libxml++\nodes\xincludestart.cc
  • libxml++\nodes\entitydeclaration.h
  • libxml++\nodes\xincludeend.h
  • libxml++\nodes\xincludestart.h
  • libxml++\attributedeclaration.cc
  • libxml++\attributenode.cc
  • libxml++\relaxngschema.cc
  • libxml++\schemabase.cc
  • libxml++\xsdschema.cc
  • libxml++\attributedeclaration.h
  • libxml++\attributenode.h
  • libxml++\relaxngschema.h
  • libxml++\schemabase.h
  • libxml++\xsdschema.h
  • libxml++\schema.cc
  • libxml++\schema.h
4) make libxml++ project static library

5) set /MT or /MTd to libxml++ project

6) make two property sheets - for 'Release x64' and for 'Debug x64'

Debug x64:
  • additional include directories
    • D:\projects\libraries\libxmlplusplus\glib
    • D:\projects\libraries\libxmlplusplus\glib\glib
    • D:\projects\libraries\libxmlplusplus\glibmm\glib
    • D:\projects\libraries\libxmlplusplus\libxml2\_build_x64_static_debug_mtd\include\libxml2
    • D:\projects\libraries\libxmlplusplus\libsigc++-2.4.1\_cygwin\include\sigc++-2.0
    • D:\projects\libraries\libxmlplusplus\pcre-8.37\_build_x64_static_debug_mtd\include
    • D:\projects\libraries\libxmlplusplus\libffi-3.2.1\_build_x64_static_debug_mtd\include
    • D:\projects\libraries\libxmlplusplus\intl\_build_x64_static_debug_mtd\include
  • additional library directories
    • D:\projects\libraries\libxmlplusplus\glibmm\MSVC_Net2010\Debug\x64\bin
    • D:\projects\libraries\libxmlplusplus\libxml2\_build_x64_static_debug_mtd\lib
    • D:\projects\libraries\libxmlplusplus\glib\build\win32\vs10\Debug\x64\bin
    • D:\projects\libraries\libxmlplusplus\libffi-3.2.1\_build_x64_static_debug_mtd\.libs
    • D:\projects\libraries\libxmlplusplus\libsigc++-2.4.1\MSVC_Net2010\x64\Debug
    • D:\projects\libraries\libxmlplusplus\pcre-8.37\_build_x64_static_debug_mtd\Debug
    • D:\projects\libraries\libxmlplusplus\intl\_build_x64_static_debug_mtd\lib
  • input
    • glibmm.lib
    • libxml2_a.lib
    • glib.lib
    • gobject.lib
    • sigc-vc100-d-2_0.lib
    • ws2_32.lib
    • pcred.lib
    • winmm.lib
    • libffi_convenience.lib
    • intl.lib
Release x64:
  • additional include directories
    • D:\projects\libraries\libxmlplusplus\glib
    • D:\projects\libraries\libxmlplusplus\glib\glib
    • D:\projects\libraries\libxmlplusplus\glibmm\glib
    • D:\projects\libraries\libxmlplusplus\libxml2\_build_x64_static_release_mt\include\libxml2
    • D:\projects\libraries\libxmlplusplus\libsigc++-2.4.1\_cygwin\include\sigc++-2.0
    • D:\projects\libraries\libxmlplusplus\pcre-8.37\_build_x64_static_release_mt\include
    • D:\projects\libraries\libxmlplusplus\libffi-3.2.1\_build_x64_static_release_mt\include
    • D:\projects\libraries\libxmlplusplus\intl\_build_x64_static_release_mt\include
  • additional library directories
    • D:\projects\libraries\libxmlplusplus\glibmm\MSVC_Net2010\Release\x64\bin
    • D:\projects\libraries\libxmlplusplus\libxml2\_build_x64_static_release_mt\lib
    • D:\projects\libraries\libxmlplusplus\glib\build\win32\vs10\Release\x64\bin
    • D:\projects\libraries\libxmlplusplus\libffi-3.2.1\_build_x64_static_release_mt\.libs
    • D:\projects\libraries\libxmlplusplus\libsigc++-2.4.1\MSVC_Net2010\x64\Release
    • D:\projects\libraries\libxmlplusplus\pcre-8.37\_build_x64_static_release_mt\Release
    • D:\projects\libraries\libxmlplusplus\intl\_build_x64_static_release_mt\lib
  • input
    • glibmm.lib
    • libxml2_a.lib
    • glib.lib
    • gobject.lib
    • sigc-vc100-2_0.lib
    • ws2_32.lib
    • pcre.lib
    • winmm.lib
    • libffi_convenience.lib
    • intl.lib
It must be exactly libxml2_a.lib, not libxml2.lib, becase 'libxml2.lib' makes dynamic linking.

7) Add property sheet to every project in solution.

8) Select all 'examples...' projects, go to their properties, and make two things:
  • delete .lib filename from 'input'
  • set runtime library - /MT or /MTd

9) To projects:
  • examples_dom_parse_entities
  • examples_dom_parser
add file 'testutilities.cc' to the project (it's located here: 'libxml++-2.38.1\examples\testutilities.cc').

8) Build all - everything must compiles successfully!

Run any example* .exe file - if everything is ok, it must just print something in console and correctly finishes.

building glibmm by visual studio

Problem

I've needed build glibmm (as dependency for libxml++), and wanted it as static x64 library with static runtime.


Tools & versions

  • glibmm (version near 2.45.3)
  • cygwin x32
  • visual studio 2013
  • git client
  • notepad++
  • far manager
  • Compiled in visual studio 2013 libraries:
    • Compiled intl (version 20090911) in Visual Studio 2013 - [link]
    • Compiled pcre (version 8.37) in Visual Studio 2013 - [link]
    • Compiled libffi (version 3.2.1) in Visual Studio 2013 - [link]
    • Compiled zlib (version 1.2.8) in Visual Studio 2013 - [link]
    • Compiled libsig++ (version 2.4.1) in Visual Studio 2013 - [link]
    • Compiled glib (version 2.45.3) in Visual Studio 2013 - [link]
glibmm:
Date: (02.07.2015 22:03:00)
Commit hash: 9c5e30622e38974e909fef60ea045d08000e33a0
just last commit on the moment of building


Action

I assume building take place in 'D:\projects\libraries\libxmlplusplus' and here are all dependencies already built by my instructions.

1) git clone https://github.com/GNOME/glibmm

2) delete '\r' in ./configure.ac & ./autogen.sh (I did it with notepad++)

3) Run 'Cygwin.bat' as administrator (it's important - somewhy it works only this way)

4) In cygwin console go to directory with sources and exec:
export GLIBMM_CFLAGS=-I"D:/projects/libraries/libxmlplusplus/libsigc++-2.4.1/_cygwin/include"
export GLIBMM_LIBS="D:/projects/libraries/libxmlplusplus/libsigc++-2.4.1/_cygwin/lib"
export GIOMM_CFLAGS=-I"D:/projects/libraries/libxmlplusplus/libsigc++-2.4.1/_cygwin/include"
export GIOMM_LIBS="D:/projects/libraries/libxmlplusplus/libsigc++-2.4.1/_cygwin/lib"
./autogen.sh
cd ./glib/src
make
cd ../../gio/src
make
These steps lead to generating a lot of source code files - which just absent without them.

5) Comment '#  define GLIBMM_DLL 1' in files:
  • D:\projects\libraries\libxmlplusplus\glibmm\glib\glibmmconfig.h
  • D:\projects\libraries\libxmlplusplus\glibmm\MSVC_Net2010\glibmm\glibmmconfig.h
6) In visual studio project 'glibmm' authors forgot to add sources (which present in directory with sources) - you should add them to project:
  • binding.h
  • binding.cc
7) Remove all .lib filenames from property_sheet 'glibmmbuilddefinesprops'

8) Create two property sheets - for 'x64 debug' and for 'x64 release' and add them to 'glibmm' project.

x64 Debug:
  • additional include directories
    • D:\projects\libraries\libxmlplusplus\glib
    • D:\projects\libraries\libxmlplusplus\glib\glib
    • D:\projects\libraries\libxmlplusplus\glib\gmodule
    • D:\projects\libraries\libxmlplusplus\libsigc++-2.4.1\_cygwin\include\sigc++-2.0
  • additional library directories
    • D:\projects\libraries\libxmlplusplus\glib\build\win32\vs10\Debug\x64\bin
  • input
    • glib.lib
    • gobject.lib
    • gmodule.lib
x64 Release:
  • additional include directories
    • D:\projects\libraries\libxmlplusplus\glib
    • D:\projects\libraries\libxmlplusplus\glib\glib
    • D:\projects\libraries\libxmlplusplus\glib\gmodule
    • D:\projects\libraries\libxmlplusplus\libsigc++-2.4.1\_cygwin\include\sigc++-2.0
  • additional library directories
    • D:\projects\libraries\libxmlplusplus\glib\build\win32\vs10\Release\x64\bin
  • input
    • glib.lib
    • gobject.lib
    • gmodule.lib
9) set /MT or /MTd - to glibmm

10) set 'configuration type' -> 'static library' - to glibmm

11) build 'glibmm'

building PCRE on visual studio

Problem

Well, actually pcre library uses cmake & builds trivial. But I need some quick instruction for myself - so, let it be here - how to build pcre as static library with static runtime.


Action

1) Go to http://www.pcre.org/
find something like that:
'You can download the current releases of the PCRE and PCRE2 libraries from their official home via anonymous FTP:' ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
Go to this FTP and take pcre sources, for example: pcre-8.37.zip
Download & unpack.

2) for debug version:
- open cmd.exe or far manager, go to directory with sources
- create directory '_build_x64_static_debug_mtd', go to it
- exec: cmake .. -G "Visual Studio 12 2013 Win64"
now in this directory will a lot of files, and one of them must be PCRE.sln
- open PCRE.sln
- select target architecture, mode, select 8 projects (which names is lowercase) in 'solution explorer' and set everywhere /MTd instead of /MDd
- build solution

2) for release version:
- open cmd.exe or far manager, go to directory with sources
- create directory '_build_x64_static_release_mt', go to it
- exec: cmake .. -G "Visual Studio 12 2013 Win64"
now in this directory will a lot of directories, and one of file must be PCRE.sln
- open PCRE.sln
- select target architecture, mode, select 8 projects (which names is lowercase) in 'solution explorer' and set everywhere /MT instead of /MD
- build solution

3) If you want use pcre ONLY as static library, you can add '#define PCRE_STATIC' into pcre.h before the first occurrence of the PCRE_STATIC.

4) I prefer move pcre.h to another directory, bcs when you include this header, extra files from this directory can be also included as side-effect, and lead to problems. So I put it like that:
pcre-8.37\_build_x64_static_release_mt\include\pcre.h
pcre-8.37\_build_x64_static_debug_mtd\include\pcre.h