• 在 msys2@mingw 下编译 BVLC/Caffe


    (参考 https://github.com/lemonsqueeze/mingw-caffe ,有关这个项目的尝试写在文章后面)

    1.  准备开发环境

    有关需要安装的工具,上面这篇已经说的很明白了,照搬于下。

    1. pacman -S --needed git make patch diffutils
    2. pacman -S --needed \
    3. mingw-w64-${MSYSTEM_CARCH}-cmake \
    4. mingw-w64-${MSYSTEM_CARCH}-python \
    5. mingw-w64-${MSYSTEM_CARCH}-tools-git \
    6. mingw-w64-${MSYSTEM_CARCH}-gcc \
    7. mingw-w64-${MSYSTEM_CARCH}-boost \
    8. mingw-w64-${MSYSTEM_CARCH}-protobuf-c \
    9. mingw-w64-${MSYSTEM_CARCH}-gflags \
    10. mingw-w64-${MSYSTEM_CARCH}-glog \
    11. mingw-w64-${MSYSTEM_CARCH}-hdf5 \
    12. mingw-w64-${MSYSTEM_CARCH}-openblas \
    13. mingw-w64-${MSYSTEM_CARCH}-leveldb \
    14. mingw-w64-${MSYSTEM_CARCH}-lmdb \
    15. mingw-w64-${MSYSTEM_CARCH}-snappy \
    16. mingw-w64-${MSYSTEM_CARCH}-python-matplotlib \
    17. mingw-w64-${MSYSTEM_CARCH}-python-pytest \
    18. mingw-w64-${MSYSTEM_CARCH}-python-scipy

     还要下载 caffe 源代码

    1. wget https://github.com/BVLC/caffe/archive/refs/tags/1.0.tar.gz
    2. tar xf 1.0.tar.gz
    3. cd caffe

    2.  编译脚本

    1. #!/bin/bash
    2. BUILD_DIR=${1:-build}
    3. export PATH=/mingw64/bin:/mingw64/include:$PATH
    4. [ -e ${BUILD_DIR} ] && rm -r ${BUILD_DIR}/* || mkdir ${BUILD_DIR}
    5. cmake \
    6. -B${BUILD_DIR} \
    7. -DBLAS=open \
    8. caffe-1.0
    9. # Build
    10. [ $? -eq 0 ] && cmake --build ${BUILD_DIR} -j 8

    3.  错误修复 

    Error 1:

    /home/rd/NN/caffe/src/caffe/layers/window_data_layer.cpp: In member function ‘virtual void caffe::WindowDataLayer::load_batch(caffe::Batch*)’:
    /home/rd/NN/caffe/src/caffe/layers/window_data_layer.cpp:293:42: error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope
      293 |         cv_img = cv::imread(image.first, CV_LOAD_IMAGE_COLOR);
          |                                          ^~~~~~~~~~~~~~~~~~~
    make[2]: *** [src/caffe/CMakeFiles/caffe.dir/build.make:1090:src/caffe/CMakeFiles/caffe.dir/layers/window_data_layer.cpp.o] 错误 1
    make[1]: *** [CMakeFiles/Makefile2:400:src/caffe/CMakeFiles/caffe.dir/all] 错误 2

    问题原因 : codes for opencv2 or opencv3 are not compatiable to opencv4, so add below codes

    解决方案 : 在 caffe-1.0/src/caffe/layers/window_data_layer.cpp 和 caffe-1.0/src/caffe/util/io.cpp 文件前部添加补丁:

    1. #if (CV_MAJOR_VERSION > 3)
    2. #include "opencv2/imgcodecs/legacy/constants_c.h"
    3. #endif

    Error 2:

    /home/rd/NN//caffe-1.0/src/caffe/util/db_lmdb.cpp:13:19: error: too many arguments to function 'int mkdir(const char*)'
       13 |     CHECK_EQ(mkdir(source.c_str(), 0744), 0) << "mkdir " << source << " failed";

    参考 : 函数'int mkdir(const char*)‘的参数太多 

    解决方案:在 caffe-1.0/src/caffe/layers/window_data_layer.cpp 前部添加宏

    1. #if (defined(_WIN32) || defined(__WIN32__))
    2. #define mkdir(A, B) mkdir(A)
    3. #endif

    Error 3: 

    /home/rd/NN/caffe-1.0/src/caffe/util/io.cpp: In function ‘bool caffe::ReadProtoFromBinaryFile(const char*, google::protobuf::Message*)’:
    /home/rd/NN/caffe/src/caffe/util/io.cpp:60:66: error: no matching function for call to ‘google::protobuf::io::CodedInputStream::SetTotalBytesLimit(const int&, int)’
       60 |   coded_input->SetTotalBytesLimit(kProtoReadBytesLimit, 536870912);

    问题原因 : refer to https://github.com/onnx/onnx/issues/2678

    解决方案 :  修改  caffe-1.0/src/caffe/util/io.cpp 相应代码

    1. #if GOOGLE_PROTOBUF_VERSION >= 3002000
    2. coded_input->SetTotalBytesLimit(kProtoReadBytesLimit);
    3. #else
    4. coded_input->SetTotalBytesLimit(kProtoReadBytesLimit, 536870912);
    5. #endif

    Error 4:

    D:/work/caffe-1.0/src/caffe/util/signal_handler.cpp:31:22: error: aggregate '{anonymous}::HookupHandler()::sigaction sa' has incomplete type and cannot be defined
       31 |     struct sigaction sa;
          |                      ^~
    D:/work/caffe-1.0/src/caffe/util/signal_handler.cpp:35:19: error: 'SA_RESTART' was not declared in this scope
       35 |     sa.sa_flags = SA_RESTART;
          |                   ^~~~~~~~~~
    D:/work/caffe-1.0/src/caffe/util/signal_handler.cpp:37:5: error: 'sigfillset' was not declared in this scope
       37 |     sigfillset(&sa.sa_mask);
          |     ^~~~~~~~~~
    D:/work/caffe-1.0/src/caffe/util/signal_handler.cpp:39:19: error: 'SIGHUP' was not declared in this scope
       39 |     if (sigaction(SIGHUP, &sa, NULL) == -1) {
          |                   ^~~~~~
    D:/work/caffe-1.0/src/caffe/util/signal_handler.cpp:39:36: error: invalid use of incomplete type 'struct {anonymous}::HookupHandler()::sigaction'
       39 |     if (sigaction(SIGHUP, &sa, NULL) == -1) {
          |                                    ^
    D:/work/caffe-1.0/src/caffe/util/signal_handler.cpp:31:12: note: forward declaration of 'struct {anonymous}::HookupHandler()::sigaction'
       31 |     struct sigaction sa;
          |            ^~~~~~~~~
    D:/work/caffe-1.0/src/caffe/util/signal_handler.cpp:42:36: error: invalid use of incomplete type 'struct {anonymous}::HookupHandler()::sigaction'
       42 |     if (sigaction(SIGINT, &sa, NULL) == -1) {
          |                                    ^
    D:/work/caffe-1.0/src/caffe/util/signal_handler.cpp:31:12: note: forward declaration of 'struct {anonymous}::HookupHandler()::sigaction'
       31 |     struct sigaction sa;

    参考c++ - Sigaction and porting Linux code to Windows - Stack Overflow

    问题原因:  sigaction 是 UNIX 信号 API 的一部分。 Windows 仅提供signal,不支持SIGHUP 或任何标志(例如SA_RESTART)。然而,非常基本的支持仍然存在,所以如果您只使用 signal(而不是 sigaction),代码应该仍然可以正常工作。

    解决方案 : 参考链接中的代码

     Error 5:

    /usr/local/lib/libgflags.a(gflags.cc.o): relocation R_X86_64_PC32 against symbol `stderr@@GLIBC_2.2.5' can not be used when making a shared object;

    问题原因: libgflags library is not built as shared. You may recompile it with below commands

    参考 https://github.com/BVLC/caffe/issues/2171,   2016-08-26/2022-06-21

    解决方案 :

    1. cd build/
    2. cmake .. -DBUILD_SHARED_LIBS=ON
    3. make
    4. sudo make install

    Error 6:

    [106/138] Linking CXX shared library bin\libcaffe.dll
    FAILED: bin/libcaffe.dll lib/libcaffe.dll.a
    cmd.exe /C "cd . && D:\msys64\mingw64\bin\c++.exe -Wno-sign-compare -Wno-uninitialized -O3 -DNDEBUG   -shared -o bin\libcaffe.dll -Wl,--out-implib,lib\libcaffe.dll.a -Wl,--major-image-version,1,--minor-image-v
    ersion,0 @CMakeFiles\caffe.rsp  && cd ."
    D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: src/caffe/CMakeFiles/caffe.dir/blob.cpp.obj:blob.cpp:(.rdata$.refptr.__emutls_v._ZN6google8protobuf8interna
    l15ThreadSafeArena13thread_cache_E[.refptr.__emutls_v._ZN6google8protobuf8internal15ThreadSafeArena13thread_cache_E]+0x0): undefined reference to `__emutls_v._ZN6google8protobuf8internal15ThreadSafeArena13thre
    ad_cache_E'
    collect2.exe: error: ld returned 1 exit status
    [113/138] Building CXX object tools/CMakeFiles/train_net.dir/train_net.cpp.ob

    在 build 目录中手动执行链接指令可重现

     c++.exe -Wno-sign-compare -Wno-uninitialized -O3 -DNDEBUG   -shared -o bin/libcaffe.dll -Wl,--out-implib,lib/libcaffe.dll.a -Wl,--major-image-version,1,--minor-image-version,0 @CMakeFiles/caffe.rsp  

    4. 尝试 https://github.com/lemonsqueeze/mingw-caffe

    这个项目和 caffe 同样悠久,所以它的 boost_headers 等补丁打不上也在情理之中。 

    可以注意到 Release caffe-cpu 20180127 · lemonsqueeze/mingw-caffe 提到的软件版本。

    1. mingw-w64-*-boost 1.66.0-1
    2. mingw-w64-*-gflags 2.2.1-1
    3. mingw-w64-*-glog 0.3.5-1
    4. mingw-w64-*-hdf5 1.8.20-1
    5. mingw-w64-*-openblas 0.2.20-1
    6. mingw-w64-*-protobuf 3.5.0-1
    7. mingw-w64-*-protobuf-c 1.3.0-1

    改写 scripts/msys_build.sh,原作不太简洁。

    1. #!/bin/sh
    2. # msys build script for appveyor
    3. die() { echo "$@"; exit 1; }
    4. # Run in top-level directory
    5. cd `dirname "$0"`/..
    6. # Show arch
    7. echo "MINGW_INSTALLS: $MINGW_INSTALLS"
    8. echo ""
    9. # Update pacman db and packages
    10. pacman --noconfirm -Syu
    11. pacman --noconfirm -Su
    12. echo ""
    13. pacman -S --noconfirm --needed \
    14. ${MINGW_PACKAGE_PREFIX}-boost \
    15. ${MINGW_PACKAGE_PREFIX}-protobuf-c \
    16. ${MINGW_PACKAGE_PREFIX}-gflags \
    17. ${MINGW_PACKAGE_PREFIX}-glog \
    18. ${MINGW_PACKAGE_PREFIX}-hdf5 \
    19. ${MINGW_PACKAGE_PREFIX}-openblas \
    20. git make patch diffutils
    21. echo "Using packages:"
    22. pacman -Q | grep 'boost\|protobuf\|gflags\|glog\|hdf5\|openblas\|-gcc '
    23. echo ""
    24. # Fix bad header in mingw-w64-i686-boost-1.66.0-1
    25. if [ -f /mingw32/include/boost/winapi/basic_types.hpp ]; then
    26. echo "Patching boost headers for ming32 ..."
    27. patch -d/ -p0 < boost_header_fix_mingw32.patch
    28. echo ""
    29. fi
    30. if [ -f /mingw64/include/boost/winapi/basic_types.hpp ]; then
    31. echo "Patching boost headers for mingw64 ..."
    32. patch -d/ -p0 < boost_header_fix_mingw64.patch
    33. echo ""
    34. fi
    35. # Build
    36. cd mingw-w64-caffe
    37. makepkg-mingw
    38. # Copy built package
    39. mkdir -p ../build
    40. cp *.pkg.tar.xz ../build/

  • 相关阅读:
    串行原理编程,中文编程工具中的串行构件,串行连接操作简单
    基于加权灰色关联投影的Bagging-Blending多模型融合短期电力负荷预测
    ElementPlus里的类型别名声明及使用
    设计模式之解释器模式
    适用于初学者,毕业设计的5个c语言项目,代码已开源
    python元组与列表的区别
    【前端笔试】知识点总结2
    pycharm 中的一个非常好使用的智能提示tabnine(大大提高代码的书写效率)
    getActionBar()=null的问题
    Spark基础【RDD分区和并行度】
  • 原文地址:https://blog.csdn.net/Tonyfield/article/details/133376577