Log in

View Full Version : "error: 'mutex' in namespace 'std' does not name a type" despite up-to-date toolchain


Reino
23rd December 2018, 16:01
I'm using 'mingw-w64-build-r25' from https://files.1f0.de/mingw/scripts/ (https://files.1f0.de/mingw/scripts/) (does anyone know who actually maintains these updated scripts? Hendrik / nevairiel?) to create my own toolchain with GCC 8.2, MinGW-w64 6.0.0 (https://github.com/Reino17/ffmpeg-windows-build-helpers/commit/0de628a18b2c1a9229a7a5777d27615dffe7f9b5) and pthreads 2.9.1.

Today I tried to compile libdmusic (https://github.com/frabert/libdmusic.git), but I got the following error:
[ 11%] Building CXX object CMakeFiles/dmusic.dir/src/DlsPlayer.cpp.obj
In file included from /[...]/libdmusic_git/include/dmusic/DlsPlayer.h:8,
from /[...]/libdmusic_git/src/DlsPlayer.cpp:1:
/[...]/libdmusic_git/include/dmusic/PlayingContext.h:74:14: error: 'mutex' in namespace 'std' does not name a type
std::mutex m_queueMutex;
^~~~~
I've done some searching and read that std::mutex and std::thread are added in C++11, which requires a recent GCC version and most likely the -std=gnu++11 compiler flag as well.
I'm using the latest GCC, but export CXXFLAGS="-std=gnu++11" didn't make any difference. I still get the error.
Now I wonder, is this guy (https://stackoverflow.com/a/17986785) right, despite having the latest GCC? Or is there a flaw in the MinGW-w64 build script that I'm getting this error?

LoRd_MuldeR
23rd December 2018, 21:21
std::mutex is indeed a C++11 feature. But I think "-std=gnu++14" is GCC's default for C++ code by now:
https://gcc.gnu.org/onlinedocs/gcc/C-Dialect-Options.html#C-Dialect-Options

Anyhow, whether setting CXXFLAGS or CFLAGS has any effect, that totally depends on the build system (e.g. Makefile) that is used to build the application!
For example, you may need to specify those when running the ./configure script, so that they will actually be applied to the generated Makefile.

Also, make sure that your MinGW was built with "posix" threading (libwinpthread). If built with "win32" threading, C++11 threading features are not available!

More info on that topic here:
* https://stackoverflow.com/a/30390278
* https://wiki.qt.io/MinGW-64-bit#GCC_Threading_model_.28posix_vs_win32.29

Reino
25th December 2018, 12:06
whether setting CXXFLAGS or CFLAGS has any effect, that totally depends on the build system (e.g. Makefile) that is used to build the application!
libdmusic is built with cmake. In 'CMakeCache.txt' at least I can see:
//Flags used by the CXX compiler during all build types.
CMAKE_CXX_FLAGS:STRING=-std=gnu++14

Also, make sure that your MinGW was built with "posix" threading (libwinpthread). If built with "win32" threading, C++11 threading features are not available!
$ ./mingw-w64-build-r25 --help
[...]
Compile Options:
[...]
--threads=LIB compile with support for thread LIB (winpthreads,
pthreads-w32, disable) [pthreads-w32]

As this script defaults to pthreads-w32, I've (successfully) rebuilt my toolchain with winpthreads this time. (libwinpthread == winpthreads, right?)
Despite all the effort however I'm still getting the mutex error. I really don't understand why.

LoRd_MuldeR
25th December 2018, 13:02
Try "g++ -v" and check the output. Should be something like this:
$ g++ -v
Using built-in specs.
COLLECT_GCC=C:\msys64\mingw32\bin\g++.exe
COLLECT_LTO_WRAPPER=C:/msys64/mingw32/bin/../lib/gcc/i686-w64-mingw32/7.3.0/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: ../gcc-7.3.0/configure --prefix=/mingw32 --with-local-prefix=/mingw32/local --build=i686-w64-mingw32 --host=i686-w64-mingw32 --target=i686-w64-mingw32 --with-native-system-header-dir=/mingw32/i686-w64-mingw32/include --libexecdir=/mingw32/lib --enable-bootstrap --with-arch=i686 --with-tune=generic --enable-languages=c,lto,c++,objc,obj-c++,fortran,ada --enable-shared --enable-static --enable-libatomic --enable-threads=posix --enable-graphite --enable-fully-dynamic-string --enable-libstdcxx-time=yes --enable-libstdcxx-filesystem-ts=yes --disable-libstdcxx-pch --disable-libstdcxx-debug --disable-isl-version-check --enable-lto --enable-libgomp --disable-multilib --enable-checking=release --disable-rpath --disable-win32-registry --disable-nls --disable-werror --disable-symvers --with-libiconv --with-system-zlib --with-gmp=/mingw32 --with-mpfr=/mingw32 --with-mpc=/mingw32 --with-isl=/mingw32 --with-pkgversion='Rev2, Built by MSYS2 project' --with-bugurl=https://sourceforge.net/projects/msys2 --with-gnu-as --with-gnu-ld --disable-sjlj-exceptions --with-dwarf2
Thread model: posix
gcc version 7.3.0 (Rev2, Built by MSYS2 project)

The following example program compiles without problem here, using the above MinGW/GCC:
https://pastebin.com/aQ25Gcp0

(BTW: Why not simply grab MinGW/GCC via MSYS2 (https://www.msys2.org/)? This is what I'm using now exclusively)

nevcairiel
25th December 2018, 19:59
Its a shame that they don't want to implement the stuff with native threading. winpthreads is kinda crappy.
Especially since someone already did all the work and made a header file with std::thread support for win32 threading model as well.

https://github.com/meganz/mingw-std-threads

Reino
26th December 2018, 15:39
Try "g++ -v" and check the output.$ ./i686-w64-mingw32-g++.exe -v
Using built-in specs.
COLLECT_GCC=./i686-w64-mingw32-g++
COLLECT_LTO_WRAPPER=/[...]/cross_compilers/mingw-w64-i686/libexec/gcc/i686-w64-mingw32/8.2.0/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: ../source/gcc-8.2.0/configure --build=i686-pc-cygwin --target=i686-w64-mingw32 --disable-shared --enable-static --disable-nls --disable-multilib --prefix=/[...]/cross_compilers/mingw-w64-i686 --with-sysroot=/[...]/cross_compilers/mingw-w64-i686 --with-mpc=/[...]/cross_compilers/pkgs/mpc/mpc-1.1.0-i686 --with-mpfr=/[...]/cross_compilers/pkgs/mpfr/mpfr-4.0.1-i686 --with-gmp=/[...]/cross_compilers/pkgs/gmp/gmp-6.1.2-i686 --with-isl=/[...]/cross_compilers/pkgs/isl/isl-0.20-i686 --enable-languages=c,c++ --enable-fully-dynamic-string --enable-lto
Thread model: win32
gcc version 8.2.0 (GCC)

I've tried to rerun the script with --enable-threads=posix ...
diff --git a/patches/mingw-w64-build-r25 b/patches/mingw-w64-build-r25
index d43cbd5..007ce53 100755
--- a/patches/mingw-w64-build-r25
+++ b/patches/mingw-w64-build-r25
@@ -317,7 +317,7 @@ fi

case "$gcc_ver" in
$gcc_old_release_ver ) CC=$gcc "../source/gcc-$gcc_ver/configure" --build="$system_type" --target="$mingw_w64_target" "${static_build[@]}" "${disable_nls[@]}" --disable-multilib --prefix="$mingw_w64_prefix" --with-sysroot="$mingw_w64_prefix" --with-mpc="$mpc_prefix" --with-mpfr="$mpfr_prefix" --with-gmp="$gmp_prefix" --with-host-libstdcxx="-lstdc++" --with-cloog="$cloog_prefix" --with-isl="$isl_prefix" --enable-languages="$enabled_langs" --enable-fully-dynamic-string --enable-lto > >(build_log) 2>&1 ;;
- $gcc_release_ver ) CC=$gcc "../source/gcc-$gcc_ver/configure" --build="$system_type" --target="$mingw_w64_target" $gcc_host "${static_build[@]}" "${disable_nls[@]}" --disable-multilib --prefix="$mingw_w64_prefix" --with-sysroot="$mingw_w64_prefix" --with-mpc="$mpc_prefix" --with-mpfr="$mpfr_prefix" --with-gmp="$gmp_prefix" --with-isl="$isl_prefix" --enable-languages="$enabled_langs" --enable-fully-dynamic-string --enable-lto > >(build_log) 2>&1 || print_error ;;
+ $gcc_release_ver ) CC=$gcc "../source/gcc-$gcc_ver/configure" --build="$system_type" --target="$mingw_w64_target" $gcc_host "${static_build[@]}" "${disable_nls[@]}" --disable-multilib --prefix="$mingw_w64_prefix" --with-sysroot="$mingw_w64_prefix" --with-mpc="$mpc_prefix" --with-mpfr="$mpfr_prefix" --with-gmp="$gmp_prefix" --with-isl="$isl_prefix" --enable-languages="$enabled_langs" --enable-fully-dynamic-string --enable-lto --enable-threads=posix > >(build_log) 2>&1 || print_error ;;
esac
}

