当前位置:  开发笔记 > 编程语言 > 正文

使用-lstdc ++ fs选项后使用GCC6时的experimental :: filesystem链接器错误

如何解决《使用-lstdc++fs选项后使用GCC6时的experimental::filesystem链接器错误》经验,为你挑选了1个好方法。

在尝试使用当前的GCC在C++中使用一些新的/实验性的东西时,我正面临链接错误.似乎已经发布了类似的问题,但仍然出现错误.

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

TEST.CPP

#include
#include
#include
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.这里我使用FS-TS所需的新-lstdc ++ fs选项.我可以使用其他新东西,如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::allocator >, std::experimental::filesystem::v1::__cxx11::path>(std::__cxx11::basic_string, std::allocator > 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> Alexei Khleb..:

如GCC文档中所述:

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

因此,你必须把-lstdc++fstest.cpp在命令行上.

推荐阅读
ifx0448363
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有