How to update gcc to 4.8.1 on any ubuntu

Everything is perfectly explained in this post:

http://ubuntuhandbook.org/index.php/2013/08/install-gcc-4-8-via-ppa-in-ubuntu-12-04-13-04/

 
List of commands

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update; sudo apt-get install gcc-4.8 g++-4.8
sudo update-alternatives --remove-all gcc 
sudo update-alternatives --remove-all g++
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 20
sudo update-alternatives --config gcc
sudo update-alternatives --config g++
gcc --version

 

How to compile FastCGI library in Visual Studio 2010 C++

FastCGI comes with Win32 project for VisualStudio6. Unfortunately when you try to open it in VisualStudio2010 the project will be corrupted. To correctly compile it for VS2010 it’s necessary to download library sources, get patches from cybozu.co.jp site and apply it.

Download library

Download latest version of Development Kit here and extract it to standalone directory.

FastCGI

Get and update patches

Download all five patches from Cybozu site. Now when you have patches it’s necessary to convert all files to correct windows-ending format. This can be done for example in Notepad++.

Convert to windows EOL

Get patch.exe application

Now you need some application capable to apply the patches. Unfortunately windows doesn’t contain any application for this task and also lot of comparing/merging applications can’t do that.

The simplest way is to download windows conversion of linux patch.exe app. Get it from gnuwin32 site – patch for windows.

Apply patch to project files

Apply patches

Open directory with fastcgi library, copy all patch files to this directory and open command line. As next run patch command for all fives patches:

...\fcgi-2.4.1-SNAP-0311112127>patch -Np1 < 4-vc9.patch
patching file Win32/FastCGI.sln
patching file Win32/authorizer.vcproj
patching file Win32/cgifcgi.vcproj
patching file Win32/config_h.vcproj
patching file Win32/echo-cpp.vcproj
patching file Win32/echo.vcproj
patching file Win32/echox.vcproj
patching file Win32/libfcgi.vcproj
patching file Win32/logdump.vcproj
patching file Win32/size.vcproj
patching file Win32/threaded.vcproj

In case you will see following error message:

patching file Win32/FastCGI.sln
Assertion failed: hunk, file ../patch-2.5.9-src/patch.c, line 354

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.

it’s necessary to convert ALL patch files to correct Windows format (correct EOL) as mentioned in previous section.

More patches…

When you apply patches by cybozu it’s time to apply my own patches ;-).

Manually updated rejected file

As first, it’s probably that you will see following message:

1 out of 26 hunks FAILED -- saving rejects to file libfcgi/os_win32.c.rej

It’s because original patch is for older version of library and file os_win32.c  is slightly different. To fix that, edit this file at line 841 and manually update following line:

//original
int len = p - bindPath + 1;

//updated line
intptr_t len = p - bindPath + 1;

Update VS project file

Now turn off generating the Map Files in project configuration. These files aren’t necessary and it isn’t possible to compile project correctly with this settings.

Turn off .map files

DONE

And that’s all. Now you will be able to sucesfully compile fast-cgi library.

Compilation complete

External links

FastCGI c++ library for all platforms (Windows, Mac and Linux)

List of existing libraries

Snippets

Stackoverflow articles

Tutorials

Other links

CGICC – how to update it for VS2010 COMPILATION

  •  error C2668: ‘cgicc::copy_if’ : ambiguous call to overloaded function – add cgicc:: to all cgicc::copy_if instances
  •  error LNK2019: unresolved external symbol – add compiled .lib path (…Debug\cgicc.lib) to all dependend projects

Error in RegEx (atlrx.h) in Visual Studio C++ 2003 and 2005

During our development we found that Microsoft RegEx (Regular expression) implementation contains a bug which caused crashes of our applications. The application module using RegEx passed all unit test, but sometimes under heavy usage the application crashed at our customer. Because we use BugTrap for error and crash notifications, we knew the error was in atlrx.h file.

After several hours of testing and searching we found the bug. The crash didn’t occur after first code execution, but we had to run thousand iterations of the same code over and over. The bug is located in file atlrx.h at line 708.

Original file looks like this:

  case RE_ADVANCE:
    sz = CharTraits::Next(szCurrInput);
    szCurrInput = sz;
    if ( sz == NULL || *sz == '\0')
      goto Error;
    ip = 0;
    pContext->m_nTos = 0;
    break;

Problem is, that variable szCurrInput have in some circumstances NULL value and this causes the crashes.

Updated file with bug fix:

  case RE_ADVANCE:
    if( szCurrInput == NULL || *szCurrInput == '\0' )
      goto Error;
    sz = CharTraits::Next(szCurrInput);
    szCurrInput = sz;
    if ( sz == NULL || *sz == '\0')
      goto Error;
    ip = 0;
    pContext->m_nTos = 0;
    break;

