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

标签 c++ c++11 caffe leveldb

#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 吗?

here 列出了关于如何使用 LevelDB 的优秀教程。

最佳答案

最后我能够(至少)解决编译错误。以下是我所做的更改:

  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. LIBRARIES += glog gflags protobuf leveldb snappy 更改了 Makefile\ lmdb boost_system boost_filesystem hdf5_hl hdf5 m\ opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs opencv_videoioLIBRARIES += 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 编译后的代码运行成功。

关于c++ - 编译 caffe 的自定义层时 LevelDB 出现奇怪的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49276694/

相关文章:

c++ - 你能不先分配资源就把数据复制到 `char*`指针吗?

c++ - 如何从UTF16编码的字符串中获取解码后的字符串

c++ - 如何使用输入和输出流操纵器将所有空白字符替换为另一个字符?

c++ - 尽管按名称返回,为什么仍复制局部变量?

c++ - 漂亮地打印 C++ 中的变量和值列表

computer-vision - lmdb 类型的 Caffe 大文件

c++ - std::is_invocable 在模板类型上的意外结果

c++ - 如何有效地查找大型 vector 中的元素

ubuntu - 编译caffe : libpng and libgfortran not found,共享库时出现ld错误

python - 在 Pycaffe 中通过索引访问 blob