-1

I‘m getting error on Windows 10 64-bits when trying to use the C++ std::mutex. The code was basically written for Linux but I'm trying to port it to Windows. (You can see compiler line in the error message that I added below.)

This is my code:

#ifndef UNTITLED_LIBRARY_H
#define UNTITLED_LIBRARY_H

#include <sys/types.h>
#include <Winsock2.h>
#include <mutex>
#include <thread>

class TCPServer
{
    static std::mutex mt;
};

#endif //UNTITLED_LIBRARY_H
Here is the error message:

g++ -Wall -std=c++14  -I./ library.h  -o libSimpleNetwork.so -fPIC -shared
library.h:11:17: error: 'mutex' in namespace 'std' does not name a type
   11 |     static std::mutex mt;
      |                 ^~~~~
library.h:8:1: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
    7 | #include <thread>
  +++ |+#include <mutex>
    8 |


Dby
  • 17
  • 7
  • 5
    Nothing. Although mutexes are OS-specific, it's the job of the implementation to call the appropriate OS constructs, not the programmer's. Why the question? Did you try something and encountered an error? – Panagiotis Kanavos Jul 02 '20 at 16:13
  • 1
    Just use it. What's the problem? If you have one *please include the code and exact error text you're getting*. – tadman Jul 02 '20 at 16:15
  • Please elaborate what you mean by "checklist", "use", "flags". Currently anybody answering your question to your satisfaction would have me speechless in awe because of their clairvoyance. – Yunnosch Jul 02 '20 at 16:15
  • I can guess - a Linux-specific header was used on Windows – Panagiotis Kanavos Jul 02 '20 at 16:16
  • Maybe you need this: `-std=c++11` – drescherjm Jul 02 '20 at 18:02
  • Im trying to compile simple code on my clion and getting error on the std::mutex (compile says its not part of std) im using the c++11 flag on when running the g++. Maybe I missed some lib when installing mingw32 ? – Dby Jul 02 '20 at 18:26
  • @PanagiotisKanavos why does it matter? Header is a header , no ? – Dby Jul 02 '20 at 18:27
  • No. Because mutexes are OS constructs, so the `std::mutex` methods need to make OS-specific calls. The declarations mat be the same but the implementation won't. In any case, if you get an error that `std::mutex` is missing, it means you forgot to include a header in code, or a library during linking. Post the *actual* code and *actual, full error message* – Panagiotis Kanavos Jul 02 '20 at 19:08
  • looks like i'm missing the lib for mutex , how to i add it ? what's the lib name ? isn't part of the mingw32 ? – Dby Jul 03 '20 at 06:12
  • 1
    Sorry, but this is a mess. You include `unistd.h` **and** `Winsock2.h` **and** `pthread.h` **and** `sys/types.h`, and at that point `` is really the least of your problems... perhaps take a couple of steps back. – DevSolar Jul 04 '20 at 17:47
  • please see , just want to understand why compiler is getting error on the std::mutex ? – Dby Jul 04 '20 at 18:05
  • also this is my compiler version : >g++ --version g++ (MinGW.org GCC Build-20200227-1) 9.2.0 – Dby Jul 04 '20 at 18:49
  • [Potentially related question](https://stackoverflow.com/questions/44567784/c-compile-error-mutex-in-std-does-not-name-a-type-in-mingw-gcc-6-3-0), [and this](https://stackoverflow.com/questions/43864159/mutex-is-not-a-member-of-std-in-mingw-5-3-0), [and this](https://stackoverflow.com/questions/14191566/c-mutex-in-namespace-std-does-not-name-a-type)... – DevSolar Jul 04 '20 at 21:51
  • Can't reproduce with x86_64-w64-mingw32-g++ (GCC) 9.2.0 (the one that Cygwin installs), so I guess it's the MinGW.org build that's non-compliant (as per the questions I linked in my previous comment)... – DevSolar Jul 06 '20 at 09:17
  • Is there a more mingw32 and mingw64 ? Can you please put here the link for your mingw ? – Dby Jul 06 '20 at 11:39
  • Also isn’t is a win-sdk compiler that i can use ? That come with windows instead of downloading the mingw and struggle with it ? – Dby Jul 06 '20 at 11:41
  • [Cygwin](https://www.cygwin.com) provides a more or less complete POSIX environment for Windows. The package manager / setup also offers mingw64 packages. Since I never used a Windows system *without* Cygwin, that's my go-to solution. I also found it much more accessible than the "raw" MinGW environment. – DevSolar Jul 06 '20 at 14:24

1 Answers1

0

So the answer to this issue is to install mingw64 (not mingw32!): https://sourceforge.net/projects/mingw-w64/

This includes the posix x86_64-posix-seh that gcc needs for libstd

Dby
  • 17
  • 7
  • Also, if this is correct when running gcc -v , you should see "Thread model: posix" – Dby Jul 08 '20 at 13:37