jeudi 2 janvier 2020

HOW to solve "'mutex' in namespace 'std' does not name a type"?

My C++ file is as following:

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

When I compile it on Ubuntu with 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,

I meet the following problem:

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
.

It seems that I cannot use std::mutex, my g++ version is 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. and I use Ubuntu.

Aucun commentaire:

Enregistrer un commentaire