VS2010 always rebuilding moc files on Qt

My VS2010 on every build always recreate all .moc files and associated .cpp files. Problem was in incredibuild clausule during compilation of my Qt libraries.

 

      <AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Rem;axApplication\axAppException\dialogUnhandledException.ui;%(AdditionalInputs)</AdditionalInputs>
      <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Rem IncrediBuild_AllowRemote &#x000D;&#x000A;if errorlevel 1 goto VCEnd&#x000D;&#x000A; Rem IncrediBuild_OutputFile ..\GeneratedFiles\ui_dialogUnhandledException.h &#x000D;&#x000A;if errorlevel 1 goto VCEnd&#x000D;&#x000A; Q:\ExternalLibraries\qt-git-32bit\bin\uic.exe axApplication\axAppException\dialogUnhandledException.ui -o ..\GeneratedFiles\ui_dialogUnhandledException.h</Command>
      <Message Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">UIC axApplication\axAppException\dialogUnhandledException.ui</Message>
      <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">..\GeneratedFiles\ui_dialogUnhandledException.h;%(Outputs)</Outputs>

instead of

      <AdditionalInputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Q:\ExternalLibraries\qt-4.8.2-32bit\bin\uic.exe;axApplication\axAppException\dialogUnhandledException.ui;%(AdditionalInputs)</AdditionalInputs>
      <Command Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Q:\ExternalLibraries\qt-4.8.2-32bit\bin\uic.exe axApplication\axAppException\dialogUnhandledException.ui -o ..\GeneratedFiles\ui_dialogUnhandledException.h</Command>
      <Message Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">UIC axApplication\axAppException\dialogUnhandledException.ui</Message>
      <Outputs Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">..\GeneratedFiles\ui_dialogUnhandledException.h;%(Outputs)</Outputs>

Although qmake added all these incredibuild defines to my VS project, Incredibuild still doesn’t compile it in parallel so it was totally useless. The most simply way is edit incredibuild_xge.prf and turn-off it.

mkspecs\features\incredibuild_xge.prf 

contains(TEMPLATE, "XXXvc.*") {

How to enable VS2010 logging

GetTickCount64 problem on WindowsXP

The procedure entry point GetTickCount64 could not be located in the dynamic link library KERNEL32.dll
The procedure entry point GetTickCount64 could not be located in the dynamic link library KERNEL32.dll

Today we received report from one of our customers about problem with our ORM Designer on WindowsXP – 32bit. Our latest version returns following error:

The procedure entry point GetTickCount64 could not be located in the dynamic link library KERNEL32.dll

The problem is that GetTickCount64 doesn’t exists in XP system.

Solution:

It’s necessary to compile your application (and any library which uses GetTickCount64) with correct WINVER and _WIN32_WINNT defines value. List of WINVER values for all Windows version can be found here.

You can define these values in your resource.h file:

#define WINVER 0x0501
#define _WIN32_WINNT 0x0501

or add it to a project property -> C/C++ -> Preprocessor -> Preprocessor Definitions:

2013-08-29_15-45-10

Qt Solution:

If you want to define these values automatically in Qt project, add following lines to your .pro file:

#Windows XP compatability
DEFINES += "WINVER=0x0501"
DEFINES += "_WIN32_WINNT=0x0501"

External links:

How to sign your Qt Mac OS X App for Gatekeeper

Starting from Mac os 10.8 apple applications requires certificate. Without that certificate (or without additional system tweaks described here on our product support page: http://support.orm-designer.com/5/macos-mountain-lion-10-8-unidentified-developer ) user will se following message:

"OrmDesigner2" can't be opened because it is from an unidentified developer.
MacOS unidentified developer in ORM Designer

Solution

To solve this error message it’s necessary to do following steps:

  1. Register in Apple developer program and pay $99 per year
  2. Download and install developer certificate
  3. Sign whole application
  4. Test it!

1) Register on Developer.apple.com

You need to create registration here: https://developer.apple.com/. It’s necessary to fill info about contact person and company. After that, your registration will be reviewed by apple team and if everything will be OK, your registration will be approved.

2) Use Apple site to generate certificates

Open https://developer.apple.com/account/overview.action ,choose Certificates,  Click Add. Than select certificate parameters suitable for your need. In my case it was Mac Development and  Developer ID.

Now you need to install this certificate to your developer machine. Simply double-click on certificate and let system to import it. You can check that certificate is imported in Go->Utilities->Keychain Access->login. Now search for “Developer ID Application: XXXX”

MacOS certificate

Note: In my case when I transfer certificate to several developer machines I need to migrate also other Apple certificates. Without that my certificate wasn’t a valid.

3) Sign your application

Now you need to sign your application including all plugins and frameworks inside app bundle. After you sing your app, you can’t do any changes in the bundle. So as first run your deploy as usual and as last step do app singing.

For ORM Designer sign script looks like this:

#go to deploy directory
cd $StarkDeploy.directory$/deploy

#sign app
codesign --force --verify --verbose --sign "Developer ID Application: Inventic s.r.o." ./OrmDesigner2.app

#sign all *.dylib files
find OrmDesigner2.app -name *.dylib | xargs -I $ codesign --force --verify --verbose --sign "Developer ID Application: Inventic s.r.o." $

#sign all Qt* frameworks
find OrmDesigner2.app -name Qt* -type f | xargs -I $ codesign --force --verify --verbose --sign "Developer ID Application: Inventic s.r.o." $

4) Test it!

As last step it’s necessary to test that sign process was successful. As first you can try following command line to validate  it:

codesign -vvv -d OrmDesigner2.app

#RESULT:
Executable=/OrmDesigner2/DeployFiles/macos64/deploy/OrmDesigner2.app/Contents/MacOS/OrmDesigner2
Identifier=com.orm-designer.OrmDesigner2
Format=bundle with Mach-O thin (x86_64)
CodeDirectory v=20100 size=174478 flags=0x0(none) hashes=8717+3 location=embedded
Hash type=sha1 size=20
CDHash=5a491e16f7dcca15b44af4XXXX1a2d2dcc786518
Signature size=4237
Authority=Developer ID Application: Inventic s.r.o. (6BYV46LH6T)
Authority=Developer ID Certification Authority
Authority=Apple Root CA
Signed Time=6 Jun 2013 23:16:08
Info.plist entries=10
Sealed Resources rules=4 files=27
Internal requirements count=1 size=212

