使用-lstdc ++ fs选项

时间:2016-01-04 06:45:58

标签: c++ gcc c++17

在尝试使用当前的GCC在C ++中使用一些新的/实验性的东西时,我面临链接错误。似乎已经发布了类似的question,但仍然会收到错误。

有人可以解释一下我在这里做了什么/哪里错了吗?希望我已经提供了与此相关的所有详细信息。

  

TEST.CPP

#include<iostream>
#include<string>
#include<experimental/filesystem>
namespace fs = std::experimental::filesystem;

    int main(int argc, const char* argv[])
    {
        std::string s(argv[0]);
        fs::path p(s);
        std::cout << "p = " << p << std::endl;

        return 0;
    }
  

我已构建(调试版)新 gcc版本6.0.0 20151122 (实验性)   (GCC)为g ++ - 6.0.0。这里我使用的是新的-lstdc ++ fs选项   FS TS要求。我可以使用其他新东西,如 C ++概念

~/practice/gcc6$ g++-6.0.0 -v -g -lstdc++fs -o out test.cpp
Using built-in specs.
COLLECT_GCC=g++-6.0.0
COLLECT_LTO_WRAPPER=/usr/gcc_6_0/libexec/gcc/x86_64-linux-gnu/6.0.0/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../configure --build=x86_64-linux-gnu --prefix=/usr/gcc_6_0 --with-gmp=/usr/gcc_6_0 --with-mpfr=/usr/gcc_6_0 --with-mpc=/usr/gcc_6_0 --enable-languages=c,c++,fortran --disable-multilib --program-suffix=-6.0.0
Thread model: posix
gcc version 6.0.0 20151122 (experimental) (GCC) 
COLLECT_GCC_OPTIONS='-v' '-g' '-o' 'out' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/gcc_6_0/libexec/gcc/x86_64-linux-gnu/6.0.0/cc1plus -quiet -v -imultiarch x86_64-linux-gnu -D_GNU_SOURCE test.cpp -quiet -dumpbase test.cpp -mtune=generic -march=x86-64 -auxbase test -g -version -o /tmp/ccX3oTv2.s
GNU C++ (GCC) version 6.0.0 20151122 (experimental) (x86_64-linux-gnu)
    compiled by GNU C version 6.0.0 20151122 (experimental), GMP version 6.1.0, MPFR version 3.1.3, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
ignoring nonexistent directory "/usr/local/include/x86_64-linux-gnu"
ignoring nonexistent directory "/usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/../../../../x86_64-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/../../../../include/c++/6.0.0
 /usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/../../../../include/c++/6.0.0/x86_64-linux-gnu
 /usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/../../../../include/c++/6.0.0/backward
 /usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/include
 /usr/local/include
 /usr/gcc_6_0/include
 /usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/include-fixed
 /usr/include/x86_64-linux-gnu
 /usr/include
End of search list.
GNU C++14 (GCC) version 6.0.0 20151122 (experimental) (x86_64-linux-gnu)
    compiled by GNU C version 6.0.0 20151122 (experimental), GMP version 6.1.0, MPFR version 3.1.3, MPC version 1.0.3
