提问者:小点点

在MacOS X和编译Boost库中使用C++11的难题


我正在尝试编译一个广泛使用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


共1个答案

匿名用户

编辑:现代macOS见下面的评论;C++11标志不再有效

经过大量调研,我了解到以下几点:

  • MacOS X上的所有东西都是使用stdlibc++构建的(包括您用自制程序安装的所有东西--这就是我的例子)
  • GCC4.2附带的stdlibc++没有实现C++11的许多特性。示例有unordered_map,to_string,stoi,。。。只有libc++可以。
  • libc++由Xcode安装在MacOS中。

结论:

  • 如果您使用的是C++11,则必须使用-stdlib=libc++。
  • 如果要使用boost或任何其他库,还必须使用-stdlib=libc++重新编译它们。

所以我从他们的网站下载了boost源代码,并使用以下命令行进行编译:

bootstrap.sh && b2 toolset=clang cxxflags="-stdlib=libc++" linkflags="-stdlib=libc++" install

如果你使用自制的,你可以使用它,它会为你做正确的事情:

brew install boost --c++11 

相关问题


MySQL Query : SELECT * FROM v9_ask_question WHERE 1=1 AND question regexp '(macos|x|编译|boost|库|中|c++11|难题)' ORDER BY qid DESC LIMIT 20
MySQL Error : Got error 'repetition-operator operand invalid' from regexp
MySQL Errno : 1139
Message : Got error 'repetition-operator operand invalid' from regexp
Need Help?