编译caffe自定义层时遇到LevelDB的奇怪错误

5
#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)

LevelDB可以通过sudo apt-get install libleveldb-dev命令进行安装。

在另一个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;
}

有人能帮我调试这个错误:预期未限定标识符吗?

一个关于如何使用 LevelDB 的优秀教程在这里列出。


1
开始从那一行中删除一些内容,以查看是什么导致了错误。在我看来,编译器可能不知道该行代码中的某个符号(这就是为什么它说 'int' 的原因)。 - ZivS
我刚刚检查了该行的ASCII字符;我的实现(不起作用)和原始实现(起作用)中都没有任何“模糊”的字符。 - Khurram Amin
1
我是指,你应该调用auto status1 = leveldb::DB::Open(nullptr, nullptr, nullptr);或类似的方法,而不是调用leveldb::Status status1 = leveldb::DB::Open(options, argv[arg_offset+2], &db1);,以确保调用有效,并确保每个参数都有效等。 - ZivS
还有一些头文件可能会将Status定义为类似于int的东西,是吗? - bogdan
1个回答

5

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

  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 已成功运行编译后的代码。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接