GGC heuristics: --param ggc-min-expand=30 --param ggc-min-heapsize=4096
Compiler executable checksum: 14452760b2e0f1df03cbd137364317c8
COLLECT_GCC_OPTIONS='-v' '-g' '-o' 'out' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as -v --64 -o /tmp/ccMWw2R6.o /tmp/ccX3oTv2.s
GNU assembler version 2.25.1 (x86_64-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.25.1
COMPILER_PATH=/usr/gcc_6_0/libexec/gcc/x86_64-linux-gnu/6.0.0/:/usr/gcc_6_0/libexec/gcc/x86_64-linux-gnu/6.0.0/:/usr/gcc_6_0/libexec/gcc/x86_64-linux-gnu/:/usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/:/usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/
LIBRARY_PATH=/usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/:/usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/../../../../lib64/:/lib/x86_64-linux-gnu/:/lib/../lib64/:/usr/lib/x86_64-linux-gnu/:/usr/lib/../lib64/:/usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-g' '-o' 'out' '-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/gcc_6_0/libexec/gcc/x86_64-linux-gnu/6.0.0/collect2 -plugin /usr/gcc_6_0/libexec/gcc/x86_64-linux-gnu/6.0.0/liblto_plugin.so -plugin-opt=/usr/gcc_6_0/libexec/gcc/x86_64-linux-gnu/6.0.0/lto-wrapper -plugin-opt=-fresolution=/tmp/ccQOTKmb.res -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc -plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc --eh-frame-hdr -m elf_x86_64 -dynamic-linker /lib64/ld-linux-x86-64.so.2 -o out /usr/lib/x86_64-linux-gnu/crt1.o /usr/lib/x86_64-linux-gnu/crti.o /usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/crtbegin.o -L/usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0 -L/usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/../../../../lib64 -L/lib/x86_64-linux-gnu -L/lib/../lib64 -L/usr/lib/x86_64-linux-gnu -L/usr/lib/../lib64 -L/usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/../../.. -lstdc++fs /tmp/ccMWw2R6.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /usr/gcc_6_0/lib/gcc/x86_64-linux-gnu/6.0.0/crtend.o /usr/lib/x86_64-linux-gnu/crtn.o
/tmp/ccMWw2R6.o: In function `std::experimental::filesystem::v1::__cxx11::path::path<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::experimental::filesystem::v1::__cxx11::path>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
/usr/gcc_6_0/include/c++/6.0.0/experimental/bits/fs_path.h:167: undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status
  

下面提供了安装GCC的详细版本信息   机。

~/practice/gcc6$ g++-6.0.0 -v
Using built-in specs.
COLLECT_GCC=g++-6.0.0
COLLECT_LTO_WRAPPER=/usr/gcc_6_0/libexec/gcc/x86_64-linux-gnu/6.0.0/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../configure --build=x86_64-linux-gnu --prefix=/usr/gcc_6_0 --with-gmp=/usr/gcc_6_0 --with-mpfr=/usr/gcc_6_0 --with-mpc=/usr/gcc_6_0 --enable-languages=c,c++,fortran --disable-multilib --program-suffix=-6.0.0
Thread model: posix
gcc version 6.0.0 20151122 (experimental) (GCC)
  

试图提取libstdc ++ fs.a。从我可以看到的命令下面   以下方法确实存在于dir.o目标文件中。不过还是   链接器抛出未定义的引用错误。

~/practice/gcc6$ ar t /usr/gcc_6_0/lib64/libstdc++fs.a
dir.o
ops.o
path.o
cow-dir.o
cow-ops.o
cow-path.o

~/practice/gcc6$ nm dir.o |grep "split"
                 U _ZNSt12experimental10filesystem2v17__cxx114path14_M_split_cmptsEv

~/practice/gcc6$ nm dir.o |c++filt |grep "split"
                 U std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts(

  

似乎路径也已正确设置。

~/practice/gcc6$ which g++-6.0.0
/usr/gcc_6_o/bin/g++-6.0.0
  

这是调试版。

~/practice/gcc6$ file /usr/gcc_6_0/bin/g++-6.0.0
/usr/gcc_6_0/bin/g++-6.0.0: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, not stripped

更新

通过在命令末尾添加 - lstdc ++ fs ,可以在评论中按照 Galik 的建议正常工作。但是我想知道你是如何确定我使用的命令是错误的?

从我的所有分析中,我无法找出GCC可能指出的任何线索。如果有人可以提供有关新链接器选项(-lstdc ++ fs)排序的详细输入,那将是很棒的。 GCC文档中没有关于此链接器选项排序的信息。

1 个答案:

答案 0 :(得分:6)

正如GCC documentation所述:

  

-llibrary
  -l library
  ......   它在您编写此选项的命令中有所不同;该   链接器按顺序搜索和处理库和目标文件   它们是指定的。因此,'foo.o -lz bar.o'之后搜索库'z'   文件foo.o但在bar.o之前如果bar.o引用'z'中的函数,   这些功能可能无法加载。

因此,您必须在-lstdc++fs之后将test.cpp放在命令行上。