We change the first two lines. It is necessary to test szCurrInput variable for NULL and empty string value. If szCurrInput is NULL or empty string, it’s necessary to stop processing RegEx. Otherwise stack overflow during processing string occurs.

Note

Some time later we had other problems with Microsoft RegEx implementation and non-standard RegEx syntax. So we left MS RegEx parser and moved to Boost.Regex which is really nice piece of code (as well as other libraries of the Boost pack) and supports Perl and POSIX regular expressions. Whole Boost library is carefully unit test and can be relied on.

List of C++ profilers for windows (commercial and non-commercial)

Commercial

Free

System performance tools

Linux test tools (not tested yet)

External resources

 

Problem with compiling Boost

Warning: "using python" expects a two part (major, minor) version number; got %d.%d\nplatform=%s\nprefix=%s\nexec_prefix=%s\nexecutable=%s' % (version_info[0],version_info[1],platform,prefix,exec_prefix,executable))"  instead
Q:/ExternalLibraries/boost_trunk/tools/build/v2/build\feature.jam:276: in implied-feature from module featureerror: "version_info[1]" is not an implicit feature value
Q:/ExternalLibraries/boost_trunk/tools/build/v2/build\feature.jam:355: in expand-subfeatures-aux from module featureQ:/ExternalLibraries/boost_trunk/tools/build/v2/build\feature.jam:424: in feature.expand-subfeatures from module feature
Q:/ExternalLibraries/boost_trunk/tools/build/v2/build\property.jam:186: in property.expand-subfeatures-in-conditions from module property
Q:/ExternalLibraries/boost_trunk/tools/build/v2/build\property-set.jam:407: in property-set.create-from-user-input from module property-set
Q:/ExternalLibraries/boost_trunk/tools/build/v2/build\property-set.jam:461: in property-set.refine-from-user-input from module property-set
Q:/ExternalLibraries/boost_trunk/tools/build/v2/build\targets.jam:1580: in targets.main-target-requirements from module targets
Q:/ExternalLibraries/boost_trunk/tools/build/v2/tools\builtin.jam:532: in lib from module builtin
Q:/ExternalLibraries/boost_trunk/tools/build/v2/tools\python.jam:680: in declare-libpython-target from module python
Q:/ExternalLibraries/boost_trunk/tools/build/v2/tools\python.jam:925: in configure from module python
Q:/ExternalLibraries/boost_trunk/tools/build/v2/tools\python.jam:110: in python.init from module python
Q:/ExternalLibraries/boost_trunk/tools/build/v2/build\toolset.jam:41: in using from module toolset libs\python\build\Jamfile.v2:16: in modules.load from module Jamfile<Q:\External
Libraries\boost_trunk\libs\python\build>
Q:/ExternalLibraries/boost_trunk/tools/build/v2/build\project.jam:311: in load-jamfile from module project
Q:/ExternalLibraries/boost_trunk/tools/build/v2/build\project.jam:64: in load from module project
Q:/ExternalLibraries/boost_trunk/tools/build/v2/build\project.jam:89: in load-used-projects from module project
Q:/ExternalLibraries/boost_trunk/tools/build/v2/build\project.jam:75: in load from module project
Q:/ExternalLibraries/boost_trunk/tools/build/v2/build\project.jam:145: in project.find from module project
Q:/ExternalLibraries/boost_trunk/tools/build/v2\build-system.jam:552: in load from module build-system
Q:\ExternalLibraries\boost_trunk\tools\build\v2/kernel\modules.jam:289: in import from module modules
Q:\ExternalLibraries\boost_trunk\tools\build\v2/kernel/bootstrap.jam:139: in boost-build from module
Q:\ExternalLibraries\boost_trunk\boost-build.jam:17: in module scope from module

The problem was that boost build helper run my python.bat instead of correct python.exe from %PATH% directory.

To check which python is used enter following param:

b2 --debug-configuration:
notice: [python-cfg] Configuring python...
notice: [python-cfg] Registry indicates Python 2.7 installed at "C:\Python27\"
notice: [python-cfg] Checking interpreter command "python"...
notice: [python-cfg] running command 'python -c "from sys import *; print('version=%d.%d\nplatform=%s\nprefix=%s\nexec_prefix=%s\nexecutable=%s' % (version_info[0],version_info[1],platform,prefix,exec_prefix,executable))" 2>&1'
notice: [python-cfg] ...does not invoke a working interpreter
notice: [python-cfg] Checking interpreter command "C:\Python27\python"...

####HERE####
notice: [python-cfg] running command 'DIR /-C /A:S "C:\Python27\python.exe" 2>&1'
####HERE####

