天天看點

thread——MFC下Boost的線程編譯錯誤問題

在MFC中使用boost的多線程庫,可能會抛對外連結接錯誤:

__pRawDllMain already defined in ....

這是由于thread的源碼中葉有一個入口函數

extern BOOL (WINAPI * const _pRawDllMain)(HANDLE, DWORD, LPVOID)=&dll_callback;

解決方法是将

boost/libs/thread/src/win32/tss_pe.cpp

中的這句話注釋然後重新編譯即可解決。

網上看到另一種方法,經測試失敗,如果測試成功請告之

連結:http://www.juicydata.com/LinkingMfcAndBoostThread

原文如下:

Linking against Boost can sometimes be tricky, as a minor misconfiguration in your project / makefiles can cause the auto-link mechanism to operate differently than you expect. Here's one example of a failure during link:

libboost_thread-vc80-mt-1_40.lib(tss_pe.obj) : error LNK2005:
   __pRawDllMain already defined in mfcs80.lib(rawdllmainproxy.obj)
   Creating library c:\Work\MyLib\Release\MyLib.lib and object 
   c:\Work\MyLib\Release\MyLib.exp

.\Release\MyLib.dll : fatal error LNK1169: 
   one or more multiply defined symbols found
      

In this example, MFC already defines this symbol to do it's own setup during dll load or attach. The Boost::Thread library tries to do the same, with the laudable intent to initialize or teardown everything correctly. Only one actor can use this mechanism, so we'll have to modify one.

Option 1: Cause the boost thread library to be used dynamically. The preprocessor definition BOOST_ALL_DYN_LINK can be used to force all boost libraries to be used dynamically. Note that the define BOOST_LIB_DIAGNOSTIC can be used to get more info for debugging the build process.

Option 1 (continued): If you want to explicitly select libraries to link against dynamically, use preprocessor def's such as: BOOST_DATE_TIME_DYN_LINK, or BOOST_REGEX_DYN_LINK. Nota bene: For the Boost::Thread library, the define is incorrectly BOOST_THREAD_DYN_DLL instead of BOOST_THREAD_DYN_LINK. This bug was fixed around May 2010.

Option 2: Remove MFC. Do you really need MFC?

當然,最好是這裡的option2 開發前先思考一下,真的必須用MFC嗎?