Now when you checked that App is correctly signed, it’s time to try it on clean computer where no security policy changes was made. Upload your app and execute it.

If you don’t see annoying screen “Can’t execute application from unidentified developer”, you win ;-).

External links

How to transfer certificate: 

How to import:

Apple links

Qt stylesheet guidance

Style sheets

Layout Management

How to tips

How to recompute Qt style-sheet based on Q_PROPERTY:

void QControl::RedrawWidgetStylesheet()
{
	style()->unpolish(this);
	style()->polish(this);
	update();
}

Problem with Qt application on MacOS Retina display

Standard Qt application on retina display looks ugly. Fuzzy fonts and images. This is how ORM Designer originaly looks on Retina:

ORM Designer on Retina before optimizations

Improve font autoscaling

First step how to improve Qt application on retina is by adding following definise to Info.plist

NSPrincipalClass
NSApplication
How to update Info.plist

If you’re updating existing application, it’s necessary to copy application to different location after Info.plist update. MacOS cache all .plist files and without that update will not be apply.

After applying this fix, this is how application looks:

How ORM Designer looks after fix

External sources

Qt QtDBus compilation

To add DBUS to Qt compilation, add following switch

./configure -opensource -dbus ....

If you get following error:

The QtDBus module cannot be enabled because libdbus-1 version 0.93 was not found.
Turn on verbose messaging (-v) to ./configure to see the final report.
If you believe this message is in error you may use the continue
switch (-continue) to ./configure to continue.

It’s necessary to install libsbus:

sudo apt-get install libdbus-1-3
Reading package lists... Done
Building dependency tree
Reading state information... Done
libdbus-1-3 is already the newest version.

and also developer package:

sudo apt-get install libdbus-1-dev

Now everything should work correctly.

Qt and Google breakpad Windows/Linux/MacOS

Integration of Google breakpad on any platform is really challenge. I didn’t figure out how to do it by using recommended ways. So I did it my way 😉

Create your own Qt project

As first thing I did when integrating Qt and Breakpad is creation of my own .pri file. I manually add all required files from Breakpad one-by-one, starting with src/platform/exception_handler continuing with all files included in previous ones. The complete .pri file you will find at end of this post.

After that I created simple CCrashHandler (inspired by qt-breakpad project) class which register exception handler. It’s necessary to implement this handler for each platform separately because each platform have different parameters in google_breakpad::ExceptionHandler() constructor. CrashHandler source code you can find also at end of this post.

Using Breakpad on Linux:

Article about Linux integration.

Notes Linux

  • GCC 4.6.3
  • Ubuntu 12.04.1 32/64bit

Get and Compile Breakpad

#get breakpad latest version
svn checkout http://google-breakpad.googlecode.com/svn/trunk/ google-breakpad-read-only

#compile all breakpad tools for extracting symbol files
./configure
make
sudo make install

#when compilation is ready, you should have installed file dump_syms from original directory /src/tools/linux/dump_syms in your /usr/bin
dump_syms

Generate symbol file and test debug info

Now how to generate and use symbol file:

#generate .sym file
dump_syms ./Application > Application.sym

#store sym file in the correct location
#this step is necessary. Without that minidump_stackwalk tool doesn't work

head -n1 Application.sym
#Result: MODULE Linux x86_64 6EDC6ACDB282125843FD59DA9C81BD830 Application

mkdir -p ./symbols/Application/6EDC6ACDB282125843FD59DA9C81BD830
mv Application.sym ./symbols/Application/6EDC6ACDB282125843FD59DA9C81BD830

#show stack with using minidump_stackwalk tool
minidump_stackwalk ./crash.dmp ./symbols

Result of minidump_stackwalk tool can look like this:

Use additional tools

There is script written by Mozzila corp which simplifies extracting and storing .sym file:
http://mxr.mozilla.org/mozilla-central/source/toolkit/crashreporter/tools/symbolstore.py?raw=1

Now with this script, you can do only one step instead of all described above.

python /path/to/script/symbolstore.py /usr/local/bin/dump_syms ./symbol-storage ./Application

This command correctly creates folder Application/uuid in path symbol-storage and copy generated .sym file inside.

Using Breakpad on MacOS:

To integrate Breakpad and Qt on MacOS I used the same article as for Linux because I’m using gcc and linux-like development toolchain. For native XCode integration you have to probably use this article.

Notes MacOS

  • GCC 4.6.3
  • Qt creator toolchain
  • Mac OS 10.7.3 64bit
  • Don’t using any of XCode tool chaing

Do almost all steps like on Linux integration. There is one exception, because you can’t compile Breakpad tools using linux-like configure&make. You have to build these tools with XCode.

Compile google_breakpad/src/tools/mac/dump_syms/dump_syms.xcodeproj with XCode

Before I managed to successfully compile dump_syms in XCode, I have to update Architectures Debug/Release to 64-bit Intel and BaseSDK to “Latest Mac OS X”. I also fix all issues displayed in the left part of XCode window.

Next step is change compilation type from Debug to Release. After a short searching I found a way in “Schema->Edit schema->Build configuration”.

To be honest, I was not sure what I did, but it worked ;-). Maybe there is some easier way how to compile dump_syms in xcode, but I don’t know how. This was my first contact with XCode ;-).

The application is compiled to path “/Users/User/Library/Developer/Xcode/DerivedData/dump_syms-bjvr/Build/Products/Release/dump_syms”. Now copy dump_syms to safe location from where you will use it.

The rest steps are same like in the Linux.

Using Breakpad on Windows

Notes Windows

Article about Windows integration.

  • Compile with Visual Studio 2010
  • Post-mortem debugging is done also via VS2010
  • Google breakpad under windows generates common .dmp files which can by simply load directly to VS

Generate symbol files and test debug info

You have to choices for Windows. First option is use standard .PDB and .DMP files mechanism in VS. Second is use the same way how you can did it for Linux and Mac.

First way: Let compiler generate .PDB file. Store this files and use them later in VS together with your crash dump.

Second way: Use dump_sys.exe. This file is located in the google breakpad directory tree: google-breakpad\src\tools\windows\binaries\dump_syms.exe .

dump_syms.exe AtomixDevelopment.exe >AtomixDevelopment.sym

And also like under Linux, you can use Python script symbolstore.py to extract symbols to correct directory:

python.exe symbolstore.py binaries\dump_syms.exe .\Symbols AtomixDevelopment.exe

Minidump stackwalk for windows:
http://code.google.com/p/pcxfirefox/source/browse/trunk/betterpgo/talos/talos/breakpad/win32/minidump_stackwalk.exe?r=19

The best thing at end

The most amazing thing I found about dump_syms is their platform independence. If you correctly extract symbol file on each platform and store it in one location, you can analyze any crashdump on one location. No matter what is source platform, you will analyze .dmp file by using minidump_stackwalk ./crash.dmp ./symbols.

How to include CrashHandler directly to your project

This is what I get when I follow all includes from .h and .cpp files starting with exception_handler.h for all platforms. With small modification you can include it to your project and your application will be fully breakpad-able ;-).

# ---------- HEADER -----------------------------------------------------------
OTHERS   += $$PWD/axCrashHandler.pri
HEADERS += $$PWD/CrashHandler.h
SOURCES += $$PWD/CrashHandler.cpp

BREAKPAD_PATH=$$EXTERNAL_LIBRARIES_PATH/breakpad-qt
INCLUDEPATH += $$BREAKPAD_PATH/src

OSMAC {
  HEADERS += $$BREAKPAD_PATH/src/client/mac/handler/exception_handler.h
  HEADERS += $$BREAKPAD_PATH/src/client/mac/crash_generation/crash_generation_client.h
  HEADERS += $$BREAKPAD_PATH/src/client/mac/crash_generation/crash_generation_server.h
  HEADERS += $$BREAKPAD_PATH/src/client/mac/crash_generation/client_info.h
  HEADERS += $$BREAKPAD_PATH/src/client/mac/handler/minidump_generator.h
  HEADERS += $$BREAKPAD_PATH/src/client/mac/handler/dynamic_images.h
  HEADERS += $$BREAKPAD_PATH/src/client/mac/handler/breakpad_nlist_64.h
  HEADERS += $$BREAKPAD_PATH/src/client/mac/handler/mach_vm_compat.h
  HEADERS += $$BREAKPAD_PATH/src/client/minidump_file_writer.h
  HEADERS += $$BREAKPAD_PATH/src/client/minidump_file_writer-inl.h
  HEADERS += $$BREAKPAD_PATH/src/common/mac/macho_utilities.h
  HEADERS += $$BREAKPAD_PATH/src/common/mac/byteswap.h
  HEADERS += $$BREAKPAD_PATH/src/common/mac/MachIPC.h
  HEADERS += $$BREAKPAD_PATH/src/common/mac/scoped_task_suspend-inl.h
  HEADERS += $$BREAKPAD_PATH/src/common/mac/file_id.h
  HEADERS += $$BREAKPAD_PATH/src/common/mac/macho_id.h
  HEADERS += $$BREAKPAD_PATH/src/common/mac/macho_walker.h
  HEADERS += $$BREAKPAD_PATH/src/common/mac/macho_utilities.h
  HEADERS += $$BREAKPAD_PATH/src/common/mac/bootstrap_compat.h
  HEADERS += $$BREAKPAD_PATH/src/common/mac/string_utilities.h
  HEADERS += $$BREAKPAD_PATH/src/common/linux/linux_libc_support.h
  HEADERS += $$BREAKPAD_PATH/src/common/string_conversion.h
  HEADERS += $$BREAKPAD_PATH/src/common/md5.h
  HEADERS += $$BREAKPAD_PATH/src/common/memory.h
  HEADERS += $$BREAKPAD_PATH/src/common/using_std_string.h
  HEADERS += $$BREAKPAD_PATH/src/common/convert_UTF.h
  HEADERS += $$BREAKPAD_PATH/src/processor/scoped_ptr.h
  HEADERS += $$BREAKPAD_PATH/src/google_breakpad/common/minidump_exception_mac.h
  HEADERS += $$BREAKPAD_PATH/src/google_breakpad/common/breakpad_types.h
  HEADERS += $$BREAKPAD_PATH/src/google_breakpad/common/minidump_format.h
  HEADERS += $$BREAKPAD_PATH/src/google_breakpad/common/minidump_size.h
  HEADERS += $$BREAKPAD_PATH/src/third_party/lss/linux_syscall_support.h

  SOURCES += $$BREAKPAD_PATH/src/client/mac/handler/exception_handler.cc
  SOURCES += $$BREAKPAD_PATH/src/client/mac/crash_generation/crash_generation_client.cc
  SOURCES += $$BREAKPAD_PATH/src/client/mac/crash_generation/crash_generation_server.cc
  SOURCES += $$BREAKPAD_PATH/src/client/mac/handler/minidump_generator.cc
  SOURCES += $$BREAKPAD_PATH/src/client/mac/handler/dynamic_images.cc
  SOURCES += $$BREAKPAD_PATH/src/client/mac/handler/breakpad_nlist_64.cc
  SOURCES += $$BREAKPAD_PATH/src/client/minidump_file_writer.cc
  SOURCES += $$BREAKPAD_PATH/src/common/mac/macho_id.cc
  SOURCES += $$BREAKPAD_PATH/src/common/mac/macho_walker.cc
  SOURCES += $$BREAKPAD_PATH/src/common/mac/macho_utilities.cc
  SOURCES += $$BREAKPAD_PATH/src/common/mac/string_utilities.cc
  SOURCES += $$BREAKPAD_PATH/src/common/mac/file_id.cc
  SOURCES += $$BREAKPAD_PATH/src/common/mac/MachIPC.mm
  SOURCES += $$BREAKPAD_PATH/src/common/mac/bootstrap_compat.cc
  SOURCES += $$BREAKPAD_PATH/src/common/md5.cc
  SOURCES += $$BREAKPAD_PATH/src/common/string_conversion.cc
  SOURCES += $$BREAKPAD_PATH/src/common/linux/linux_libc_support.cc
  SOURCES += $$BREAKPAD_PATH/src/common/convert_UTF.c
  LIBS += /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
  LIBS += /System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices
  #breakpad app need debug info inside binaries
  QMAKE_CXXFLAGS+=-g
}

