glumesc

December 14, 2009

In which Boost rears it’s ugly head again…

Filed under: Development — Tags: , , , — steve @ 10:28 pm

And so on to my next problem, which again turned out to be due to my good friend Boost.

Background:

  • Visual Studio 2008
  • 32bit Project containing a managed CLR wrapper on some unmanaged C++ libraries
  • The unmanaged libraries reference a version of Boost compiled using Visual Studio 2005.
  • Messages at compilation time: Linking to lib file: boost_thread-vc90-mt-gd-1_39.lib
  • Error message:  fatal error LNK1104: cannot open file 'boost_thread-vc90-mt-gd-1_39.lib'

The more sharp-sighted amongst you will have quickly noticed the reference to “vc90″ in the lib file name, implying the libraries were compiled with Visual Studio 2008.  This is odd because the libraries are most definitely called boost*vc80*.lib.  Where was Visual Studio picking up this reference to vc90?  There was no reference to vc90 in the project properties, which implies that Visual Studio was dragging this out of thin air.

After a fair amount of investigation, my suspicions finally fell up the Boost includes.  The cause had to be something in the boost include files that wass forcing the assumption that the libraries had been compiled with VS2008.  A quick search revealed the following culprit: include/boost/config/auto_link.hpp

The auto_link.hpp code includes a section that searches for the _MSC_VER environment variable (which is set by Visual Studio). If BOOST_LIB_TOOLSET is not predefined, _MSC_VER is used to infer the relevant compiler version (vc80, vc90 etc).  The BOOST_LIB_TOOLSET property is then used to construct the file names for the libs to be linked.  Thus, since I was using VS2008, BOOST_LIB_TOOLSET defaulted to vc90.

The solution was to add the following to the Preprocessor Definitions: BOOST_LIB_TOOLSET=vc80

Once set, _MSC_VER was ignored and everything compiled as expected.

64bit application “is not a valid Win32 application”(!)

Filed under: Development — Tags: , , — steve @ 10:27 pm

This took me ages to find the solution: a 64bit application running on XP64 throws the following exception:

System.BadImageFormatException: is not a valid Win32 application

My immediate reaction was “well thanks for pointing out the obvious!”.  Unfortunately sarcasm didn’t resolve the fact that the application would not run.

The obvious things were checked:

  • The solution and all its projects were being compiled as x64
  • dumpbin reported all the DLLs (including third parties) as x64

I did manage to narrow the source of the problem down to one DLL: a managed CLR wrapper on some unmanaged C++ libraries.

After much hunting around I finally found the following post that indicated this might due to boost, upon which the C++ code depended.  This eventually led me to this post, which proved to be the solution. 

In summary: ensure boost libraries are linked dynamically rather than statically by specifying BOOST_ALL_DYN_LINK in the Preprocessor directives of the C++ project.

Theme: Silver is the New Black. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.