我使用Xcode 4.5.2并且我想使用Boost,但是我遇到了一些问题。
在构建设置中,如果我选择libc ++(支持C ++ 11的LLVM C ++标准库),我将得到错误消息“Apple Mach-O Linker(ld)Error”。 就像这样:
Undefined symbols for architecture x86_64:
"boost::filesystem::path_traits::dispatch(boost::filesystem::directory_entry const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, std::__1::codecvt<wchar_t, char, __mbstate_t> const&)"
referenced from:
boost::filesystem::path::path<boost::filesystem::directory_entry>(boost::filesystem::directory_entry const&, boost::enable_if<boost::filesystem::path_traits::is_pathable<boost::decay<boost::filesystem::directory_entry>::type>, void>::type*) in test1 - inverted index.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
我包含这两个标题:
#include <boost/filesystem.hpp>
#include <boost/operators.hpp>
我还在Build Phases中添加了这些:
libboost_filesystem-mt.dylib
libboost_filesystem-mt.a
libboost_system-mt.dylib
libboost_system-mt.a
代码在这里:
#include <boost/filesystem.hpp>
#include <boost/operators.hpp>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
using namespace boost::filesystem;
std::map<std::string, std::set<int>> invertedIndex;
std::map<std::string, int> number;
bool check_char(const char in)
{
if((in>='A' && in<='Z') || (in>='a' && in<='z'))
return true;
else
return false;
}
int main() {
int con = 0;
std::string word;
path root("/Users/tomhu/Desktop/pro/data/");
std::string rootDirectory = root.native();
recursive_directory_iterator iter(root);
recursive_directory_iterator end;
for (; iter != end; ++iter)
{
if(is_regular_file(*iter))
{
std::string filename;
std::string directory(rootDirectory);
filename = iter->path().filename().native();
directory.append(filename);
std::ifstream fileIn(directory.c_str());
number[filename] = con;
if(!fileIn)
{
std::cerr << "File doesn't exist!" << std::endl;
exit(1);
}
while(fileIn>>word)
{
long po = 0;
if(!check_char(*(word.end()-1)))
word.pop_back();
transform(word.begin(),word.end(),word.begin(),::tolower);
if(word=="i")
word="I";
invertedIndex[word].insert(con);
}
}
}
std::cout << con << std::endl;
return 0;
}
!!!!!!!! 如果我在“Build Setting”中选择libstdc ++(GUN C ++标准库),我将不会收到有关Boost的任何错误消息,但我不能在C ++ 11 Standard中使用任何内容。
如何解决这些问题?
答案 0 :(得分:2)
见Why can't clang with libc++ in c++0x mode link this boost::program_options example? - 霍华德给出了一个很好的答案。那里出了什么问题。
以下是如何为clang + libc ++重新编译Boost:How to compile/link Boost with clang++/libc++?