1. 程式人生 > >boost靜態連結庫和c++/clr不相容問題:未能載入檔案或程式集,不是有效的Win32應用程式。

boost靜態連結庫和c++/clr不相容問題:未能載入檔案或程式集,不是有效的Win32應用程式。

專案上遇到的問題:c++編寫的類使用託管c++包裝成dll提供給c#專案使用。c++需要使用boost,clr,專案目標平臺都是win32/x86。開發環境win10 x64系統,vs2013,.Net Framework 4.0,boost 1.55。

【問題表現】c#程式在win10 x86/x64正常執行,但在win7 x86/x64執行時發生異常:
Additional information: Could not load file or assembly “LicenseClientDLL_D.dll” or one of its dependencies. is not a valid Win32 application. (Exception from HRESULT:0x800700C1)
這裡寫圖片描述

【原因】boost庫預設使用靜態連結方式,但boost靜態連結庫和C++/clr不相容,所以要換成動態連結。
stackoverflow上相關資料:
[引用]Too many comments in the net saying boost static libs and CLR are not compatible.
[引用]It was the boost libraries having some issue when compiled without /clr but linked to a program that uses /clr

【解決方案】c++使用boost動態連結庫。
1.在需要使用boost庫的專案的“前處理器定義”增加 BOOST_THREAD_DYN_LINK

(要求動態連結boost庫)
2.手動拷貝需要引用的boost庫檔案到專案路徑 libboost、boost開頭的lib檔案(BOOST_LIB_DIAGNOSTIC 該巨集可以檢視需要的boost庫)
3.重新編譯