OSLIN {
  HEADERS += $$BREAKPAD_PATH/src/client/linux/handler/exception_handler.h
  HEADERS += $$BREAKPAD_PATH/src/client/linux/crash_generation/crash_generation_client.h
  HEADERS += $$BREAKPAD_PATH/src/client/linux/handler/minidump_descriptor.h
  HEADERS += $$BREAKPAD_PATH/src/client/linux/minidump_writer/minidump_writer.h
  HEADERS += $$BREAKPAD_PATH/src/client/linux/minidump_writer/line_reader.h
  HEADERS += $$BREAKPAD_PATH/src/client/linux/minidump_writer/linux_dumper.h
  HEADERS += $$BREAKPAD_PATH/src/client/linux/minidump_writer/linux_ptrace_dumper.h
  HEADERS += $$BREAKPAD_PATH/src/client/linux/minidump_writer/directory_reader.h
  HEADERS += $$BREAKPAD_PATH/src/client/linux/log/log.h
  HEADERS += $$BREAKPAD_PATH/src/client/minidump_file_writer-inl.h
  HEADERS += $$BREAKPAD_PATH/src/client/minidump_file_writer.h
  HEADERS += $$BREAKPAD_PATH/src/common/linux/linux_libc_support.h
  HEADERS += $$BREAKPAD_PATH/src/common/linux/eintr_wrapper.h
  HEADERS += $$BREAKPAD_PATH/src/common/linux/ignore_ret.h
  HEADERS += $$BREAKPAD_PATH/src/common/linux/file_id.h
  HEADERS += $$BREAKPAD_PATH/src/common/linux/memory_mapped_file.h
  HEADERS += $$BREAKPAD_PATH/src/common/linux/safe_readlink.h
  HEADERS += $$BREAKPAD_PATH/src/common/linux/guid_creator.h
  HEADERS += $$BREAKPAD_PATH/src/common/linux/elfutils.h
  HEADERS += $$BREAKPAD_PATH/src/common/linux/elfutils-inl.h
  HEADERS += $$BREAKPAD_PATH/src/common/using_std_string.h
  HEADERS += $$BREAKPAD_PATH/src/common/memory.h
  HEADERS += $$BREAKPAD_PATH/src/common/basictypes.h
  HEADERS += $$BREAKPAD_PATH/src/common/memory_range.h
  HEADERS += $$BREAKPAD_PATH/src/common/string_conversion.h
  HEADERS += $$BREAKPAD_PATH/src/common/convert_UTF.h
  HEADERS += $$BREAKPAD_PATH/src/google_breakpad/common/minidump_format.h
  HEADERS += $$BREAKPAD_PATH/src/google_breakpad/common/minidump_size.h
  HEADERS += $$BREAKPAD_PATH/src/google_breakpad/common/breakpad_types.h
  HEADERS += $$BREAKPAD_PATH/src/processor/scoped_ptr.h
  HEADERS += $$BREAKPAD_PATH/src/third_party/lss/linux_syscall_support.h
  SOURCES += $$BREAKPAD_PATH/src/client/linux/crash_generation/crash_generation_client.cc
  SOURCES += $$BREAKPAD_PATH/src/client/linux/handler/exception_handler.cc
  SOURCES += $$BREAKPAD_PATH/src/client/linux/handler/minidump_descriptor.cc
  SOURCES += $$BREAKPAD_PATH/src/client/linux/minidump_writer/minidump_writer.cc
  SOURCES += $$BREAKPAD_PATH/src/client/linux/minidump_writer/linux_dumper.cc
  SOURCES += $$BREAKPAD_PATH/src/client/linux/minidump_writer/linux_ptrace_dumper.cc
  SOURCES += $$BREAKPAD_PATH/src/client/linux/log/log.cc
  SOURCES += $$BREAKPAD_PATH/src/client/minidump_file_writer.cc
  SOURCES += $$BREAKPAD_PATH/src/common/linux/linux_libc_support.cc
  SOURCES += $$BREAKPAD_PATH/src/common/linux/file_id.cc
  SOURCES += $$BREAKPAD_PATH/src/common/linux/memory_mapped_file.cc
  SOURCES += $$BREAKPAD_PATH/src/common/linux/safe_readlink.cc
  SOURCES += $$BREAKPAD_PATH/src/common/linux/guid_creator.cc
  SOURCES += $$BREAKPAD_PATH/src/common/linux/elfutils.cc
  SOURCES += $$BREAKPAD_PATH/src/common/string_conversion.cc
  SOURCES += $$BREAKPAD_PATH/src/common/convert_UTF.c
  #breakpad app need debug info inside binaries
  QMAKE_CXXFLAGS+=-g
}

