0

I'm new to C++ so forgive me. I use CodeBlocks which i just installed last week. I need to iterate through every text file in a directory. I found this example online:

#include <filesystem>

for ( const auto &dirEntry : std::filesystem::directory_iterator("saved_decks"))
    myHelpFxns::growLogOutputLines("file=" + dirEntry, this->log_output);

Where I'm trying to loop through every text file in the "saved_decks" directory then log the file name.

When I run this, I get the message "error: std::filesystem has not been declared". As per suggestions, I went into CodeBlocks Compiler options and checked the box under compiler flags:

"Have g++ follow the c++ 17 ISO C++ language standard"

Now i changed my only lines of code pertaining to fileystem:

At top of .cpp file:

#include <filesystem>

Inside a method of the class defined in the .cpp file, just to see if it works:

std::filesystem::create_directories("/sandbox/a/b");

And i rebuilt my wxWidgets project. Now I'm getting the error from the builder:

C:\Program Files\CodeBlocks\MinGW\lib\gcc\x86_64-w64-mingw32\8.1.0\include\c++\bits\fs_path.h|237|error: no match for 'operator!=' (operand types are 'std::filesystem::__cxx11::path' and 'std::filesystem::__cxx11::path')|

And when I try to build the program, it opens the file fs_path.h and shows error on the line:

#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
  if (__p.is_absolute()
  || (__p.has_root_name() && __p.root_name() != root_name()))  <-- Error on this line
operator=(__p);
  else
{
  string_type __pathname;
  if (__p.has_root_directory
....

In order to get the compiler to work with my project, is there any easy fix here?

EDIT: Build Log

-------------- Build: Debug in MullSimple-2 (compiler: GNU GCC Compiler)---------------

g++.exe -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE -Winvalid-pch -include wx_pch.h -DWX_PRECOMP -Wall -g -D__WXDEBUG__ -std=c++17 -IC:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\include -IC:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\lib\gcc_dll\mswud -c "C:\Users\gmloo\OneDrive\Desktop\C++ Projects\MullSimple-2\MullSimple_2Main.cpp" -o obj\Debug\MullSimple_2Main.o
g++.exe -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE -Winvalid-pch -include wx_pch.h -DWX_PRECOMP -Wall -g -D__WXDEBUG__ -std=c++17 -IC:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\include -IC:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\lib\gcc_dll\mswud -c "C:\Users\gmloo\OneDrive\Desktop\C++ Projects\MullSimple-2\non-wx\data_vars.cpp" -o obj\Debug\non-wx\data_vars.o
g++.exe -pipe -mthreads -D__GNUWIN32__ -D__WXMSW__ -DWXUSINGDLL -DwxUSE_UNICODE -Winvalid-pch -include wx_pch.h -DWX_PRECOMP -Wall -g -D__WXDEBUG__ -std=c++17 -IC:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\include -IC:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\lib\gcc_dll\mswud -c "C:\Users\gmloo\OneDrive\Desktop\C++ Projects\MullSimple-2\non-wx\file_manager.cpp" -o obj\Debug\non-wx\file_manager.o
g++.exe -LC:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\lib\gcc_dll -o bin\Debug\MullSimple-2.exe obj\Debug\MullSimple_2App.o obj\Debug\MullSimple_2Main.o obj\Debug\non-wx\data_vars.o obj\Debug\non-wx\file_manager.o obj\Debug\non-wx\my_help_fxns.o  obj\Debug\resource.res -mthreads  -lwxmsw30ud_core -lwxbase30ud -lwxpngd -lwxzlibd -mwindows
In file included from C:/Program Files/CodeBlocks/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/filesystem:37,
                 from C:\Users\gmloo\OneDrive\Desktop\C++ Projects\MullSimple-2\MullSimple_2Main.cpp:19:
C:/Program Files/CodeBlocks/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h: In member function 'std::filesystem::__cxx11::path& std::filesystem::__cxx11::path::operator/=(const std::filesystem::__cxx11::path&)':
C:/Program Files/CodeBlocks/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:237:47: error: no match for 'operator!=' (operand types are 'std::filesystem::__cxx11::path' and 'std::filesystem::__cxx11::path')
    || (__p.has_root_name() && __p.root_name() != root_name()))
                               ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
In file included from C:/Program Files/CodeBlocks/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_algobase.h:64,
                 from C:/Program Files/CodeBlocks/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/char_traits.h:39,
                 from C:/Program Files/CodeBlocks/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/string:40,
                 from C:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\include/wx/stringimpl.h:66,
                 from C:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\include/wx/unichar.h:15,
                 from C:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\include/wx/strvararg.h:22,
                 from C:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\include/wx/string.h:46,
                 from C:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\include/wx/log.h:43,
                 from C:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\include/wx/msw/private.h:23,
                 from C:\Users\gmloo\OneDrive\Desktop\wxWidgets-3.0.5\include/wx/wxprec.h:44,
                 from ./wx_pch.h:14,
                 from <command-line>:
C:/Program Files/CodeBlocks/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_pair.h:456:5: note: candidate: 'template<class _T1, class _T2> constexpr bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)'
     operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
     ^~~~~~~~
C:/Program Files/CodeBlocks/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/stl_pair.h:456:5: note:   template argument deduction/substitution failed:
In file included from C:/Program Files/CodeBlocks/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/filesystem:37,
                 from C:\Users\gmloo\OneDrive\Desktop\C++ Projects\MullSimple-2\MullSimple_2Main.cpp:19:
C:/Program Files/CodeBlocks/MinGW/lib/gcc/x86_64-w64-mingw32/8.1.0/include/c++/bits/fs_path.h:237:60: note:   'std::filesystem::__cxx11::path' is not derived from 'const std::pair<_T1, _T2>'
    || (__p.has_root_name() && __p.root_name() != root_name()))

EDIT: linker settings?

I'm not sure what you mean by linking files, this is maybe the part in CodeBlocks where I can do it. What would I enter?

enter image description here

Geoff L
  • 765
  • 5
  • 22
  • ok so what does that mean exactly? – Geoff L Aug 14 '20 at 00:59
  • It means you should have C++17 support, and thus filesystem. Could you add the compiler command line that Code::blocks is generating? – user4581301 Aug 14 '20 at 01:00
  • The complete build output message would be helpful as well. – user4581301 Aug 14 '20 at 01:01
  • theres a lot more but i just put the part leading up to the first error – Geoff L Aug 14 '20 at 01:02
  • Not seeing the problem. Can you produce a really small program that has the same bad behaviour and post that? Turns out I don't have gcc 8.1 on my windows box and testing and it looks like testing this out on Linux will be a waste of time so I'll probably have to leave this to someone else. – user4581301 Aug 14 '20 at 01:15
  • @user4581301 thanks for your help but I think I'm going to find a diff approach this is beyond me – Geoff L Aug 14 '20 at 01:25
  • You can try out the answer below. I don't see how it would work, but hey. Dev tools are getting sneakier all the time. Looks like all you should have to do is paste `stdc++fs` into that add library window. – user4581301 Aug 14 '20 at 01:29

1 Answers1

0

The GCC error is actually confusing, but this is most likely be simply because you are missing -lstdc++fs to require linking the C++ fs lib. This is specific to GCC 7 and was changed in more recent version.

I'm not familiar with CodeBlocks and if you can instruct it to link to a library from the GUI, but if there is, you will need to link to stdc++fs. -lstdc++fs is the juxtaposition of -l and the library name, which asks the linker to link the library specified.

More information details in this post: experimental::filesystem linker error

Adrien Leravat
  • 2,731
  • 18
  • 32