... but that didn't work out:
/[...]/cross_compilers/pkgs/gcc/build/./gcc/xgcc -B/[...]/cross_compilers/pkgs/gcc/build/./gcc/ -L/[...]/cross_compilers/mingw-w64-i686/i686-w64-mingw32/lib -L/[...]/cross_compilers/mingw-w64-i686/mingw/lib -isystem /[...]/cross_compilers/mingw-w64-i686/i686-w64-mingw32/include -isystem /[...]/cross_compilers/mingw-w64-i686/mingw/include -B/[...]/cross_compilers/mingw-w64-i686/i686-w64-mingw32/bin/ -B/[...]/cross_compilers/mingw-w64-i686/i686-w64-mingw32/lib/ -isystem /[...]/cross_compilers/mingw-w64-i686/i686-w64-mingw32/include -isystem /[...]/cross_compilers/mingw-w64-i686/i686-w64-mingw32/sys-include -g -O2 -O2 -I../../../source/gcc-8.2.0/libgcc/../winsup/w32api/include -g -O2 -DIN_GCC -DCROSS_DIRECTORY_STRUCTURE -W -Wall -Wno-narrowing -Wwrite-strings -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wold-style-definition -isystem ./include -g -DIN_LIBGCC2 -fbuilding-libgcc -fno-stack-protector -I. -I. -I../.././gcc -I../../../source/gcc-8.2.0/libgcc -I../../../source/gcc-8.2.0/libgcc/. -I../../../source/gcc-8.2.0/libgcc/../gcc -I../../../source/gcc-8.2.0/libgcc/../include -I../../../source/gcc-8.2.0/libgcc/config/libbid -DENABLE_DECIMAL_BID_FORMAT -DHAVE_CC_TLS -DUSE_EMUTLS -o unwind-dw2.o -MT unwind-dw2.o -MD -MP -MF unwind-dw2.dep -fexceptions -c ../../../source/gcc-8.2.0/libgcc/unwind-dw2.c
In file included from ../../../source/gcc-8.2.0/libgcc/gthr.h:148,
from ../../../source/gcc-8.2.0/libgcc/unwind-dw2.c:37:
./gthr-default.h:35:10: fatal error: pthread.h: No such file or directory
#include <pthread.h>
^~~~~~~~~~~
compilation terminated.
make[1]: *** [../../../source/gcc-8.2.0/libgcc/static-object.mk:17: unwind-dw2.o] Error 1
make[1]: Leaving directory '/[...]/cross_compilers/pkgs/gcc/build/i686-w64-mingw32/libgcc'
make: *** [Makefile:12735: all-target-libgcc] Error 2
(BTW: Why not simply grab MinGW/GCC via MSYS2 (https://www.msys2.org/)? This is what I'm using now exclusively)
The toolchain that the 'mingw-w64-build-r25' script produces is the only toolchain I have yet experience with. I'm still relatively new to this.
I might give it a try some day.

LoRd_MuldeR
31st December 2018, 13:51
The toolchain that the 'mingw-w64-build-r25' script produces is the only toolchain I have yet experience with. I'm still relatively new to this.
I might give it a try some day.

Just install MYSY2 from here (https://www.msys2.org/) (you probably want 64-Bit these days). Then start MSYS2 shell and run "pacman -Syu", restart MSYS2 shell, run "pacman -Su", and restart MSYS2 shell again.

Finally, run "pacman -S mingw-w64-i686-toolchain" or "pacman -S mingw-w64-x86_64-toolchain" to install MinGW/GCC targeting 32-Bit or 64-Bit, respectively. That's it :)

filler56789
8th November 2019, 08:26
Finally, run "pacman -S mingw-w64-i686-toolchain" or "pacman -S mingw-w64-x86_64-toolchain" to install MinGW/GCC targeting 32-Bit or 64-Bit, respectively. That's it :)

That's simple, but it's not as smart as it may seem.
Downloading the entire "toolchain" means installing normally-unnecessary stuff... Most people need only the C and C++ compilers and really have no use for Ada, Fortran and etc.

So I recommend these command-lines:

1) pacman -S mingw-w64-i686-binutils mingw-w64-i686-crt-git mingw-w64-i686-gcc mingw-w64-i686-gcc-libs
mingw-w64-i686-headers-git mingw-w64-i686-libwinpthread-git mingw-w64-i686-tools-git mingw-w64-i686-winpthreads-git

2) pacman -S mingw-w64-x86_64-binutils mingw-w64-x86_64-crt-git mingw-w64-x86_64-gcc mingw-w64-x86_64-gcc-libs
mingw-w64-x86_64-headers-git mingw-w64-x86_64-libwinpthread-git mingw-w64-x86_64-tools-git mingw-w64-x86_64-winpthreads-git

And only if you absolutely need them: mingw-w64-i686-winstorecompat-git && mingw-w64-x86_64-winstorecompat-git, respectively.