OS X Troubleshooting

Using Qt debug libraries on Mac OS X 10.6 Snow Leopard

When using Qt frameworks, you might want to use the debug libraries instead of the release ones. This is done by setting the DYLD_IMAGE_SUFFIX environment variable to _debug.

  1. export DYLD_IMAGE_SUFFIX=_debug

Unfortunately, on Snow Leopard, this causes your application to crash. The reason is that Apple system frameworks crash when using the debug libraries. To be able to use Qt in debug mode, you must remove the debug version of the library (or overwrite it with the release version).

  1. $ sudo mv /usr/lib/libSystem.B_debug.dylib /usr/lib/libSystem.B_debug.dylib.backup
  2. $ sudo cp /usr/lib/libSystem.B.dylib /usr/lib/libSystem.B_debug.dylib // optional

Another solution is to change the install name information in the executable directly with install_name_tool (an utility shipped with Xcode). First of all, we can see a Qt program will link to several libraries and frameworks:

  1. $ otool -L tst_qfontmetrics.app/Contents/MacOS/tst_qfontmetrics
  2. tst_qfontmetrics.app/Contents/MacOS/tst_qfontmetrics:
  3.          /Users/username/qt/lib/QtTest.framework/Versions/4/QtTest (compatibility version 4.8.0, current version 4.8.0)
  4.          /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit (compatibility version 1.0.0, current version 275.0.0)
  5.          /System/Library/Frameworks/ApplicationServices.framework/Versions/A/ApplicationServices (compatibility version 1.0.0, current version 38.0.0)
  6.          /System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 37594.0.0)
  7.          /Users/username/qt/lib/QtGui.framework/Versions/4/QtGui (compatibility version 4.8.0, current version 4.8.0)
  8.          /Users/username/qt/lib/QtCore.framework/Versions/4/QtCore (compatibility version 4.8.0, current version 4.8.0)
  9.          /usr/lib/libstdc++.6.dylib (compatibility version 7.0.0, current version 7.9.0)
  10.          /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 625.0.0)
  11.          /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.0)

We need to change the paths of QtTest, QtGui and QtCore here, which means:

  1. $ install_name_tool -change /Users/username/qt/lib/QtCore.framework/Versions/4/QtTest /Users/username/qt/lib/QtCore.framework/Versions/4/QtTest_debug ./tst_qfontmetrics.app/Contents/MacOS/tst_qfontmetrics
  2. $ install_name_tool -change /Users/username/qt/lib/QtCore.framework/Versions/4/QtGui /Users/username/qt/lib/QtCore.framework/Versions/4/QtGui_debug ./tst_qfontmetrics.app/Contents/MacOS/tst_qfontmetrics
  3. $ install_name_tool -change /Users/username/qt/lib/QtCore.framework/Versions/4/QtCore /Users/username/qt/lib/QtCore.framework/Versions/4/QtCore_debug ./tst_qfontmetrics.app/Contents/MacOS/tst_qfontmetrics

In this way we can selectively use debug libraries for this program without affecting the system library. But changing this manually is cumbersome and error-prone, be sure to create a script for that if you are doing this constantly.

Categories: