Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

undefined reference when using experimental/filesystem

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.

like image 453
The Wizard Alt Avatar asked Mar 13 '18 06:03

The Wizard Alt


1 Answers

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.

like image 155
Flopp Avatar answered Nov 10 '22 21:11

Flopp