使用 LLVM 在 OSX 上将问题与 boost::p rogram_options 链接
Linking troubles with boost::program_options on OSX using LLVM
由于 Boost 1.49 的问题,我在C++程序中无法完成链接阶段。我已经切换到C++(-std=c++11 -libc=libc++
),它适用于另一段代码(也使用boost)。Boost是使用自制软件安装的:
brew install boost --universal --with-mpi --with-icu
麻烦从boost::program_options
开始.我收到这样的链接错误:
"boost::program_options::validate(boost::any&, std::__1::vector<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, std::__1::allocator<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > > > const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >*, int)", referenced from:
... etc. ...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
这有点奇怪,因为在使用的库上做一个 nm 会发现,符号似乎在那里:
nm -U /usr/local/lib/libboost_program_options-mt.dylib | grep validate
0000000000019880 - 01 0000 FUN __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISbIwSt11char_traitsIwESaIwEESaIS7_EEPSsi
0000000000019880 T __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISbIwSt11char_traitsIwESaIwEESaIS7_EEPSsi
00000000000199e0 - 01 0000 FUN __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISbIwSt11char_traitsIwESaIwEESaIS7_EEPbi
00000000000199e0 T __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISbIwSt11char_traitsIwESaIwEESaIS7_EEPbi
0000000000019930 T __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISsSaISsEEPSsi
0000000000019930 - 01 0000 FUN __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISsSaISsEEPSsi
0000000000019c70 - 01 0000 FUN __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISsSaISsEEPbi
0000000000019c70 T __ZN5boost15program_options8validateERNS_3anyERKSt6vectorISsSaISsEEPbi
我已经尝试过通过在安装之前相应地设置 CXX 和 CXX_FLAGS来哄骗自制软件使用 clang 而不是 gcc 编译 boost。不确定我成功了。
指针非常感谢。
您需要使用 clang 和 std11 标志重新编译 boost,libc++ 库与 OSX 中安装的 libstdc++ 不兼容二进制(在更改为 gpl3 之前是 gcc 的早期版本)。如果您的 clang 版本是 3.1 或更高版本,那么您可以使用(否则将 c++11 更改为 c++0x 对于早期版本)。
./bootstrap.sh
mkdir build
sudo ./bjam toolset=clang cxxflags="-std=c++0x -stdlib=libc++" variant=release link=static threading=multi runtime-link=shared --build-dir=Build --layout=system --without-mpi --without-python install --prefix=/usr/local
您当然可以根据需要更改其中任何一个,除了
toolset=clang cxxflags="-std=c++0x -stdlib=libc++"
这应该适合您。
分享我在 Mac OS X 10.8.5 上使用 xcode 5.0 提供的 clang 5.0.0 构建 Boost 1.54 的(中等痛苦)经验。如果你想要 C++11 功能,编译和链接非常重要 clang++
,而不是 clang
.
图示:采用以下简单程序:
#include <iostream>
#include <string>
int main(int argc, char *argv[]) {
std::string str = "OK";
std::cout << str << std::endl;
return 0;
}
可以使用以下命令构建:
clang++ -std=c++11 -stdlib=libc++ clangstr.cc -o clangstr
但是,如果您尝试以下操作:
clang -std=c++11 -stdlib=libc++ clangstr.cc -o clangstr
然后你得到链接器错误。请注意,clang
手册页说语言是通过-std=
选项选择的,但这显然是不够的。
教训是,我们必须告诉bjam
在编译具有 C++11 支持的 Boost 时显式使用clang++
。
在这篇非常有用的帖子之后,我将以下内容放入我的tools/build/v2/user-config.jam
:
using clang : 11
: "/usr/bin/clang++"
: <cxxflags>"-std=c++11 -stdlib=libc++ -ftemplate-depth=512" <linkflags>"-stdlib=libc++"
;
然后我运行了./b2 clean
,然后我用以下命令构建了 Boost:
mkdir -p build/clangstage/
./b2 -j8 --build-dir=build --stagedir=build/clangstage toolset=clang-11 define=BOOST_SYSTEM_NO_DEPRECATED variant=release threading=multi address-model=64 stage
这将构建具有多线程支持的 64 位静态和动态库。如果您需要不同的集合,请相应地更改上面的命令。
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- CMake-按正确顺序将项目与C运行时对象文件链接
- 从链接列表c++中删除一个项目
- 有根的二进制搜索树.保留与其父级的链接
- 读取文件的最后一行并输入到链接列表时出错
- 静态数据成员的问题-修复链接错误会导致编译器错误
- node-gyp 在 macOS 上未正确链接库
- 基于boost的程序的静态链接——zlib问题
- 无法链接 CMake 中的本地库
- 内联函数中具有内部链接的全局变量
- 链接阶段在Ubuntu上失败,但在MacOS上失败
- 使用gcc从静态链接的文件中查找可选符号
- 我可以将一个用clang c++11编译的对象与另一个用c++17编译的对象链接起来吗
- 将--whole archive链接器选项与CMake和具有其他库依赖项的库一起使用
- 下面是我为检测链接列表中的循环而制作的代码
- 有了gcc,是否可以链接库,但前提是它存在
- 使用C链接在函数内部创建C++模板
- Visual Studio mkl_link_tool.exe链接错误
- 尝试链接我的着色器时,我收到错误代码"error c5145 must write to gl_position"
- 有人能帮我处理这个链接列表吗?C++