编译 caffe 的自定义层时 LevelDB 出现奇怪的错误

A weird error in in LevelDB while compiling a custom layer of caffe

#include <leveldb/status.h>
#include <leveldb/db.h>
#include <leveldb/write_batch.h>
...
int arg_offset = 0;
leveldb::DB* db1;
leveldb::Options options;
options.error_if_exists = true;
options.create_if_missing = true;
options.write_buffer_size = 268435456;
leveldb::WriteBatch* batch;
...
if (db_backend == "leveldb") 
{  
    // leveldb
    LOG(INFO) << "Opening leveldb " << argv[arg_offset+2];
    leveldb::Status status1 = leveldb::DB::Open(options, argv[arg_offset+2], &db1);
    CHECK(status1.ok()) << "Failed to open leveldb " << argv[arg_offset+2];
    batch = new leveldb::WriteBatch();
}

以上代码片段产生以下错误

$ g++ tools/convert_imageset_and_disparity.cpp -MMD -MP -pthread -fPIC -DCAFFE_VERSION=1.0.0-rc5 -DNDEBUG -O2 -DUSE_OPENCV -DUSE_LEVELDB -DUSE_LMDB -DWITH_PYTHON_LAYER -I/usr/include/python3.5m -I/usr/lib/python3.5/dist-packages/numpy/core/include -I/usr/local/include -I/usr/include/hdf5/serial -I/usr/include -I.build_release/src -I./src -I./include -I/usr/local/cuda-9.0/include -Wall -Wno-sign-compare -c -o .build_release/tools/convert_imageset_and_disparity.o 2> .build_release/tools/convert_imageset_and_disparity.o.warnings.txt \
    || (cat .build_release/tools/convert_imageset_and_disparity.o.warnings.txt; exit 1)



error: expected unqualified-id before ‘int’
         leveldb::Status status1 = leveldb::DB::Open(options, argv[arg_offset+2], &db1);
 -----------------^

error: ‘status1’ was not declared in this scope
         CHECK(status1.ok()) << "Failed to open leveldb " << argv[arg_offset+2];
---------------^

编译器版本:

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.9' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)

使用 sudo apt-get install libleveldb-dev

安装 LevelDB

同一个库正在另一个 cpp 文件中编译对 leveldb 的类似调用,并且编译成功

void LevelDB::Open(const string& source, Mode mode) {
  leveldb::Options options;
  options.block_size = 65536;
  options.write_buffer_size = 268435456;
  options.max_open_files = 100;
  options.error_if_exists = mode == NEW;
  options.create_if_missing = mode != READ;
  leveldb::Status status = leveldb::DB::Open(options, source, &db_);
  CHECK(status.ok()) << "Failed to open leveldb " << source
                     << std::endl << status.ToString();
  LOG(INFO) << "Opened leveldb " << source;
}

有人可以帮我调试这个 error: expected unqualified-id 吗?

列出了关于 How to use LevelDB 的优秀教程 here

我终于(至少)解决了编译错误。以下是我所做的更改:

  1. 已将 leveldb::Status status1 = leveldb::DB::Open(options, argv[arg_offset+2], &db1) 更改为 auto status1 = leveldb::DB::Open(options, argv[arg_offset+2], &db1) 感谢@ZivS 指出。
  2. 将 Makefile 从 LIBRARIES += glog gflags protobuf leveldb snappy \ lmdb boost_system boost_filesystem hdf5_hl hdf5 m \ opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs opencv_videoio 更改为 LIBRARIES += glog gflags protobuf leveldb snappy \ lmdb boost_system boost_filesystem hdf5_hl hdf5 m \ opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs opencv_videoio X11
  3. 然后最后用-std=c++11标志编译

代码能够编译。一旦我使用构建的对象加载 LevelDB,就会更新。

编辑2编译代码运行成功。