2014-10-19 17 views
0

我有這樣的片段:使用stdlib=libc++鏘不承認的std :: shared_ptr的用的libstdC++

main.cpp:4:8: error: no member named 'shared_ptr' in namespace 'std' 
    std::shared_ptr<int> p(new int); 
    ~~~~~^ 

它運作良好:

#include <memory> 
int main() { 
    std::shared_ptr<int> p(new int); 
} 

如果我clang++ -std=c++0x -stdlib=libstdc++ main.cpp編譯我得到這個錯誤代替。該++的libstdc的版本是6.0.9,編譯器是

$ clang++ --version 
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn) 
Target: x86_64-apple-darwin13.3.0 
Thread model: posix 

我看到了同樣的結果使用clang version 3.5.0 (tags/RELEASE_350/final)時。

我已經意識到,它的作品,如果我用tr1,但這似乎不太便攜式對我說:

#include <tr1/memory> 
int main() { 
    std::tr1::shared_ptr<int> p(new int); 
} 

那麼,是不是有可能使用std::shared_ptr鏗鏘和libstdC++?

+0

這取決於你的GCC/libstdC++版本。很可能你正在一個發佈GCC舊版本的平臺上。 – 2014-10-19 04:18:34

+1

正如我所猜測的,libstdC++ 6.0.9附帶GCC 4.2.1。您應該考慮更新GCC(即通過自制軟件)。我認爲您必須使用指向您的GCC 4.8安裝的'--with-gcc-toolchain'重新編譯clang。 – 2014-10-19 04:23:39

回答

1

Mac發佈了一個非常舊的GCC版本(4.2.1),它當然帶有一個非常老的libstdC++。該Getting Started頁面LLVM說:

If you intend to use Clang's C++ support, you may need to tell it how to find your C++ standard library headers. In general, Clang will detect the best version of libstdc++ headers available and use them - it will look both for system installations of libstdc++ as well as installations adjacent to Clang itself. If your configuration fits neither of these scenarios, you can use the --with-gcc-toolchain configure option to tell Clang where the gcc containing the desired libstdc++ is installed.

這將是做到這一點的最簡單的方法,只需將其指向您的現代GCC安裝。如果你不想重新編譯鐺,那麼你可以嘗試跟蹤該mailing list說明:

Yes, of course. Finding the standard library is no magic, you can simply add some -isystem, -L, and possibly -Wl,-rpath arguments to clang++ when using it. Use the -v option to see what clang is using by default, so you can just add the same with s/4.4/4.7/g. If clang puts one of its own directories first (before the gcc ones), be sure to specify it again yourself so it still ends up first. You can even try some flags like -nostdinc++ to clean up a bit (again, check with -v that the list makes sense).

-- Marc Glisse

請記住,如果你設法讓鐺++有新的libstdC++,那的libstdC++和libc的工作++是binary incompatible。這意味着任何編譯爲一個的庫(例如Boost)都必須與其他庫重新編譯。

相關問題