OSWIN {
  BREAKPAD_PATH=q:/Applications/breakpad-qt/third-party/latest-breakpad
  INCLUDEPATH += $$BREAKPAD_PATH/src
  HEADERS += $$BREAKPAD_PATH/src/common/windows/string_utils-inl.h
  HEADERS += $$BREAKPAD_PATH/src/common/windows/guid_string.h
  HEADERS += $$BREAKPAD_PATH/src/client/windows/handler/exception_handler.h
  HEADERS += $$BREAKPAD_PATH/src/client/windows/common/ipc_protocol.h
  HEADERS += $$BREAKPAD_PATH/src/google_breakpad/common/minidump_format.h 
  HEADERS += $$BREAKPAD_PATH/src/google_breakpad/common/breakpad_types.h 
  HEADERS += $$BREAKPAD_PATH/src/client/windows/crash_generation/crash_generation_client.h 
  HEADERS += $$BREAKPAD_PATH/src/processor/scoped_ptr.h 

  SOURCES += $$BREAKPAD_PATH/src/client/windows/handler/exception_handler.cc
  SOURCES += $$BREAKPAD_PATH/src/common/windows/string_utils.cc
  SOURCES += $$BREAKPAD_PATH/src/common/windows/guid_string.cc
  SOURCES += $$BREAKPAD_PATH/src/client/windows/crash_generation/crash_generation_client.cc 
}

And this is the minimal implementation of your new CrashHandler. My ownd crash handler is much more sophisticate to perform reporting, sending crash dump etc. But for purposes of this article this is what you need (And also I don’t want to share my whole know-how here ;-)))

CrashHandler.h

#pragma once
#include <QtCore/QString>

namespace Atomix
{
	class CrashHandlerPrivate;
	class CrashHandler
	{
	public:
		static CrashHandler* instance();
    void Init(const QString&  reportPath);

		void setReportCrashesToSystem(bool report);
		bool writeMinidump();

	private:
		CrashHandler();
		~CrashHandler();
		Q_DISABLE_COPY(CrashHandler)
		CrashHandlerPrivate* d;
	};
}

CrashHandler.cpp

#include "CrashHandler.h"
#include <QtCore/QDir>
#include <QtCore/QProcess>
#include <QtCore/QCoreApplication>
#include <QString>

#if defined(Q_OS_MAC)
#include "client/mac/handler/exception_handler.h"
#elif defined(Q_OS_LINUX)
#include "client/linux/handler/exception_handler.h"
#elif defined(Q_OS_WIN32)
#include "client/windows/handler/exception_handler.h"
#endif

namespace Atomix
{
	/************************************************************************/
	/* CrashHandlerPrivate                                                  */
	/************************************************************************/
	class CrashHandlerPrivate
	{
	public:
		CrashHandlerPrivate()
		{
			pHandler = NULL;
		}

		~CrashHandlerPrivate()
		{
			delete pHandler;
		}

		void InitCrashHandler(const QString& dumpPath);
		static google_breakpad::ExceptionHandler* pHandler;
		static bool bReportCrashesToSystem;
	};

	google_breakpad::ExceptionHandler* CrashHandlerPrivate::pHandler = NULL;
	bool CrashHandlerPrivate::bReportCrashesToSystem = false;

	/************************************************************************/
	/* DumpCallback                                                         */
	/************************************************************************/
#if defined(Q_OS_WIN32)
	bool DumpCallback(const wchar_t* _dump_dir,const wchar_t* _minidump_id,void* context,EXCEPTION_POINTERS* exinfo,MDRawAssertionInfo* assertion,bool success)
#elif defined(Q_OS_LINUX)
	bool DumpCallback(const google_breakpad::MinidumpDescriptor &md,void *context, bool success)
#elif defined(Q_OS_MAC)
	bool DumpCallback(const char* _dump_dir,const char* _minidump_id,void *context, bool success)
#endif
	{
		Q_UNUSED(context);
#if defined(Q_OS_WIN32)
		Q_UNUSED(_dump_dir);
		Q_UNUSED(_minidump_id);
		Q_UNUSED(assertion);
		Q_UNUSED(exinfo);
#endif
		qDebug("BreakpadQt crash");

		/*
		NO STACK USE, NO HEAP USE THERE !!!
		Creating QString's, using qDebug, etc. - everything is crash-unfriendly.
		*/
		return CrashHandlerPrivate::bReportCrashesToSystem ? success : true;
	}

	void CrashHandlerPrivate::InitCrashHandler(const QString& dumpPath)
	{
		if ( pHandler != NULL )
			return;

#if defined(Q_OS_WIN32)
		std::wstring pathAsStr = (const wchar_t*)dumpPath.utf16();
		pHandler = new google_breakpad::ExceptionHandler(
			pathAsStr,
			/*FilterCallback*/ 0,
			DumpCallback,
			/*context*/
			0,
			true
			);
#elif defined(Q_OS_LINUX)
		std::string pathAsStr = dumpPath.toStdString();
		google_breakpad::MinidumpDescriptor md(pathAsStr);
		pHandler = new google_breakpad::ExceptionHandler(
			md,
			/*FilterCallback*/ 0,
			DumpCallback,
			/*context*/ 0,
			true,
			-1
			);
#elif defined(Q_OS_MAC)
		std::string pathAsStr = dumpPath.toStdString();
		pHandler = new google_breakpad::ExceptionHandler(
			pathAsStr,
			/*FilterCallback*/ 0,
			DumpCallback,
			/*context*/
			0,
			true,
			NULL
			);
#endif
	}

	/************************************************************************/
	/* CrashHandler                                                         */
	/************************************************************************/
	CrashHandler* CrashHandler::instance()
	{
		static CrashHandler globalHandler;
		return &globalHandler;
	}

	CrashHandler::CrashHandler()
	{
		d = new CrashHandlerPrivate();
	}

	CrashHandler::~CrashHandler()
	{
		delete d;
	}

	void CrashHandler::setReportCrashesToSystem(bool report)
	{
		d->bReportCrashesToSystem = report;
	}

	bool CrashHandler::writeMinidump()
	{
		bool res = d->pHandler->WriteMinidump();
		if (res) {
			qDebug("BreakpadQt: writeMinidump() successed.");
		} else {
			qWarning("BreakpadQt: writeMinidump() failed.");
		}
		return res;
	}

	void CrashHandler::Init( const QString& reportPath )
	{
		d->InitCrashHandler(reportPath);
	}
}

Main.cpp

And here is how to use it:

