c++ - 如何解决命名空间 "' 中的 'std' 互斥体'未命名类型“?

标签 c++ c++11 ubuntu g++ stdmutex

我的 C++ 文件如下:

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&);
};

当我在 Ubuntu 上编译它时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 ,

我遇到以下问题:
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。

最佳答案

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

关于c++ - 如何解决命名空间 "' 中的 'std' 互斥体'未命名类型“?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59572833/

相关文章:

c++ - 在 Qt 中从 xml 文件中添加/删除特定元素?

c++ - Qt - QStackedWidget 用法

c++ - boost 时出现段错误( boost :filesystem:exists)

qt - QT : svg 中的未知模块

c - 循环客户端到服务器

C++ Linux Binary 以信号 SIGKILL 终止 - 为什么? (加载到 GDB 中)

c++ - conio.h 和 linux 问题

c++ - 当元素数 > 1000 时,如何制作 vector 的笛卡尔积?

C++:具有任意数量初始化参数的模板类/函数

visual-studio-2010 - 使用 C++0x decltype 返回值时返回局部变量或临时变量的地址