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

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

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.

Got undeclared identifier error for a function argument (C++) in Linux, but not in Windows

I have written these codes:
#include <iostream>
#include <limits>
#include <string>
#ifdef _WIN32
#include "WinSock2.h"
#include "WS2tcpip.h"
#pragma comment(lib,"ws2_32.lib")
#elif __linux__
#include <sys/socket.h>
#include <arpa/inet.h>
#define SOCKET socket
#else
#error Compiler cannot interpret platform. Please compile this program in Windows 32/64-bit or Linux!
#endif
//...some codes here for initalization for _WIN32
int socket_create(SOCKET &socketHandler)
{
socketHandler = socket(AF_INET, SOCK_STREAM, 0);
if (socketHandler == INVALID_SOCKET)
{
#ifdef _WIN32
return (WSAGetLastError());
#else
return -1;
#endif
}
else
{
return 0;
}
}
//...some codes for other functions
//...main function
When compiled using Clang 11.0.0 on Windows 10 x64 with arguments:
clang server.cpp -Wall -Wextra -std=c++17 -g -glldb -lws2_32 -fexceptions -O0 -o target\debug\win-amd64\server.exe -fms-compatibility -m64
it was flawless. The program compiled without even a warning and was running perfectly.
However, bringing it straight to linux (Ubuntu 20.04 x64, with clang-11 and libc++-dev & libc++abi-dev installed), and compiled using Clang 11.0.0 in there with arguments:
clang-11 server.cpp -Wall -Wextra -std=c++17 -g -glldb -fexceptions -O0 -o target\debug\deb-amd64\server.exe -m64
It gives me this error:
error: use of undeclared identifier 'socketHandler'
int socket_create(SOCKET &socketHandler)
^
and
error: expected ';' after top level declarator
int socket_create(SOCKET &socketHandler)
^
Question: Why is it different in Linux? Did I miss something in declaration? If so, how could the same version of clang in Windows would compile it fine, while straight up refused in Linux?
TBH, this is my first time compiling things in Linux, so I don't know if I miss something that I should do in Linux but not in Windows for C++. Thank you.
What socket() in Linux returns is int, not socket nor SOCKET.
You are using #define SOCKET socket in Linux mode and it will have it put function name where type name is expected like this:
int hoge(){ return 0; }
int fuga(hoge& x) { // error
return 0;
}
This error is not produced in Windows because the problematic line #define SOCKET socket is not used in Windows thanks to the #ifdef directives.
In conclusion, the line #define SOCKET socket should be typedef int SOCKET;.

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

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;
^~~

fatal error: unordered_map: No such file or directory (C++ 98, gcc 4.8.5, using CMAKE)

gcc throws fatal error: unordered_map.h: No such file or directory on #include . I am using CMAKE, and gcc 4.8.5 compiler.
main:
#include "a.h"
...
void main() { ...}
a.h:
#include <unordered_map>
I wasn't able to cross reference, but it says that unordered_map was introduced in C++11.
Source:
https://en.cppreference.com/w/cpp/container/unordered_map