#include "axCore/axCrashHandler/CrashHandler.h"
#include <QDebug>
#include <QCoreApplication>
#include <iostream>

int buggyFunc()
{
	delete reinterpret_cast<QString*>(0xFEE1DEAD);
	return 0;
}

int main(int argc, char * argv[])
{
	qDebug() << "App start";
	QCoreApplication app(argc, argv);

#if defined(Q_OS_WIN32)
	Atomix::CrashHandler::instance()->Init("c:\\dump");
#elif defined(Q_OS_LINUX)
	Atomix::CrashHandler::instance()->Init("/Users/dev/dump");
#elif defined(Q_OS_MAC)
	Atomix::CrashHandler::instance()->Init("/Users/User/dump");
#endif
	
	qDebug() << "CrashHandlerSet";
	buggyFunc();
	return 0;
}

QWidget: Must construct a QApplication before a QPaintDevice

Today I encounter this error after applying macdeployqt to my application. There is a lot of questions about this topic on google but not very answers.

The most important thing is setup following variable.

export DYLD_PRINT_LIBRARIES=1 

Using this you will se which libraries are loaded during execution of your application. The interesting thing is, that I see loading a libraries from their original directories instead of Application.app. But when I temporary moved these libraries from their location, libraries begin to load from then bundle location.

The reason for my “QWidget: Must construct a QApplication before a QPaintDevice” was executing an older version of QTitanRibbon from shared libraries path instead of the newest one compiled directly to /usr/lib.
The second reason for this issue was caused by another path in “export DYLD_LIBRARY_PATH”. It seems that application search for libraries first in DYLD_LIBRARY_PATH and after in paths from their inner records.

To turn off this debug output, use:

unset DYLD_PRINT_LIBRARIES

How to create nice MacOS DMG installer

In this article I will show how to create DMG installer in reusable way. The most of things and ideas presented here are ideas from several articles mentioned on the end of this article.

1) As first step we need to create our DMG template with link to Applications.
2) Next we have to modify the visual representation of DMG file.
3) Next we store created DMG as template
4) Now mount copied template as directory, fill DMG with real files
5) Detach mounted DMG, pack DMG
6) Distribute app 😉

More detailed info about all these steps you will find in articles mentioned below. Here is a short script I wrote for three-phase deploying (1) create template, 2) pack template, 3)fill template with real data and compress it)

#! /bin/bash

TEMPLATE_DMG=atomix-development-template.dmg
VOLUME_NAME="Atomix Development"
APPLICATION_FILE_NAME="AtomixDevelopment.app"

if [ "$1" = "create-template" ]
then
  echo Create DMG template
  mkdir template
  cd template
  mkdir $APPLICATION_FILE_NAME
  touch $APPLICATION_FILE_NAME/fake
  ln -s /Applications/ Applications
  cd ..
  rm $TEMPLATE_DMG
  rm $TEMPLATE_DMG.bz2
  hdiutil create -fs HFSX -layout SPUD -size 100m "$TEMPLATE_DMG" -srcfolder template -format UDRW -volname "$VOLUME_NAME" -quiet
  rm -rf template
elif [ "$1" = "zip-template" ]
then
  echo Zipping DMG template
  bzip2 "$TEMPLATE_DMG"
elif [ "$1" = "create-dmg" ]
then
  echo Creating DMG file from template and data

  #unzip template
  rm $TEMPLATE_DMG
  bunzip2 -k $TEMPLATE_DMG.bz2

  #create pack path and attach DMG
  PACK_PATH=`pwd`/dmg-pack
  mkdir $PACK_PATH
  hdiutil attach "$TEMPLATE_DMG" -noautoopen -quiet -mountpoint "$PACK_PATH"

  #copy new content to DMG
  cp -r "$2" "$PACK_PATH"
  rm $PACK_PATH/$APPLICATION_FILE_NAME/fake

  #detach DMG, remove mount path
  hdiutil detach "$PACK_PATH" -force -quiet
  rm -rf $PACK_PATH

  #compress DMG
  hdiutil convert -format UDZO -o tmp-packer-output.dmg "$TEMPLATE_DMG" -imagekey zlib-level=9
  rm ./$TEMPLATE_DMG
  mv ./tmp-packer-output.dmg $TEMPLATE_DMG

  #rm $TEMPLATE_DMG
else
  echo Missing parameter
  echo "create-dmg [create|zip-template|create-dmg Path/Application.app]"
fi

So, it looks like that DMG file for ORM Designer2 is ready 😉

Another tools to create DMG package

External links

http://codevarium.gameka.com.br/how-to-create-your-own-beautiful-dmg-files/
http://el-tramo.be/guides/fancy-dmg/
http://stackoverflow.com/questions/96882/how-do-i-create-a-nice-looking-dmg-for-mac-os-x-using-command-line-tools
http://stackoverflow.com/questions/8680132/creating-nice-dmg-installer-for-mac-os-x
http://stackoverflow.com/questions/2104364/how-to-install-a-qt-application-on-a-customers-system

How to deploy Qt application on MacOS – part II

In first part of this article I introduced a manual way how to deploy MacOS application. Because doing all these stuff manually was a lot of hand work, Qt introduced small tool called macdeployqt.

This utility looks very good, but also have some drawbacks. As first I will show you how utility works:

macdeployqt Application.app

That’s all ;-). This command should do all the hard work for you. But in some cases it didn’t.

ERROR: no file at "/usr/lib/libboost_iostreams.dylib"
ERROR: no file at "/usr/lib/libboost_filesystem.dylib"
ERROR: no file at "/usr/lib/libboost_system.dylib"
ERROR: no file at "/usr/lib/libboost_thread.dylib"
ERROR: no file at "/usr/lib/libboost_date_time.dylib"
ERROR: no file at "/usr/lib/libboost_regex.dylib"
ERROR: no file at "/usr/lib/libboost_chrono.dylib"
ERROR: no file at "/usr/lib/libyaml-cpp.0.2.dylib"

The problem is that libraries mentioned in the previous list doesn’t has included absolute path. If you inspecit this library, you will se this:

