Cómo solucionar GCC undefined reference a std::experimental::filesystem::...

Problema:

Quieres compilar un ejecutable usando GCC/G++ que usa funcionalidad de std::experimental::filesystem pero obtienes un mensaje de error como este durante la compilación / enlazado:

linker-error-stdc++fs.txt
test.cpp:(.text+0x33): undefined reference to `std::experimental::filesystem::v1::file_size(std::experimental::filesystem::v1::__cxx11::path const&)'
/tmp/ccSXeCVb.o: In function `std::experimental::filesystem::v1::__cxx11::path::path<char [9], std::experimental::filesystem::v1::__cxx11::path>(char const (&) [9])':
test.cpp:(.text._ZNSt12experimental10filesystem2v17__cxx114pathC2IA9_cS3_EERKT_[_ZNSt12experimental10filesystem2v17__cxx114pathC5IA9_cS3_EERKT_]+0x73): undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status

Solución

Necesitas enlazar la librería stdc++fs, es decir, añade -lstdc++fs a tu comando g++.

Por ejemplo, un comando funcional se ve así:

build-with-stdc++fs.sh
g++ -o myprogram main.cpp -lstdc++fs

Echa un vistazo a artículos similares por categoría: C/C++ Errores de GCC