notice: [python-cfg] running command 'C:\Python27\python -c "from sys import *;print('version=%d.%d\nplatform=%s\nprefix=%s\nexec_prefix=%s\nexecutable=%s' % (version_info[0],version_info[1],platform,prefix,exec_prefix,executable))" 2>&1'
notice: [python-cfg] ...requested configuration matched!
notice: [python-cfg] Details of this Python configuration:
notice: [python-cfg]   interpreter command: "C:\Python27\python"
notice: [python-cfg]   include path: "C:\Python27\Include"
notice: [python-cfg]   library path: "C:\Python27\libs"
notice: [python-cfg]   DLL search path: "C:\Python27"

Qt application crash when compiled for 64-bit VS2010

Callstack

>	QtGui4.dll!00000000652f071f()
 	[Frames below may be incorrect and/or missing, no symbols loaded for QtGui4.dll]
 	QtGui4.dll!00000000652f07ae()
 	QtGui4.dll!0000000065326821()
 	QtGui4.dll!0000000065327f30()
 	QtGui4.dll!00000000653284ba()
 	QtGui4.dll!000000006532a5f5()
 	QtGui4.dll!00000000652f4792()
 	QtGui4.dll!000000006532f701()
 	QtGui4.dll!000000006535be71()
 	QtCore4.dll!0000000059a0ab9f()
 	QtGui4.dll!0000000065306eb9()
 	QtCore4.dll!00000000599f7903()
 	QtGui4.dll!0000000064eaffa2()
 	QtGui4.dll!0000000064eb2e56()
 	QtCore4.dll!00000000599f7792()
 	QtGui4.dll!0000000064eec2a3()
 	QtGui4.dll!0000000064eee85e()
 	QtGui4.dll!0000000064eec265()
 	QtGui4.dll!0000000064eee85e()
 	QtGui4.dll!0000000064eec265()
 	QtGui4.dll!0000000064eee85e()
 	QtGui4.dll!0000000064efa870()
 	QtGui4.dll!0000000065208727()
 	QtGui4.dll!000000006527a082()
 	QtGui4.dll!0000000064eaffb6()
 	QtGui4.dll!0000000064eb2e56()
 	QtCore4.dll!00000000599f7792()
 	QtGui4.dll!0000000064ef264c()
 	QtGui4.dll!0000000064ef26c7()
 	QtGui4.dll!0000000064ef26c7()
 	QtGui4.dll!0000000064ef26c7()
 	QtGui4.dll!0000000064efe30c()
 	OrmDesigner2.exe!000000013f95a2cf()
 	OrmDesigner2.exe!000000013f95dce2()
 	kernel32.dll!000000007794652d()
 	ntdll.dll!0000000077c8c521()

The bug is probably caused by wrong alignment or incorrect VS2010 compiler optimalization.

Found resources:
https://bugreports.qt.nokia.com//browse/QTBUG-11445
https://connect.microsoft.com/VisualStudio/feedback/details/573262/incorrect-alignment-with-x64-optimizer-and-movaps
http://support.microsoft.com/kb/2280741
https://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=31433

Workarounds:

First way – Update VS2010 mkspec for Qt

go to $QTDIR/mkspecs/win32-msvc2010/qmake.conf and replace -02 with -01 (ie. reducing the optimzation level).Also /Ob0 could be used.

Microsoft hotfix for VS

http://archive.msdn.microsoft.com/KB2280741
File patch name VS10-KB2268081-x86.exe

Install Visual Studio 2010 SP1

Service pack could also contains required fix.
http://www.microsoft.com/download/en/details.aspx?id=23691

Strange “no matching function for call to …” error on MacOS

Error appear when using BOOST::fusion::map<....>

Error message:

In file included from axOrm/ormObject/ormObject.test.cpp:5:
axOrm/ormObject/testObjects/objContactAndAddress.h: In member function 'void Atomix::Orm::Tests::CBaseOrmContact::SetName(XString)':
axOrm/ormObject/testObjects/objContactAndAddress.h:92: error: no matching function for call to 'Atomix::Orm::Tests::CBaseOrmContact::GetPropertyHolder()'

Invalid source code

template <class TNameParam> typename fus::result_of::at_key< TmapAssociations,TNameParam>::type & GetAssociationHolder()
{ return fus::at_key<TNameParam>(m_mapAssociations); }

for fix simply change result_of::at_key to result_of::value_at_key

template <class TNameParam> typename fus::result_of::value_at_key<TmapAssociations,TNameParam>::type & GetAssociationHolder()

After fixing this error, you can achieved problem with const modifier. Correct syntax for const definition is:

template typename fus::result_of::value_at_key::type const & GetPropertyHolder() const
{ return fus::at_key(m_mapProperties); }

C++ – unpack sub-type from template structure only for specified object type