otool -L libboost_regex.dylib
libboost_regex.dylib:
	libboost_regex.dylib (compatibility version 0.0.0, current version 0.0.0)
	/usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 52.0.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)

And this is the problem. Inner path in library is only filename without full path, so when we link library with our executable, our executable also has only relative path. And when macdeployqt try to find this library, automatically try to search in /usr/lib.

One possible solution is copy libraries to /usr/lib. Second solution is update paths in libraries to absolute paths. The third way is fix library linker to include full paths in libraries.

I found out that the simplies way how to fix it is call install_name_tool after library compilation and fix the path (don’t forget to update library paths inside other library paths!)

  install_name_tool -id "$SHL_PATH/boost/lib/libboost_chrono.dylib" $SHL_PATH/boost/lib/libboost_chrono.dylib
  install_name_tool -id "$SHL_PATH/boost/lib/libboost_date_time.dylib" $SHL_PATH/boost/lib/libboost_date_time.dylib
  ...
  install_name_tool -change "libboost_system.dylib" "$SHL_PATH/boost/lib/libboost_system.dylib" $SHL_PATH/boost/lib/libboost_chrono.dylib
  install_name_tool -change "libboost_system.dylib" "$SHL_PATH/boost/lib/libboost_system.dylib" $SHL_PATH/boost/lib/libboost_filesystem.dylib

So, now when we have updated all libraries, we can try to create application bundle again

macdeployqt AtomixDevelopment.app/

And if we did everything correctly, now we should have a working app ;-).

Troubleshooting

In some cases you can get error “Permission defined”, “Bad file descriptor” or other similar errors when using macdeployqt.

ERROR: "install_name_tool: can't open input file: OrmDesigner2.app/Contents/Frameworks//libssl.1.0.0.dylib for writing (Permission denied)
install_name_tool: can't lseek to offset: 0 in file: OrmDesigner2.app/Contents/Frameworks//libssl.1.0.0.dylib for writing (Bad file descriptor)
install_name_tool: can't write new headers in file: OrmDesigner2.app/Contents/Frameworks//libssl.1.0.0.dylib (Bad file descriptor)
install_name_tool: can't close written on input file: OrmDesigner2.app/Contents/Frameworks//libssl.1.0.0.dylib (Bad file descriptor)

The reason is insufficient permissions for modifying copied libraries. Simple execute

sudo macdeployqt

and everything should work ok. Another workaround for this issue is using newer version of Qt. I figured out that on MacOS I wrongly use Qt 4.7.4. When I correct this to version Qt 4.8.2 this issue was solved.

DMG file

If you want to create simply DMG file, simply call macdeployqt with param –dmg. But this dmg doesn’t look so good:

This will need more investigation how to make DMG files nicer ;-).

Notes

  • Everytime you run macdeployqt run it on clean folder. The best way is to call rm -rf Application, compile app and than run macdeployqt

Notes to bug in macdeployqt

The problem is caused by invalid permissions. Copied libraries have the same permissions like in the source location. There are three different solutions:

  1. Use sudo and execute macdeployqt with root permissions
  2. Update external libraries permissions to rw, so also copies will have a correct permissions
  3. Update macdeployqt script. In file shared.cpp on line 91 is method copyFilePrintStatus. Here is a necessary to update copied files permissions to correct rw

In my case the problem was in file liblzma.5.dylib

Frameworks/liblzma.5.dylib is not writable (Permission denied)

Orinal library is located in path:

/usr/local/Cellar/xz/5.0.4/lib

How to deploy Qt application on MacOS – part I

As I wrote yesterday, I found the way how to proceed deployment on the Linux system. Today I have to manage it the same on the MacOS systems.

Note: the simplest way is introduced in the second part of this article.

The first usefull command for tracking dependencies between your application and other shared libraries is otool:

#path to executable, not .App directory!
otool -L Application.app/Contents/MacOS/AppExecutable

Now we can see paths to our libraries. The bad news is that MacOS doesn’t have any -RPATH and $ORIGIN. The good news is utility install_name_tool and variable @executable_path which works similar like $ORIGIN.
Note: For newer versions of MacOS there are more variables like @executable_path. On version 10.4 was introduced @loader_path and on version 10.5 apple introduce @rpath variable.

The step-by-step solution

As first step it’s necessary to upload all dependent libraries directly to the Application.app/Contents/MacOS/. For list all required files use otool:

cd Application.app/Contents/MacOS
otool -L AppExecutable

When we have copied all libraries, we need to change paths for and in all libraries.

Here is example how to set libxml2 library. As first step we will check how is library referred.

$user otool -L AtomixDevelopment

AtomixDevelopment:
	libboost_iostreams.dylib (compatibility version 0.0.0, current version 0.0.0)
	libboost_filesystem.dylib (compatibility version 0.0.0, current version 0.0.0)
	/Users/User/dev/SharedLibraries/libxml/lib/libxml2.2.dylib (compatibility version 10.0.0, current version 10.8.0)
	/Users/User/dev/SharedLibraries/libxslt/lib/libxslt.1.dylib (compatibility version 3.0.0, current version 3.26.0)
	/Users/User/dev/SharedLibraries/libxslt/lib/libexslt.0.dylib (compatibility version 9.0.0, current version 9.15.0)

And Library:

User$ otool -L libxml2.2.dylib
libxml2.2.dylib:
	/Users/User/dev/SharedLibraries/libxml/lib/libxml2.2.dylib (compatibility version 10.0.0, current version 10.8.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
	/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
	/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)

When we checked libxml2.2.dylib library with otool, we can see that library has hardocoded our development path. This isn’t good ;-). So we now change this value to path relative to the executable and again verify this inner path.

install_name_tool -id "@loader_path/libxml2.2.dylib" libxml2.2.dylib
otool -L libxml2.2.dylib
libxml2.2.dylib:
	@loader_path/libxml2.2.dylib (compatibility version 10.0.0, current version 10.8.0)
	/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 159.1.0)
	/usr/lib/libz.1.dylib (compatibility version 1.0.0, current version 1.2.5)
	/usr/lib/libiconv.2.dylib (compatibility version 7.0.0, current version 7.0.0)

As you can see, now is path (first line) set up to reffer @loader_path/libxml2.2.dylib. It’s correct now. As second step we have to update path in our executable, because also there is path hardcoded to our library repository.

install_name_tool -change "/Users/User/dev/ExternalLibraries/../SharedLibraries/libxml/lib/libxml2.2.dylib" "@loader_path/libxml2.2.dylib" AtomixDevelopment

otool -L AtomixDevelopment
AtomixDevelopment:
	libboost_iostreams.dylib (compatibility version 0.0.0, current version 0.0.0)
	libboost_filesystem.dylib (compatibility version 0.0.0, current version 0.0.0)
	@loader_path/libxml2.2.dylib (compatibility version 10.0.0, current version 10.8.0)
        ...

So, now we have changed also path in our executable. And now we need to do all these steps for all libraries….. ;-).

 

Notes

To follow the MacOS application convention it’s better to place libraries to folder Application.app/Contents/Frameworks. Here is a directory schema for Qt example application plugAndPaint.

External links

Why is compiled Linux executable so large?

Today I encountered another issue when compiling my Qt application http://www.orm-designer.com/ under Linux. The size of final executable was about 400MB.
The first thought was that there is some static libraries compiled to application. But the problem was elsewhere.

Unlike to Windows compilation where linker creates .exe and .pdb file, linux linker creates only one final file. So this file includes also lot of additional symbols useful for debugging and post-mortem bugs finding. But it is not so usefull for distribution your application. To strip all these symbols, use following command

strip --strip-unneeded /path/to/application

Strip command have a lot of switches, but after a short testing it seems that the results are the same. Maybe I’m doing something wrong or simply my executable doesn’t contain any additional infos for stripping.

The most easiest way how to deploy Qt linux Application

I found solution also for the problem with executing application form in-app directory!!, Look to text bellow.
Warning: This solution has one big disadvantage ;-(. It’s necessary to execute application only from app-dir. In other cases system doesn’t find required libraries.
The solution could be run patchelf on the client computer with final installation path

Today I start working on deploying my http://www.orm-designer.com/ Qt application on linux platform.

I read a lot of articles about deploying applications but all of the suggested methods was impractical, too complex or unusable. Here is a list of them:

  • Using qt.conf in Qt resource or application dir. Usefull for Qt libraries, but not third party libraries
  • Installing Qt and third-party libraries in one of the system library path. This method need root permission for app users
  • Using startup script for setting correct LD_LIBRARY_PATH. Really? It’s too complicated for maintenance and users
  • Using -rpath to specify predetermined path. This method requires the users have this directory and have the full access to this directory

Another solution, the simplest one

I’m not sure why this solution isn’t mentioned in Qt forums or anywhere else. Maybe this solution isn’t so clean and perfect for Linux gurus, but it’s perfect for me.

The solution is in simply changing the search path in already compiled application to “.” (current directory) current application location by using $ORIGIN variable, so application will search all libraries in application directory first. It’s simple, it’s clean. All libraries distributed with application will be isolated in application directory and app-loader will not load any other library by mistake.

Updated: The keyword $ORIGIN do the trick, that you can run application from anywhere on your computer. $ORIGIN is absolute path to your executable.

And how to do this change? As first, you need to download small utility called PatchELF. With this application you can change -rpath in app to anything you need.

patchelf --set-rpath '$ORIGIN' ./ApplicationExecutable

And it is. Now you can copy all required libraries to directory with your application and everything will works correctly.

Installation

Download Ubuntu 32/64bit package from site http://hydra.nixos.org/release/patchelf/patchelf-0.6

Note

Maybe there is a way how to set rpath directly in the linking proces by using -rpath linker param. But I didn’t manage it to work.

If you want to display list of shared libraries used by your application, use this command:

ldd ./ApplicationExecutable

External links

Problem with Qt 4.8.2 QWebkit compilation under Ubuntu

The error message is following:

In file included from /usr/include/glib-2.0/glib/gasyncqueue.h:34:0,
                 from /usr/include/glib-2.0/glib.h:34,
                 from /usr/include/glib-2.0/gobject/gbinding.h:30,
                 from /usr/include/glib-2.0/glib-object.h:25,
                 from /usr/include/glib-2.0/gio/gioenums.h:30,
                 from /usr/include/glib-2.0/gio/giotypes.h:30,
                 from /usr/include/glib-2.0/gio/gio.h:28,
                 from wtf/gobject/GOwnPtr.cpp:24:
/usr/include/glib-2.0/glib/gthread.h:51:16: error: ‘union’ tag used in naming ‘struct _GMutex’ [-fpermissive]
/usr/include/glib-2.0/glib/gthread.h:58:7: error: ‘union’ tag used in naming ‘struct _GMutex’ [-fpermissive]
make[2]: *** [.obj/release-static/GOwnPtr.o] Error 1
make[2]: Leaving directory `/home/honza/qt-everywhere-opensource-src-4.8.2/src/3rdparty/webkit/Source/JavaScriptCore'
make[1]: *** [sub-JavaScriptCore-JavaScriptCore-pro-make_default-ordered] Error 2
make[1]: Leaving directory `/home/honza/qt-everywhere-opensource-src-4.8.2/src/3rdparty/webkit/Source'
make: *** [sub-webkit-make_default-ordered] Error 2

the reason is because of incompatibility between Qt 4.8.2 and glib-2.0.

The solution is described in this (not commited) bug fix:

https://codereview.qt-project.org/#patch,sidebyside,25069,1,src/3rdparty/webkit/Source/JavaScriptCore/wtf/gobject/GTypedefs.h

Simply edit following file

Qt/src/3rdparty/webkit/Source/JavaScriptCore/wtf/gobject/GTypedefs.h

and modify following line

55: typedef struct _GMutex GMutex;

to

55: typedef union _GMutex GMutex;

that’s it.