Search code examples
c++filesystemsbazel

undefined reference to 'std::filesystem' using Bazel build


trying to pull and build Visqol following the instructions provided for Linux. I downloaded Bazel, everything seems fine. But when I try to execute with bazel build :visqol -c opt i get the following errors

bazel-out/k8-opt/bin/_objs/visqol/main.o:main.cc:function main: error: undefined reference to 'std::filesystem::__cxx11::path::_M_split_cmpts()'
bazel-out/k8-opt/bin/_objs/visqol/main.o:main.cc:function main: error: undefined reference to 'std::filesystem::status(std::filesystem::__cxx11::path const&)'
bazel-out/k8-opt/bin/_objs/visqol_lib/commandline_parser.o:commandline_parser.cc:function Visqol::VisqolCommandLineParser::FileExists(Visqol::FilePath const&): error: undefined reference to 'std::filesystem::__cxx11::path::_M_split_cmpts()'
bazel-out/k8-opt/bin/_objs/visqol_lib/commandline_parser.o:commandline_parser.cc:function Visqol::VisqolCommandLineParser::FileExists(Visqol::FilePath const&): error: undefined reference to 'std::filesystem::status(std::filesystem::__cxx11::path const&)'
bazel-out/k8-opt/bin/_objs/visqol_lib/commandline_parser.o:commandline_parser.cc:function Visqol::VisqolCommandLineParser::ReadFilesToCompare(Visqol::FilePath const&): error: undefined reference to 'std::filesystem::__cxx11::path::_M_split_cmpts()'
bazel-out/k8-opt/bin/_objs/visqol_lib/commandline_parser.o:commandline_parser.cc:function Visqol::VisqolCommandLineParser::BuildFilePairPaths(Visqol::CommandLineArgs const&): error: undefined reference to 'std::filesystem::__cxx11::path::_M_split_cmpts()'
bazel-out/k8-opt/bin/_objs/visqol_lib/commandline_parser.o:commandline_parser.cc:function Visqol::VisqolCommandLineParser::BuildFilePairPaths(Visqol::CommandLineArgs const&): error: undefined reference to 'std::filesystem::status(std::filesystem::__cxx11::path const&)'
bazel-out/k8-opt/bin/_objs/visqol_lib/commandline_parser.o:commandline_parser.cc:function Visqol::VisqolCommandLineParser::BuildFilePairPaths(Visqol::CommandLineArgs const&): error: undefined reference to 'std::filesystem::status(std::filesystem::__cxx11::path const&)'
bazel-out/k8-opt/bin/_objs/visqol_lib/commandline_parser.o:commandline_parser.cc:function Visqol::VisqolCommandLineParser::Parse(int, char**): error: undefined reference to 'std::filesystem::current_path[abi:cxx11]()'
bazel-out/k8-opt/bin/_objs/visqol_lib/commandline_parser.o:commandline_parser.cc:function Visqol::VisqolCommandLineParser::Parse(int, char**): error: undefined reference to 'std::filesystem::current_path[abi:cxx11]()'
collect2: error: ld returned 1 exit status

I've seen similar issues in c++ on the web but most of these are building with make commands and solve this by adding the -lstdc++fs or LDLIBS=-lstdc++fs options. However, those are unrecognized for Bazel

I'm on Debian 10.13 and my Bazel version is 5.3.1, my c++ version seems to be 8.3.0


Solution

  • In accordance with how to use std::filesystem on gcc 8?, change the line build --linkopt=-ldl in .bazelrc into:

    build --linkopt=-lstdc++fs -ldl
    

    You could also open a ticket in the Visgol repo and tell them about this problem and get a proper solution in the future releases of Visgol.