#include <assert.h>
#include <typeinfo>
 
struct CTreeObject1
{
};
 
/************************************************************************/
/* Nammed structs - for using one type more than once in SinglesCont.   */
/************************************************************************/
template< class TObject, class TName>
struct NammedObject : public TObject, public TName 
{
        typedef TObject object_type;
};
 
struct stFirst{};
struct stSecond{};
                
template <typename T>
struct UnpackObjectType
{
        typedef T TT;
};
 
template<typename T, typename U>
struct UnpackObjectType< NammedObject<T, U> >
{
        typedef T TT; 
};
 
int main()
{
        typedef NammedObject<CTreeObject1,stFirst>  T1;
        typedef NammedObject<CTreeObject1,stSecond>  T2;
        typedef CTreeObject1                        T3;
 
        UnpackObjectType<T1>::TT d1;
        UnpackObjectType<T2>::TT d2;
        UnpackObjectType<T3>::TT d3;
        
        //required
        assert( typeid(d1) == typeid(d2));
        assert( typeid(d2) == typeid(d3));
        
        return 0;
}

g++ error: conversion from XTestObjectA to non-scalar type XTestObjectBase requested

//error
XTestObjectBase objTestObjectBase1 = objTestObjectA;

//ok
XTestObjectBase objTestObjectBase2(objTestObjectA);

//ok
XTestObjectBase objTestObjectBase3; objTestObjectBase3 = objTestObjectA;

cite from: http://stackoverflow.com/questions/6120240/why-constructor-is-not-called-for-given-casting-operator

The problem is that the number of user-defined conversions that are invoked implicitly is limited (to 1) by the Standard.

B ob = a;
implies two user conversions:

on a: Wrap::operator A*() should be called
on the result: B::B(A*) should be called

The solution is to use explicit conversion

//now ok
XTestObjectBase objTestObjectBase1 = XTestObjectBase(objTestObjectA);

g++ error: expected primary-expression before > token

For g++ compiler is sometimes necessary to specify more typename / template keywords than for Visual Studio. This is one of examples.

To the following code it’s necessary to add template keyword:

TMemoryManager & memMngr = GetMemoryManager();
m_pObject = 
  memMngr.CreateObject<TObject>(this, m_pContainerOwner);

use

TMemoryManager & memMngr = GetMemoryManager();
m_pObject = 
  memMngr.template CreateObject<TObject>(this, m_pContainerOwner);

MinGW and compilation 32/64bit libraries

Install MinGW-64

1) Download MinGW 64-bit and 32-bit

http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win64/

http://sourceforge.net/projects/mingw-w64/files/Toolchains%20targetting%20Win32/

– Toolchains targetting Win64 / Toolchains targetting Win32
– Automated Builds
– mingw-w64-1.0-bin_i686-mingw_*.zip
– mingw-w32-1.0-bin_i686-mingw_*.zip

2) Download MSYS compatible with 64-bit MinGW

http://sourceforge.net/projects/mingw-w64/files/External%20binary%20packages%20(Win64%20hosted)/MSYS%20(32-bit)/

3) Extract MinGW32, mingw64 and MSYS to one folder

MinGW-32, MinGW-64 and MSYS to folder MinGW64 (Bin folder from MinGW will be merged with bin folder from MSYS, etc.)

External links:

MSYS wiki
MinGW-64 wiki

Collection of links about ORM, Datamapper and design patterns

Domain model

http://www.slideshare.net/weierophinney/architecting-your-models

ORM design patterns

http://martinfowler.com/eaaCatalog/mapper.html
http://martinfowler.com/eaaCatalog/dataMapper.html

DataMapper

http://stackoverflow.com/questions/207758/data-mapper-pattern
http://stackoverflow.com/questions/762252/fowler-data-mapper-object-creation
http://stackoverflow.com/questions/4658340/should-data-mapper-reference-domain-model
http://stackoverflow.com/questions/1977684/should-a-finder-method-be-part-of-the-data-mapper-or-part-of-the-domain-class
http://stackoverflow.com/questions/3738687/using-the-data-mapper-pattern-should-the-entities-domain-objects-know-about-th

Anemic domain model

http://www.martinfowler.com/bliki/AnemicDomainModel.html (Anemic domain model – why huge service layer  is bad)
http://stackoverflow.com/questions/258534/anemic-domain-model-pros-cons

Data Mapper best practices

http://stackoverflow.com/questions/4465237/doctrine2-best-practice-should-entities-use-services
http://stackoverflow.com/questions/4108291/using-entitymanager-inside-doctrine-2-0-entities/4109458#4109458

Doctrine2 articles

http://www.doctrine-project.org/docs/orm/2.0/en/cookbook/aggregate-fields.html