error with <mutex> when using a library in C++, Windows - c++

I am trying to use the library Armadillo in my C++ code. When I try to use it, it gives me an error: "error: 'mutex' in namespace 'std' does not name a type"
I get the same error with the following minimalist implementation:
#include <iostream>
#include <mutex>
std::mutex mtx;
int main () {
return 0;
}
I think the "mutex" library is simply not there. I am using Windows, G++ 8.1.0, MinGW-w64, and SublimeText for editing. I am adding -std=c++11 when running the file. I run the file using the following command (try_mutex.cpp is a file with the previous code):
g++ -std=c++11 try_mutex.cpp
The output is as follows:
try_mutex.cpp:3:6: error: 'mutex' in namespace 'std' does not name a type
std::mutex mtx;
^~~~~
try_mutex.cpp:3:1: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
+#include <mutex>
std::mutex mtx;
^~~

Related

With MinGW g++ 9.2.0 I get an error: 'mutex' is not a member of 'std'

I updated my g++ because my older version didn't fully support std::filesystem, but now I cannot use mutexes at all. Example code:
#include <mutex>
int main(const int argc, const char** argv)
{
std::mutex test;
return 0;
}
Compiled with:
g++ -Wall -pedantic main.cpp -std=c++17 -o main.exe
The error, note how it suggests adding include for <mutex> right under the include that is already there:
main.cpp: In function 'int main(int, const char**)':
main.cpp:5:8: error: 'mutex' is not a member of 'std'
5 | std::mutex test;
| ^~~~~
main.cpp:2:1: note: 'std::mutex' is defined in header '<mutex>'; did you forget to '#include <mutex>'?
1 | #include <mutex>
+++ |+#include <mutex>
2 |
The full version of g++ is: g++ (MinGW.org GCC Build-2) 9.2.0. I installed MinGW using the MinGW installation manager. Is it possible that I am missing the correct version of stdlib?
I opened the mutex file in the MinGW directory and I see nothing that would be obviously wrong.

Why does 'error: 'thread' is not a member of 'std' occur even after I've link thread in cmake? [duplicate]

I wrote this as a simplified version of a multithreading example to get a feel for it, but am running into some issues when compiling. My compiler says that thread is not a member of std and prompts me to add #include <thread> to my code even though I have already done so. I've been unable to find any similar problems so far, but I suspect that it is an issue with how I'm compiling it because my code is very similar to the example code.
#include <iostream>
#include <thread>
void doWork () {
std::cout << "Working...\n";
}
int main () {
std::thread worker(doWork);
work.join();
std::cout << "Finished\n";
return 0;
}
My compiler is MinGW g++ 9.2.0
I compiled with g++ main.cpp -o main and it gave me these errors:
main.cpp: In function 'int main()':
main.cpp:9:7: error: 'thread' is not a member of 'std'
9 | std::thread worker(doWork);
| ^~~~~~
main.cpp:3:1: note: 'std::thread' is defined in header '<thread>'; did you forget to '#include <thread>'?
2 | #include <thread>
+++ |+#include <thread>
3 |
main.cpp:11:2: error: 'work' was not declared in this scope
11 | work.join();
| ^~~~
MinGW-w64 by default comes with native (Win32) instead of POSIX threads support, and unfortunately there is currently no Win32 gthreads implementation (the threading subsystem of libstdc++), hence no threading functionality in GCC.
You need to switch from x86_64-w64-mingw32-g++-win32 to x86_64-w64-mingw32-g++-posix package to get std::thread working in MinGW-w64.
The question mingw-w64 threads: posix vs win32 discusses this issue in more detail.

What should i have on windows to use std::mutex?

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 |
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

Possible namespace errors in OpenCV header files

I'm just now getting into OpenCV (4.1.0) in C++ (relatively new to both) and I'm getting an odd error from simply including some header files. (GCC 6.3.0)
Error:
from c:\Users\Logan\Projects\Code\C++\webcamTest.cpp:1:
C:\Users\Logan\Projects\Code\C++\Includes\opencv\build\include/opencv2/core/utility.hpp:697:14: error: 'recursive_mutex' in namespace 'std' does not name a type
typedef std::recursive_mutex Mutex;
^~~~~~~~~~~~~~~
C:\Users\Logan\Projects\Code\C++\Includes\opencv\build\include/opencv2/core/utility.hpp:698:25: error: 'Mutex' is not a member of 'cv'
typedef std::lock_guard<cv::Mutex> AutoLock;
^~
C:\Users\Logan\Projects\Code\C++\Includes\opencv\build\include/opencv2/core/utility.hpp:698:25: error: 'Mutex' is not a member of 'cv'
C:\Users\Logan\Projects\Code\C++\Includes\opencv\build\include/opencv2/core/utility.hpp:698:34: error: template argument 1 is invalid
typedef std::lock_guard<cv::Mutex> AutoLock;
Code:
#include <opencv2/core.hpp>
#include <opencv2/videoio.hpp>
#include <opencv2/highgui.hpp>
int main()
{
return 0;
}
To me this looks like an error with the way the headers are set up, but perhaps I didn't set something up correctly?
Currently the command I'm using is as follows:
g++ -std=c++11 -g -I C:\\Users\\Logan\\Projects\\Code\\C++\\Includes\\opencv\\build\\include -o C:\\Users\\Logan\\Projects\\Code\\C++\\Bins\\<File name I'm using in the editor>.exe <File I'm using in the editor>

error: 'mutex' does not name a type

When I am try the following code in ubuntu with arm-none-eabi-g++ tool chain i was getting compilation errors:
#include <iostream>
#include <thread> // std::thread
#include <mutex> // std::mutex
mutex mtx; // mutex for critical section
int main ()
{
return 0;
}
commpile command :
arm-none-eabi-g++ -Os -Wall -std=c++11 -fno-rtti -fno-exceptions -c mt.cc
compile error:
mt.cc:5:1: error: 'mutex' does not name a type mutex mtx; //
mutex for critical section
^
gcc version:
gcc version 4.8.4 20140725 (release) [ARM/embedded-4_8-branch revision 213147] (GNU Tools for ARM Embedded Processors)
You got the comment right:
#include <mutex> // std::mutex
But then you didn't get the code right:
mutex mtx; // mutex for critical section
That should be std::mutex