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 :)

1 comment: