我有一个非常简单的程序,使用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会产生链接时错误?我该如何诊断为什么即使提供了参数,链接器也无法在GCC路径的boost::system
中找到符号?
答案 0 :(得分:3)
您应该假定“单程链接”,这意味着较低级别的boost_system
必须出现在较高级别的boost_filesystem
之后。