我正在尝试编译一个广泛使用C++11标准的C++项目。只使用-std=C++11一切都很顺利,直到我尝试使用unordered_map,MacOS在usr/include中没有unordered_map头文件。
我做了一些研究,发现使用-stdlib=libc++可以修复它(不知道如何修复,如果include文件不在文件系统中,这对我来说就像魔法一样)。的确是这样。它编译得很好,但是链接器无法链接到我的程序也广泛使用的boost::program_options。如果没有-stdlib=libc++,可以很好地增强链接,但是我丢失了unordered_map。
我应该怎么做才能使Mac OS Clang++编译器具有最新的C++11特性,并且仍然能够链接到boost库(这些库是从这个Mac上的源代码构建的)
PS:在我的arch linux盒子里,所有的工作都很好
我的生成文件:
LIBS=-lboost_program_options CXXFLAGS=-stdlib=libc++-std=c++11-wall-g OBJ=fastq.o fastq_reader.o main.o degenerate.o interleave.o complement.o interval_utils.o interval.o interval_utils_test_tood.o
雾角:$(OBJ)$(link.cc)-O$@$^$(LIBS)
使用-stdlib=libc++的输出
$make C++-stdlib=libC++-std=C++11-wall-g-c-o fastq.o fastq.cpp C++-stdlib=libC++-std=C++11-wall-g-c-o fastq_reader.o fastq_reader.cpp C++-stdlib=libC++-std=C++11-wall-g-c-o main.o main.cpp C++-stdlib=libC++-std=C++11-wall-g-c-o degenerate.o degenerate.cpp体系结构X86_64的未定义符号:“boost::program_options::to_internal(Sd::__1::Basic_String,std::__1::allocator>Const&)”,引用自:std::__1::Vector,std::__1::allocator>,std::__1::allocator>;>;>;boost::program_options::to_internal,std::__1::allocator>;
(std::__1::vector,std::__1::allocator>,std::__1::allocator,std::__1::allocator>,std::__1::allocator>,std::__1::allocator>在Main.o中
... [clipped output for readability]
ld:找不到体系结构x86_64的符号clang:错误:链接器命令失败,退出代码%1(使用-v查看调用)make:*[foghorn]错误%1
编辑:现代macOS见下面的评论;C++11标志不再有效
经过大量调研,我了解到以下几点:
结论:
所以我从他们的网站下载了boost源代码,并使用以下命令行进行编译:
bootstrap.sh && b2 toolset=clang cxxflags="-stdlib=libc++" linkflags="-stdlib=libc++" install
如果你使用自制的,你可以使用它,它会为你做正确的事情:
brew install boost --c++11