首页 文章

在MacOS X中使用c 11并编译Boost库的难题

提问于
浏览
14

我正在尝试编译一个广泛使用c 11标准的c项目 . 只有-std = c 11,一切都很顺利,直到我尝试使用unordered_map而MacOS在usr / include中的任何地方都没有unordered_map头文件 .

我做了一些研究,发现使用-stdlib = libc会修复它(不知道如何,如果包含文件在文件系统中没有任何地方,这对我来说似乎很神奇) . 它确实做到了 . 编译得很好,但链接器无法链接到我的程序也广泛使用的boost :: program_options . 没有-stdlib = libc,可以完美地提升链接,但是我失去了unordered_map .

我应该怎么做才能拥有Mac OS clang编译器的最新C 11功能,并且仍然可以链接到boost库(这是从这个mac上的源代码构建的)

ps:在我的arch linux框中一切正常

我的makefile:

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_tool.o foghorn :$(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 c -stdlib = libc -std = c 11 -Wall -g -c -o interleave.o interleave.cpp c -stdlib = libc -std = c 11 -Wall -g -c -o complement.o complement.cpp c -stdlib = libc -std = c 11 -Wall -g -c -o interval_utils.o interval_utils.cpp c -stdlib = libc -std = c 11 -Wall -g -c -o interval.o interval.cpp c -stdlib = libc -std = c 11 -Wall -g -c -o interval_utils_test_tool.o interval_utils_test_tool.cpp c -stdlib = libc -std = c 11 -Wall -g -o foghorn fastq.o fastq_reader.o main.o degenerate.o interleave . o complement.o interval_utils.o interval.o interval_utils_test_tool.o -lboost_program_options体系结构x86_64的未定义符号:“boost :: program_options :: to_internal(std :: __ 1 :: basic_string,std :: __ 1 :: allocato r> const&)“,引自:std :: __ 1 :: vector,std :: __ 1 :: allocator>,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 >>> const&)in main .o ... [裁剪输出以便于阅读]
ld:找不到架构x86_64 clang的符号:错误:链接器命令失败,退出代码为1(使用-v查看调用)make:* [foghorn]错误1

1 回答

  • 30

    经过大量的研究,我学到了以下内容:

    • MacOS X上的所有东西都是使用stdlibc构建的(包括你用自制软件安装的所有东西 - 这是我的情况)
      gcc 4.2附带的

    • stdlibc没有实现c 11的许多功能 . 例如unordered_map,to_string,stoi,...只有libc才能实现 .

    • libc由XCode安装在MacOS中 .

    结论:

    • 如果您使用的是c 11,则 have 使用-stdlib = libc .

    • 如果要使用boost或任何其他库,则必须使用-stdlib = libc重新编译它们 .

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

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

    如果您使用Homebrew,您可以使用它,它会为您做正确的事情:

    brew install boost --c++11
    

相关问题