undefined reference when using experimental/filesystem
Asked Answered
L

2

15

I am trying to compile a project with experimental::filesystem in visual studio code using code runner, however I can't get it to compile even in the terminal.

The code is as follows, a very simple test usage from the docs:

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

int main(){
    fs::create_directories("sandbox/a/b");
    cout << "done.";
    return 0;
}

Compiling with

g++ -std=c++17 $fullFileName && ./a.out -lstdc++fs

in the code runner config or with just

g++ -std=c++17 test.cpp -o test -lstdc++fs

in terminal doesn't compile or doesn't work in general.

The error it provides is:

/tmp/cco0g7Qf.o: In function `main':
test.cpp:(.text+0x24): undefined reference to `std::experimental::filesystem::v1::create_directories(std::experimental::filesystem::v1::__cxx11::path const&)'
/tmp/cco0g7Qf.o: In function `std::experimental::filesystem::v1::__cxx11::path::path<char [12], std::experimental::filesystem::v1::__cxx11::path>(char const (&) [12])':
test.cpp:(.text._ZNSt12experimental10filesystem2v17__cxx114pathC2IA12_cS3_EERKT_[_ZNSt12experimental10filesystem2v17__cxx114pathC5IA12_cS3_EERKT_]+0x64): undefined reference to `std::experimental::filesystem::v1::__cxx11::path::_M_split_cmpts()'
collect2: error: ld returned 1 exit status

Any help would be apreciated, I am running linux and have already checked to make sure my libstdc++ is up to date along with my gcc.

Lammond answered 13/3, 2018 at 6:8 Comment(0)
H
22

The following command line work great for me (GCC 7.2.0 on Ubuntu 17.10):

g++ -std=c++17 test.cpp -lstdc++fs

Note that when you omit the -lstdc++fs or put it before test.cpp in the command line, the linking will fail with the undefined reference error you've observed.

Heracles answered 13/3, 2018 at 7:40 Comment(6)
I'm an idiot, in the terminal I was running main.cpp which had other errors. In visual studio code, I was putting it after the ./a.out which is the file it runs, not where it compiles. placing it before the && fixed this issue.Lammond
Do you need the -lstdc++fs flag when using the c++17 filesystem library? As in, NOT the experimental one? I'm getting that same undefined reference error with std::filesystem::create_directories(std::filesystem::path const&)Bisset
@IronAttorney I guess this comes late, but anybody else see this answer of Jonathan Wakely.Incapacious
@IronAttorney I found I needed the linker flag for g++ < v10 but for g++ 10 it appears the filesystem library is fully included in libstdc++. This is the case whether I was compiling against C++17 or C++2a/20 with -std=g++XX or -std=c++XX, XX in {17, 2a, 20}Equip
Yeah, I've similarly noticed it works fine lately using latest GCC and Clang with C++ 17 and 20, no messing around with extra flagsBisset
To use the linker argument through VSCode on Ubuntu 18.04 I added it to the args entry in the .vscode/tasks.json file: { "tasks": [ { "type": "cppbuild", "label": "C/C++: g++ build active file", "command": "/usr/bin/g++", "args": [ "-fdiagnostics-color=always", "-Wall", "-Wextra", "-Werror", "-g", "${file}", "-lstdc++fs", "-o", "${fileDirname}/${fileBasenameNoExtension}"],.... Shortened on purposePhosphorylase
J
0

Option 1: The answer from @Flopp worked well for me.

Option 2: Remove ::experimental:: from all of my filesystem calls, then it compiled just fine in Linux under C++ 20 with g++ and clang++.

Joycejoycelin answered 2/11, 2022 at 13:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.