gpt4 book ai didi

c++ - 使用 -lstdc++fs 选项后使用 GCC6 时出现实验性::文件系统链接器错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:30:28 27 4
gpt4 key购买 nike

在尝试使用当前的 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;
}

I have built(debug version) the new gcc version 6.0.0 20151122 (experimental) (GCC) as g++-6.0.0. Here I am using new -lstdc++fs option which is required for FS TS. I am able to use the other new stuff like C++ concept.

~/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

The below provides detailed version information of GCC installed on machine.

~/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)

Tried to extract the libstdc++fs.a. From below commands I could see that below method does exist in dir.o object file. However still linker is throwing the undefined reference error.

~/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(

)

It seems that path has also been set correctly.

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

This is debug version.

~/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

更新

通过将 -lstd​​c++fs 放在命令的末尾,效果很好,正如 Galik 在评论中所建议的那样。不过我想知道你是怎么确定我使用的命令是错误的?。

从我所有的分析中,我无法从 GCC 中识别出任何可能指向这一点的线索。如果有人可以提供一些有关新链接器选项 (-lstdc++fs) 排序的详细信息,那就太好了。 GCC 文档中没有关于此链接器选项顺序的信息。

最佳答案

GCC documentation 中所写:

-llibrary
-l library
...
It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, ‘foo.o -lz bar.o’ searches library ‘z’ after file foo.o but before bar.o. If bar.o refers to functions in ‘z’, those functions may not be loaded.

因此你必须在命令行的test.cpp之后加上-lstdc++fs

关于c++ - 使用 -lstdc++fs 选项后使用 GCC6 时出现实验性::文件系统链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34585987/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com