首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何解决“命名空间'std‘中的’mutex‘不命名类型”?

如何解决“命名空间'std‘中的’mutex‘不命名类型”?
EN

Stack Overflow用户
提问于 2020-01-03 11:27:51
回答 1查看 488关注 0票数 1

我的C++文件如下:

代码语言:javascript
运行
复制
class LogMessage {
 public:
  LogMessage(const char* file, int line)
      :
#ifdef __ANDROID__
        log_stream_(std::cout)
#else
        log_stream_(std::cerr)
#endif
  {
    std::unique_lock<std::mutex> lk(mu_);
    log_stream_ << "[" << pretty_date_.HumanDate() << "] " << file << ":"
                << line << ": ";
  }
  ~LogMessage() { log_stream_ << "\n"; }
  std::ostream& stream() { return log_stream_; }

 protected:
  std::ostream& log_stream_;

 private:
  DateLogger pretty_date_;
  mutable std::mutex mu_;
  LogMessage(const LogMessage&);
  void operator=(const LogMessage&);
};

当我用g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -pthread -c src/postoffice.cc -o build/postoffice.o在Ubuntu上编译它的时候,

我遇到了以下问题:

代码语言:javascript
运行
复制
root@4ec615bfc8fa:~/hbsun/nccl/ps-lite# ./make.sh
rm -rf build  tests/test_connection  tests/test_simple_app  tests/test_kv_app_multi_workers  tests/test_kv_app_benchmark  tests/test_kv_app tests/*.d
find src -name "*.pb.[ch]*" -delete
/home/hbsun/nccl/ps-lite/deps/bin/protoc --cpp_out=./src --proto_path=./src src/meta.proto
g++ -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -std=c++11 -MM -MT build/customer.o src/customer.cc >build/customer.d
g++ -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -std=c++11 -MM -MT build/postoffice.o src/postoffice.cc >build/postoffice.d
g++ -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -std=c++11 -MM -MT build/van.o src/van.cc >build/van.d
g++ -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include -std=c++11 -MM -MT build/meta.pb.o src/meta.pb.cc >build/meta.pb.d
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include  -pthread -c src/meta.pb.cc -o build/meta.pb.o
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include  -pthread -c src/customer.cc -o build/customer.o
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include  -pthread -c src/van.cc -o build/van.o
g++ -std=c++11 -msse2 -fPIC -O3 -ggdb -Wall -finline-functions -I./src -I./include -I/home/hbsun/nccl/ps-lite/deps/include  -pthread -c src/postoffice.cc -o build/postoffice.o
In file included from ./include/ps/internal/utils.h:6:0,
                 from ./include/ps/base.h:7,
                 from src/van.cc:8:
./include/dmlc/logging.h:189:8: error: 'mutex' in namespace 'std' does not name a type
   std::mutex mu_;
        ^
./include/dmlc/logging.h: In constructor 'dmlc::LogMessage::LogMessage(const char*, int)':
./include/dmlc/logging.h:177:5: error: 'unique_lock' is not a member of 'std'
     std::unique_lock<std::mutex> lk(mu_);
     ^
./include/dmlc/logging.h:177:22: error: 'mutex' is not a member of 'std'
     std::unique_lock<std::mutex> lk(mu_);
                      ^
./include/dmlc/logging.h:177:37: error: 'mu_' was not declared in this scope
     std::unique_lock<std::mutex> lk(mu_);
                                     ^
./include/dmlc/logging.h:177:40: error: 'lk' was not declared in this scope
     std::unique_lock<std::mutex> lk(mu_);
                                        ^
Makefile:52: recipe for target 'build/van.o' failed
make: *** [build/van.o] Error 1
make: *** Waiting for unfinished jobs....
rm src/meta.pb.h
.

似乎我不能使用std::mutex,我的g++版本是g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609 Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.,我使用的是Ubuntu。

EN

回答 1

Stack Overflow用户

发布于 2020-01-03 11:37:59

可能最好添加:#include <mutex>

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59572833

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档