2015-11-12 30 views
4

我有一個嵌入式系統,並希望在這個系統中使用boost,但需要禁用異常,因爲我不想支付異常的代價。爲什麼boost :: shared_ptr無法編譯,如果BOOST_NO_EXCEPTIONS定義在user.hpp

升壓給一個user.hpp和可設置的微距拍攝選項BOOST_NO_EXCEPTIONSBOOST_NO_EXCEPTION_STD_NAMESPACE,但的boost :: shared_ptr的不能編譯(更準確地說,無法鏈接),如果這兩個宏定義。

shared_ptr_boost.cpp:(.text._ZN5boost6detail12shared_countC2IiEEPT_[_ZN5boost6detail12shared_countC5IiEEPT_]+0x7a): undefined reference to `boost::throw_exception(std::exception const&)' 
collect2: error: ld returned 1 exit status 

爲什麼boost會給宏選項,但不承諾用這些選項編譯?

回答

9

它可以編譯。

它只是不能鏈接。

這是因爲如果你定義BOOST_NO_EXCEPTIONS,你必須在某處提供boost::throw_exception(std::exception const&)的實現,以取代通常的錯誤提升設施。

必須通過評論讀取中throw_exception.hpp:

namespace boost 
{ 

#ifdef BOOST_NO_EXCEPTIONS 

void throw_exception(std::exception const & e); // user defined 

#else 

//[Not user defined --Dynguss] 
template<class E> inline void throw_exception(E const & e) 
{ 
    throw e; 
} 

#endif 

} // namespace boost 
+0

如果由用戶定義,在BOOST_NO_EXCEPTIONS被定義的情況下,這個函數是否有意義?誰會叫它? –

+0

如果庫需要失敗,庫會調用它。在未處理的特殊情況下......我猜是否合理取決於你留下的未處理錯誤以及如何實現它。 – sehe

-1

這是我的最終解決方案。

shared_ptr_boost.cpp:

#include <boost/shared_ptr.hpp> 
#include <stdio.h> 
#include <exception> 

namespace boost{ 
    void throw_exception(std::exception const &e){} 
} 
int main(){ 
    boost::shared_ptr<int> pName(new int(2)); 
    *pName += 3; 
    printf("name = %d\n", *pName); 
    return 0; 
} 

編譯命令:

arm-hisiv100-linux-uclibcgnueabi-g++ -I../boost_1_59_0/ -DBOOST_NO_EXCEPTIONS -DBOOST_NO_EXCEPTION_STD_NAMESPACE -fno-exceptions shared_ptr_boost.cpp

對於這樣一個小的測試程序,我得到約1.7K較小的可執行文件;這就是我不想爲異常支付的費用。

+0

「這樣一個小程序」的大小差異是無關緊要的。我的90MiB二進制文件可能還有1.7k。另外,你做了什麼?只是忽略例外?我不想使用該軟件。 – sehe

相關問題