Compilation errors for C++17 <filesystem> on MinGW - c++

I want to play around with the new filesystem library that's now apart of the C++17 standard, however I can't get things to compile.
Things I've already tried:
Updating MinGW to 8.2.0
Compiling with g++ -std=c++17 test.cpp -o test
Adding -lstdc++fs to the compilation (this does not work, I get the error c:/mingw/bin/../lib/gcc/mingw32/8.2.0/../../../../mingw32/bin/ld.exe: cannot find -lstdc++fs)
Using <filesystem> as well as <experimental\filesystem>
Here is my simple test code just to try and get things compiling:
#include <iostream>
#include <filesystem>
using namespace std;
int main(int argc, char* argv[]) {
return 0;
}
and compiling with g++ -std=c++17 test.cpp -o test
With this I get the error(s):
In file included from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\filesystem:37,
from test.cpp:2:
c:\mingw\lib\gcc\mingw32\8.2.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:\mingw\lib\gcc\mingw32\8.2.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:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iosfwd:40,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ios:38,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\ostream:38,
from c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\iostream:39,
from test.cpp:1:
... many more errors ...
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\fs_path.h:603:7: note: suggested alternative: 'string_view'
string_type __tmp;
^~~~~~~~~~~
string_view
c:\mingw\lib\gcc\mingw32\8.2.0\include\c++\bits\fs_path.h:604:45: error: '__tmp' was not declared in this scope
if (__str_codecvt_in(__first, __last, __tmp, __cvt))
Does anyone else have any suggestions? It seems like most people are solving this by adding -lstdc++fs to compilation, but like I said that doesn't work for me.
Thanks!

The issue is with the mingw and gcc/g++ 8 branch itself, not with the compiler flags or pre-processor directives. The bug is open here.
Try using stable mingw-w64-7.x releases with #include <experimental/filesystem> directive and -lstdc++fs -std=c++17 flags. This will work for now, or otherwise wait for v9.1.0.
On experimental channel you need to use std::experimental::filesystem instead of std::filesystem.
If you don't want to go with experimental features, switch to MSYS2. It has v10.2.0-6 of gcc available as of Jan 2021.

Related

Error compiling C++ source utilizing the Boost.Math library

