c++ - 使用带有clang而不是gcc的boost::filesystem链接的程序
问题描述
我有一个非常简单的程序 using boost::filesystem
,取自图书馆的教程。
// fs_example.cpp
#include <boost/filesystem.hpp>
#include <iostream>
using namespace boost::filesystem;
int main() {
path p = current_path();
directory_iterator it{p};
while (it != directory_iterator()) {
std::cout << *it++ << '\n';
}
}
我正在使用以下极其简单的脚本构建它,它应该以正确的顺序向链接器提供正确的参数:
#!/bin/sh
set -u
${CXX} \
-Wall \
-Werror \
--std=c++14 \
-lboost_system \
-lboost_filesystem \
-o fs_example \
fs_example.cpp
使用env CXX=clang++ ./build.sh
,程序编译和链接
$ env CXX=clang++ ./build.sh
$ ls
build.sh* fs_example* fs_example.cpp
使用env CXX=g++ ./build.sh
,程序编译失败。
$ env CXX=g++ ./build.sh
/tmp/ccFcZ3W6.o: In function `__static_initialization_and_destruction_0(int, int)':
fs_example.cpp:(.text+0x1c1): undefined reference to `boost::system::generic_category()'
...
collect2: error: ld returned 1 exit status
为什么当 Clang 没有时 GCC 会产生链接时错误?boost::system
即使提供了参数,我该如何诊断为什么链接器无法在 GCC 路径中找到符号?
解决方案
您应该假设“一次性链接”,这意味着较低级别boost_system
必须出现在较高级别之后boost_filesystem
。
推荐阅读
- google-cloud-platform - 为什么我在 Dataproc 上的初始化操作脚本无法设置环境变量?
- r - Dplyr group_by 和将函数排列在一起不会将相同的值组合在一起
- python - 如何制作一个可以将间隔相加成嵌套数组的函数?
- python - 将另一个 python 文件中的对象作为列表导入
- arrays - 取 x 个整数并将它们放入数组中
- javascript - getElementsByName 而不是 getElementById
- ios - 创建一个包含它所依赖的框架的 iOS 静态库文件
- c# - 如何在 Core .NET 2.2 项目中启用 facebook 身份验证?
- python - 我开始使用 sqlalchemy。我在处理和粘贴示例代码时遇到操作错误
- javascript - 清理数据以防止 XSS