1. 程式人生 > >關於編譯報錯 error: cannot convert ‘const std::__cxx11::basic_string’ to ‘const char*’ 的處理

關於編譯報錯 error: cannot convert ‘const std::__cxx11::basic_string’ to ‘const char*’ 的處理

所以然

目前C++11標準開始普及,大家都開始預設支援或者使用c++11,例如GCC 5就開始預設啟用C++11特性。但是由於c++11相對於c++03,很多實現的資料結構都發生了改變,所以兩者並不能完全混用。 
預設情況下,GCC 5在編譯時會將std::string型別按c++11下std::__cxx11::basic_string<char> 來處理,這時如果你呼叫的庫在編譯時未啟用c++11特性則其中的std::string實際上是std::basic_string<char> ,這時如果將c++11下的string當作引數傳入非c++11的庫時,就會出現error: cannot convert 'const std::__cxx11::basic_string<char>' to 'const char*'

,或者未定義的方法引用(undefined reference)。

所以解決這個問題/類似問題的根本方法就是在編譯時不要混用c++11和c++03庫/程式碼。

如果你是在GCC5下使用了非GCC5編譯的庫,或者編譯時關閉了c++11特性的庫,那麼在編譯你的程式碼時請相應的關閉C++11特性:

  • 0x01 編譯選項中預定義巨集
<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">-D_GLIBCXX_USE_CXX11_ABI</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;">0</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
  • 0x02 程式碼中定義巨集
<code class="hljs cs has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-preprocessor" style="color: rgb(68, 68, 68); box-sizing: border-box;">#<span class="hljs-keyword" style="box-sizing: border-box;">define</span> _GLIBCXX_USE_CXX11_ABI 0</span></code><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul><ul class="pre-numbering" style="box-sizing: border-box; position: absolute; width: 50px; top: 0px; left: 0px; margin: 0px; padding: 6px 0px 40px; border-right-width: 1px; border-right-style: solid; border-right-color: rgb(221, 221, 221); list-style: none; text-align: right; background-color: rgb(238, 238, 238);"><li style="box-sizing: border-box; padding: 0px 5px;">1</li></ul>
  • 0x03 通過其他方法定義 _GLIBCXX_USE_CXX11_ABI 巨集的值為0 
    例如Qt中可以在Pro檔案中新增:
<code class="hljs fix has-numbering" style="display: block; padding: 0px; color: inherit; box-sizing: border-box; font-family: 'Source Code Pro', monospace;font-size:undefined; white-space: pre; border-radius: 0px; word-wrap: normal; background: transparent;"><span class="hljs-attribute" style="box-sizing: border-box;">DEFINES +</span>=<span class="hljs-string" style="color: rgb(0, 136, 0); box-sizing: border-box;"> _GLIBCXX_USE_CXX11_ABI=0</span></code>

但是,換個思路,可不可以用第三方容器解決版本問題呢? 其實用Boost 容器還是不錯。