I'm trying to use a couple of functions from the Boost Math library in some C++ code using the G++ compiler but I've been unsuccessful. This is on macOS.
I downloaded and extracted the Boost tar.gz from here and placed it into my source folder.
Within my C++ I've tried
#include "boost_1_63_0/boost/math/distributions/chi_squared.hpp" and
#include <boost_1_63_0/boost/math/distributions/chi_squared.hpp>.
The quotation version partially works but the chi_squared.hpp file includes fwd.hpp using the bracket (#include <...>) notation and that breaks my compilation with error In file included from main.cpp:9: ./boost_1_63_0/boost/math/distributions/chi_squared.hpp:12:10: fatal error: 'boost/math/distributions/fwd.hpp' file not found #include <boost/math/distributions/fwd.hpp>.
To compile I've used an assortment of commands, all unsuccessfully:
g++ -L /boost_1_63_0/boost/math/distributions main.cpp
g++ -I"/boost_1_63_0/boost/math/" main.cpp
g++ -I "/boost_1_63_0/boost/math/" main.cpp
g++ main.cpp -lboost_math
What is the correct include statement and G++ command that I need to use?
Resolved using
#include "/Users/[me]/[project_dir]/boost_1_63_0/boost/math/distributions/chi_squared.hpp"
and
g++ -I/Users/[me]/[project_dir]/boost_1_63_0/ main.cpp

Problems with compiling c++20

I'm trying to play around with some new c++20 features but I am not able to compile even some simple lines of code..
I am working on macOS and first tried to compile it in Xcode with c++2a language dialect which didn't work out and then tried to compile the code from the command line with g++ but I got similar errors.
Everything is up to date (Clang Vers 12, g++/gcc vers 10) and I know that there are still some issues with some features.
So here is an example:
import <iostream>;
#include <vector>
int main(){
std::vector<int> vec{1,2,3};
for (int e : vec) std::cout << e << std::endl;
}
compiled with:
g++ -std=c++2a -fmodules-ts test.cpp
Throws the following error:
header file <iostream> (aka '/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream') cannot be imported because it is not known to be a header
So I assumed there is a problem with the header path from the toolchain which hasn't the latest updates so I tried changing the path to g++ main includes.
So I tried:
g++ -std=c++2a -fmodules-ts -I/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/ test.cpp
Which throwed this error:
test.cpp:1:8: error: header file <iostream> (aka '/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/iostream') cannot be imported because it is not known to be a header unit
import <iostream>;
^
In file included from test.cpp:2:
In file included from /usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/vector:60:
/usr/local/Cellar/gcc/10.2.0_3/include/c++/10.2.0/bits/stl_algobase.h:59:10: fatal error: 'bits/c++config.h' file not found
#include <bits/c++config.h>
^~~~~~~~~~~~~~~~~~
2 errors generated.
Can someone help with this?
I am aware that I can make this compile using #include - this was just as an example for working with new features - I had the same issue with e.g. import .
I am really struggling with getting anything sorted - which compiler is the best usecase for c++20 and how and where can I include/locate the standardlib which has c++20 support?
And how can I use everything together in Xcode?
gcc 10 does not support this yet. You can use gcc trunk or the gcc modules branch, and you need to add -fmodules-ts to the command line.

error: 'unordered_set' is not a member of 'std'

In C++, I am trying to declare an unordered_set simply like this:
std::unordered_set<int> k;
But it is showing this error:
error: 'unordered_set' is not a member of 'std'
I am using g++ (GCC) 5.3.0 on windows using MinGW. Here are the things that I have already considered:
Adding the header file by #include <unordered_set>
Upgrading MinGW
Using the flag -std=gnu++11. (This is not generating any executable or error, not sure if it doing anything or not)
How to fix it and compile my code successfully?
Use -std=c++11 switch and specify output file.
g++ -std=c++11 your_file.cpp -o your_program

Compile error: 'stoi' is not a member of 'std'

My code:
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = std::stoi(test);
std::cout << myint << '\n';
}
Gives me the compile error:
error: 'stoi' is not a member of 'std'
int myint = std::stoi(test);
^
However, according to here, this code should compile fine. I am using the line set(CMAKE_CXX_FLAGS "-std=c++11 -O3") in my CMakeLists.txt file.
Why is it not compiling?
Update: I am using gcc, and running gcc --version prints out:
gcc (Ubuntu 5.2.1-22ubuntu2) 5.2.1 20151010
In libstdc++, the definitions of stoi, stol, etc., as well as the to_string functions, are guarded by the condition
#if ((__cplusplus >= 201103L) && defined(_GLIBCXX_USE_C99) \
&& !defined(_GLIBCXX_HAVE_BROKEN_VSWPRINTF))
I have had this fail on one platform before (namely Termux on Android), resulting in to_string not being available even with g++ 6.1 and the C++14 standard. In that case, I just did
#define _GLIBCXX_USE_C99 1
before including anything, and voilà, suddenly the functions existed. (You should put this first, or even on the command line, rather than just before including <string>, because another header may include <string> first, and then its include guards will keep it from ever seeing your macro.)
I did not investigate why this macro wasn't set in the first place. Obviously this is a cause for concern if you want your code to actually work (in my case I didn't particularly, but FWIW there were no problems.)
You should check if _GLIBCXX_USE_C99 is not defined, or if _GLIBCXX_HAVE_BROKEN_VSWPRINTF is defined (which may be the case on MinGW?)
std::stoi is a C++11 function. You have to use the -std=c++11 to enable it in both g++ and clang++. This is the actual issue, not a linking error or a specific preprocessor define.
$ cat test.cxx
#include <iostream>
#include <string>
int main()
{
std::string test = "45";
int myint = std::stoi(test);
std::cout << myint << '\n';
}
$ g++ -otest test.cxx
test.cxx: In Funktion »int main()«:
test.cxx:7:17: Fehler: »stoi« ist kein Element von »std«
int myint = std::stoi(test);
^
$ g++ -otest test.cxx -std=c++11
$ ./test
45
$
edit: I just saw that you used c++11. Are you sure that's making it into your compile options? Check the generated makefile and watch the executed commands to be certain.
Your version seems up to date, so there shouldn't be an issue. I think it may be related to gcc. Try g++ instead.(Most likely automatically linking issue. If you just run gcc on a C++ file, it will not 'just work' like g++ does. That's because it won't automatically link to the C++ std library, etc.). My second advise is try std::atoi.
# I have fixed the issue. std::stoi uses libstdc++. It is about The GNU Standard C++ Library. In gcc you have to link adding -lstdc++. However, in g++, libstdc++ is linked automatically.
using gcc and using g++
Pay attention how it is compiled
using g++: g++ -std=c++11 -O3 -Wall -pedantic main.cpp && ./a.out
using gcc: gcc -std=c++11 -O3 -Wall -pedantic -lstdc++ main.cpp && ./a.out
I think you should set flag like set(CMAKE_EXE_LINKER_FLAGS "-libgcc -lstdc++") (Not tested)
#include <cstdlib>
int myInt = std::atoi(test.c_str());
If you are using Cmake to compile, add line:
"add_definitions(-std=c++11)"
after find_package command.
Use 'set(CMAKE_CXX_STANDARD 11)' for Cmake

clang++ only compiles C++11 program using boost::format when -std=c++11 option is dropped

Please take a look at the following C++11 snippet:
#include <boost/format.hpp>
int main(int argc, char** argv)
{
auto s = boost::format("");
return 0;
}
When I compile it with clang using the -std=c++11 I get the following error:
$ clang++ -std=c++11 -o main main.cpp
In file included from main.cpp:1:
In file included from /usr/include/boost/format.hpp:19:
In file included from /usr/include/boost/detail/workaround.hpp:41:
In file included from /usr/include/boost/config.hpp:40:
In file included from /usr/include/boost/config/select_stdlib_config.hpp:18:
/usr/bin/../lib64/gcc/x86_64-unknown-linux-gnu/4.9.0/../../../../include/c++/4.9.0/cstddef:51:11: error:
no member named 'max_align_t' in the global namespace
using ::max_align_t;
~~^
1 error generated.
Without the -std=c++11 everything compiles fine, but clang prints a warning:
$ clang++ -o main main.cpp
main.cpp:5:3: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
auto s = boost::format("");
^
So, it looks like a valid workaround is to drop the C++11 flag, as the current version of clang seem to be in C++11 mode, anyway? The drawback is that you will get many warnings.
Is there a better workaround beside completely switching to gcc? Patching the source code of boost::format or gcc-libs is fine for me.
System information:
Platform: Arch Linux x86_64
Boost version: 1.55.0-6
gcc-libs: 4.9.0-1
clang++: 3.4 (tags/RELEASE_34/final)
The bug is closed now. It should be fixed in Arch with clang 3.4-2.
With this commit, Evangelos Foutras merged the following patch from upstream:
http://reviews.llvm.org/rL201729