【发布时间】:2020-07-02 16:10:22
【问题描述】:
在尝试使用 C++ std::mutex 时,我在 Windows 10 64 位上遇到错误。该代码基本上是为 Linux 编写的,但我正在尝试将其移植到 Windows。 (您可以在我下面添加的错误消息中看到编译器行。)
这是我的代码:
#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 |
【问题讨论】:
-
没什么。尽管互斥锁是特定于操作系统的,但调用适当的操作系统结构是实现的工作,而不是程序员的工作。为什么这样问?您是否尝试过某事并遇到错误?
-
就用吧。有什么问题?如果您有一个请提供您收到的代码和确切的错误文本。
-
请详细说明“清单”、“使用”、“标志”的含义。目前任何回答你问题让你满意的人都会因为他们的千里眼而让我惊叹不已。
-
我可以猜到 - 在 Windows 上使用了特定于 Linux 的标头
-
抱歉,这里一团糟。你包括
unistd.h和Winsock2.h和pthread.h和sys/types.h,此时<mutex>真的是最少的你的问题......也许退后几步。
标签: c++ windows-10