编译orb_slam3[windows]

1 下载

git clone https://github.com/UZ-SLAMLab/ORB_SLAM3.git ORB_SLAM3

建议编译前,先看一下该项目上的一个pull request:(使用原来项目问题过多, 遇到的每一个问题在第4节都会详细描述,于是使用如下的一个PR)
https://github.com/UZ-SLAMLab/ORB_SLAM3/pull/53
推荐使用vs2015生成器;

2 编译

2.1 DBoW2 编译(如果使用pr,本步骤可以不看,2.3的orbslam3也对所有cmakelist.txt的修改做了说明)

主要是需要添加oepncv和boost的依赖

PS F:\prjs\ORB_SLAM3> git diff
diff --git a/Thirdparty/DBoW2/CMakeLists.txt b/Thirdparty/DBoW2/CMakeLists.txt
index c561724..2368c23 100644
--- a/Thirdparty/DBoW2/CMakeLists.txt
+++ b/Thirdparty/DBoW2/CMakeLists.txt
@@ -32,9 +32,22 @@ if(NOT OpenCV_FOUND)
    endif()
 endif()

+
+set(Boost_USE_STATIC_LIBS ON)
+add_definitions("-DBOOST_ALL_NO_LIB=1")
+find_package(Boost REQUIRED COMPONENTS
+             serialization)
+
 set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

-include_directories(${OpenCV_INCLUDE_DIRS})
+include_directories(${OpenCV_INCLUDE_DIRS} ${BOOST_INCLUDEDIR})
+link_directories(${Boost_LIBRARY_DIRS})
+
+message("lib is: " ${Boost_SERIALIZATION_LIBRARY})
+
 add_library(DBoW2 SHARED ${SRCS_DBOW2} ${SRCS_DUTILS})
-target_link_libraries(DBoW2 ${OpenCV_LIBS})
+target_link_libraries(DBoW2 
+    ${OpenCV_LIBS}    
+    ${Boost_SERIALIZATION_LIBRARY}
+)

diff --git a/Thirdparty/DBoW2/DBoW2/FORB.cpp b/Thirdparty/DBoW2/DBoW2/FORB.cpp
index 1f1990c..80bf473 100644
--- a/Thirdparty/DBoW2/DBoW2/FORB.cpp
+++ b/Thirdparty/DBoW2/DBoW2/FORB.cpp
@@ -13,7 +13,7 @@
 #include <vector>
 #include <string>
 #include <sstream>
-#include <stdint-gcc.h>
+#include <stdint.h>

 #include "FORB.h"

如果指定了vs2019,然后又用vs2015编译的库,就需要用如下的最后一个参数去掉boost的autolink;

mkdir build && cd build
cmake .. \
-G "Visual Studio 16" \
-DCMAKE_BUILDY_TYPE=Release \
-DOpenCV_DIR="F:/BASE_ENV/forOpenMVS/opencv/build" \
-DBOOST_ROOT="F:/BASE_ENV/forOpenMVS/boost_1_73_0_v140" \
-DBOOST_INCLUDEDIR="F:/BASE_ENV/forOpenMVS/boost_1_73_0_v140" \
-DBOOST_LIBRARYDIR="F:/BASE_ENV/forOpenMVS/boost_1_73_0_v140" \
-DBOOST_ALL_NO_LIB=1 \     # 避免boost的autolink,autolink会要求vs版本和boost版本一致

2.2 g2o

项目->属性->c+±>预处器宏定义->添加: WINDOWS,否则会出现vasprintf找不到定义。

cd ../../g2o

echo "Configuring and building Thirdparty/g2o ..."

mkdir build
cd build
cmake .. \
-G "Visual Studio 14" \
-DCMAKE_BUILDY_TYPE=Release \
-DEIGEN3_INCLUDE_DIR="F:\BASE_ENV\forOpenMVS\eigen" \
-DBOOST_ALL_NO_LIB=1 \

2.3 orbslam3

2.3.0 首先按照下面的diff修改所有的internal::axpy和internal::atxpy


如果你是自己在orbslam3直接编译的话,请参考: https://github.com/RainerKuemmerle/g2o/issues/91

2.3.1 找不到unistd.h

首先:unistd.h需要修改调,主要是为了使用usleep,该函数使用如下代码替换:

```cpp
#include <windows.h>

void usleep(__int64 usec) 
{ 
    HANDLE timer; 
    LARGE_INTEGER ft; 

    ft.QuadPart = -(10*usec); // Convert to 100 nanosecond interval, negative value indicates relative time

    timer = CreateWaitableTimer(NULL, TRUE, NULL); 
    SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0); 
    WaitForSingleObject(timer, INFINITE); 
    CloseHandle(timer); 
}

或者如下一行能搞定:推荐这个方法

            if (ttrack < T) {
                long usec = static_cast<long>((T - ttrack) * 1e6);
                std::this_thread::sleep_for(std::chrono::microseconds(usec));
            }

2.3.2 使用cmake-gui开始编译

cmake-gui配置:
opencv,boost,需要修改cmakelist.txt:
添加如下定义:

-DBOOST_ALL_NO_LIB=1

如下是我对cmakeList.txt做出的修改。

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 70d03fe..79b32e7 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -34,6 +34,7 @@ if (CMAKE_CXX_COMPILER_ID MATCHES "MSVC")
   #set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")
 endif()
 
+add_definitions("-DBOOST_ALL_NO_LIB=1")
 
 LIST(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake_modules)
 
@@ -46,13 +47,15 @@ if(NOT OpenCV_FOUND)
 endif()
 MESSAGE(STATUS "OpenCV VERSION: ${OpenCV_VERSION}")
 
-find_package(Eigen3 3.1.0 REQUIRED)
+# find_package(Eigen3 REQUIRED)
+set(EIGEN3_INCLUDE_DIR "F:/BASE_ENV/forOpenMVS/eigen")
+MESSAGE("eigen & boost inlcude dir is: @@@@@@" ${EIGEN3_INCLUDE_DIRS}) 
 
 find_package(Pangolin REQUIRED)
 find_package(realsense2)
 
 find_package(Boost REQUIRED COMPONENTS serialization)
-MESSAGE(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
+MESSAGE(STATUS "cxy@@@@@@@@@@ Boost_INCLUDE_DIRS & Boost_LIBRARIES: ${Boost_INCLUDE_DIRS} ${Boost_LIBRARIES}")
 
 set(OPENSSL_USE_STATIC_LIBS TRUE)
 find_package(OpenSSL REQUIRED) # for crypto library
@@ -65,6 +68,7 @@ include_directories(
   ${EIGEN3_INCLUDE_DIR}
   ${Pangolin_INCLUDE_DIRS}
   ${OPENSSL_INCLUDE_DIR}
+  ${Boost_INCLUDE_DIRS}
 )
 
 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/lib)
diff --git a/Thirdparty/DBoW2/CMakeLists.txt b/Thirdparty/DBoW2/CMakeLists.txt
index bf987be..e527175 100644
--- a/Thirdparty/DBoW2/CMakeLists.txt
+++ b/Thirdparty/DBoW2/CMakeLists.txt
@@ -51,10 +51,16 @@ if(NOT OpenCV_FOUND)
   endif()
 endif()
 MESSAGE(STATUS "OpenCV VERSION: ${OpenCV_VERSION}")
-include_directories(${OpenCV_INCLUDE_DIRS})
-target_link_libraries(DBoW2 ${OpenCV_LIBS})
 
 # add Boost
-find_package(Boost REQUIRED COMPONENTS serialization)
-message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
-target_link_libraries(DBoW2 ${Boost_LIBRARIES})
+set(Boost_USE_STATIC_LIBS ON)
+add_definitions("-DBOOST_ALL_NO_LIB=1")
+find_package(Boost REQUIRED COMPONENTS 
+                  serialization)
+message(STATUS "cxy@@@@@@@@ Boost_INCLUDE_DIRS & libs: ${Boost_INCLUDE_DIRS} >>>> ${Boost_LIBRARIES}")
+
+include_directories(${OpenCV_INCLUDE_DIRS} ${BOOST_INCLUDEDIR})
+target_link_libraries(DBoW2 
+  ${OpenCV_LIBS} 
+  ${Boost_LIBRARIES}
+)
diff --git a/Thirdparty/g2o/g2o/core/sparse_block_matrix.hpp b/Thirdparty/g2o/g2o/core/sparse_block_matrix.hpp
index 8dfa99c..80e4fa8 100644
--- a/Thirdparty/g2o/g2o/core/sparse_block_matrix.hpp
+++ b/Thirdparty/g2o/g2o/core/sparse_block_matrix.hpp
@@ -24,6 +24,13 @@
 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+// add these marcos to fix:  
+// 1>f:\prjs\orb_slam3\thirdparty\g2o\g2o\core\sparse_block_matrix.hpp(277): fatal error C1001: 编译器中发生内部错误。
+
+#define _AXPY(MatrixType,A,x,xoff,y,yoff)y.segment<MatrixType::RowsAtCompileTime>(yoff) += (A) * (x).segment<MatrixType::ColsAtCompileTime>(xoff)
+#define _ATXPY(MatrixType,A,x,xoff,y,yoff)y.segment<MatrixType::ColsAtCompileTime>(yoff) += (A).transpose() * (x).segment<MatrixType::RowsAtCompileTime>(xoff)
+
+
 namespace g2o {
   using namespace Eigen;
 
@@ -249,7 +256,8 @@ namespace g2o {
         const typename SparseBlockMatrix<MatrixType>::SparseMatrixBlock* a=it->second;
         int destOffset = it->first ? _rowBlockIndices[it->first - 1] : 0;
         // destVec += *a * srcVec (according to the sub-vector parts)
-        internal::axpy(*a, srcVec, srcOffset, destVec, destOffset);
+        // internal::axpy(*a, srcVec, srcOffset, destVec, destOffset);
+        _AXPY(MatrixType, *a, srcVec, srcOffset, destVec, destOffset);
       }
     }
   }
@@ -274,9 +282,12 @@ namespace g2o {
         if (destOffset > srcOffset) // only upper triangle
           break;
         // destVec += *a * srcVec (according to the sub-vector parts)
-        internal::axpy(*a, srcVec, srcOffset, destVec, destOffset);
-        if (destOffset < srcOffset)
-          internal::atxpy(*a, srcVec, destOffset, destVec, srcOffset);
+        // internal::axpy(*a, srcVec, srcOffset, destVec, destOffset);
+        _AXPY(MatrixType, *a, srcVec, srcOffset, destVec, destOffset);
+        if (destOffset < srcOffset) {
+          // internal::atxpy(*a, srcVec, destOffset, destVec, srcOffset);
+          _ATXPY(MatrixType, *a, srcVec, destOffset, destVec, srcOffset);
+        }
       }
     }
   }
@@ -305,7 +316,8 @@ namespace g2o {
         const typename SparseBlockMatrix<MatrixType>::SparseMatrixBlock* a=it->second;
         int srcOffset = rowBaseOfBlock(it->first);
         // destVec += *a.transpose() * srcVec (according to the sub-vector parts)
-        internal::atxpy(*a, srcVec, srcOffset, destVec, destOffset);
+        // internal::atxpy(*a, srcVec, srcOffset, destVec, destOffset);
+	    _ATXPY(MatrixType, *a, srcVec, srcOffset, destVec, destOffset);
       }
     }
     
diff --git a/Thirdparty/g2o/g2o/core/sparse_block_matrix_ccs.h b/Thirdparty/g2o/g2o/core/sparse_block_matrix_ccs.h
index 36ddfe6..91832cd 100644
--- a/Thirdparty/g2o/g2o/core/sparse_block_matrix_ccs.h
+++ b/Thirdparty/g2o/g2o/core/sparse_block_matrix_ccs.h
@@ -24,6 +24,9 @@
 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+#define _AXPY(MatrixType,A,x,xoff,y,yoff)y.segment<MatrixType::RowsAtCompileTime>(yoff) += (A) * (x).segment<MatrixType::ColsAtCompileTime>(xoff)
+#define _ATXPY(MatrixType,A,x,xoff,y,yoff)y.segment<MatrixType::ColsAtCompileTime>(yoff) += (A).transpose() * (x).segment<MatrixType::RowsAtCompileTime>(xoff)
+
 #ifndef G2O_SPARSE_BLOCK_MATRIX_CCS_H
 #define G2O_SPARSE_BLOCK_MATRIX_CCS_H
 
@@ -122,7 +125,8 @@ namespace g2o {
             const SparseMatrixBlock* a = it->block;
             int srcOffset = rowBaseOfBlock(it->row);
             // destVec += *a.transpose() * srcVec (according to the sub-vector parts)
-            internal::atxpy(*a, srcVec, srcOffset, destVec, destOffset);
+            // internal::atxpy(*a, srcVec, srcOffset, destVec, destOffset);
+            _ATXPY(MatrixType, *a, srcVec, srcOffset, destVec, destOffset);
           }
         }
       }
diff --git a/Thirdparty/g2o/g2o/core/sparse_block_matrix_diagonal.h b/Thirdparty/g2o/g2o/core/sparse_block_matrix_diagonal.h
index 7b13b9f..8605a5f 100644
--- a/Thirdparty/g2o/g2o/core/sparse_block_matrix_diagonal.h
+++ b/Thirdparty/g2o/g2o/core/sparse_block_matrix_diagonal.h
@@ -24,6 +24,9 @@
 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
+#define _AXPY(MatrixType,A,x,xoff,y,yoff)y.segment<MatrixType::RowsAtCompileTime>(yoff) += (A) * (x).segment<MatrixType::ColsAtCompileTime>(xoff)
+#define _ATXPY(MatrixType,A,x,xoff,y,yoff)y.segment<MatrixType::ColsAtCompileTime>(yoff) += (A).transpose() * (x).segment<MatrixType::RowsAtCompileTime>(xoff)
+
 #ifndef G2O_SPARSE_BLOCK_MATRIX_DIAGONAL_H
 #define G2O_SPARSE_BLOCK_MATRIX_DIAGONAL_H
 
@@ -94,7 +97,8 @@ namespace g2o {
           int srcOffset = destOffset;
           const SparseMatrixBlock& A = _diagonal[i];
           // destVec += *A.transpose() * srcVec (according to the sub-vector parts)
-          internal::axpy(A, srcVec, srcOffset, destVec, destOffset);
+          // internal::axpy(A, srcVec, srcOffset, destVec, destOffset);
+          _AXPY(MatrixType, A, srcVec, srcOffset, destVec, destOffset);
         }
       }
 

3 编译成功显示:

1>F:\prjs\ORB_SLAM3\Thirdparty\Pangolin\include\pangolin/gl/gldraw.h(109,1): warning C4267: “参数”: 从“size_t”转换到“GLint”,可能丢失数据
1>  正在创建库 F:/prjs/ORB_SLAM3_Fix/ORB_SLAM3/lib/mono_kitti.lib 和对象 F:/prjs/ORB_SLAM3_Fix/ORB_SLAM3/lib/mono_kitti.exp
1>mono_kitti.vcxproj -> F:\prjs\ORB_SLAM3_Fix\ORB_SLAM3\bin\mono_kitti.exe
1>已完成生成项目“mono_kitti.vcxproj”的操作。
========== 生成: 成功 1 个,失败 0 个,最新 4 个,跳过 0 个 ==========

如下是一个调用gif:

./Monocular/mono_euroc ../Vocabulary/ORBvoc.txt ./Monocular/EuRoC.yaml ./EuRoc_Data/MH_01_easy/ ./Monocular/EuRoC_TimeStamps/MH01.txt result/Resdataset-MH01_mono

执行结果:
编译orb_slam3[windows]

4 可能遇到的问题

4.1 g2o -> vasprint 找不到标识符

到它的声明处,就会发现,它的声明和定义均位于非活动预处理器块中,被宏定义WINDOWS关闭了。所以添加对应的宏定义即可:右键项目->属性->C+±>预处理器->预处理器定义,为其添加一个变量,WINDOWS,保存设定之后,这个错误也就消失了。
以上过程也可以在cmakelist.txt中添加: ADD_DEFINITIONS(-DWINDOWS)

4.2 orb-slam3 -> cl.exe has no C++11 support. Please use a different C++ compiler.

因为cmakelist对于cl的c++11的支持方式需要如下编写:

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -17,19 +17,20 @@ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -march=native")
 
 # Check C++11 or C++0x support
 include(CheckCXXCompilerFlag)
-CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
-CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
-if(COMPILER_SUPPORTS_CXX11)
-   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
-   add_definitions(-DCOMPILEDWITHC11)
-   message(STATUS "Using flag -std=c++11.")
-elseif(COMPILER_SUPPORTS_CXX0X)
-   set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
-   add_definitions(-DCOMPILEDWITHC0X)
-   message(STATUS "Using flag -std=c++0x.")
-else()
-   message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
-endif()
+set (CMAKE_CXX_STANDARD 11)
+# CHECK_CXX_COMPILER_FLAG("-std=c++11" COMPILER_SUPPORTS_CXX11)
+# CHECK_CXX_COMPILER_FLAG("-std=c++0x" COMPILER_SUPPORTS_CXX0X)
+# if(COMPILER_SUPPORTS_CXX11)
+#    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
+#    add_definitions(-DCOMPILEDWITHC11)
+#    message(STATUS "Using flag -std=c++11.")
+# elseif(COMPILER_SUPPORTS_CXX0X)
+#    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
+#    add_definitions(-DCOMPILEDWITHC0X)
+#    message(STATUS "Using flag -std=c++0x.")
+# else()
+#    message(FATAL_ERROR "The compiler ${CMAKE_CXX_COMPILER} has no C++11 support. Please use a different C++ compiler.")
+# endif()

4.3 stdio-gcc.h找不到

将其改为stdio.h即可

4.4 Failed to run MSBuild command: C:/Program Files (x86)/MSBuild/14.0/bin/MSBuild.exe

将上述的MSBuild.exe添加到里面,然后https://developer.microsoft.com/zh-cn/windows/downloads/sdk-archive/
里面找到win8.1的sdk,安装即可。

4.5 fata_error: compiler internal error on msc1.cpp:1xxx编译器内部错误

和g2o/core/matrix_operation.h的代码有关系,参考如下修改:
https://github.com/RainerKuemmerle/g2o/issues/91
https://github.com/UZ-SLAMLab/ORB_SLAM3/pull/53

4.6 std::max找不到定义

添加:

#include <algorithm>

4.7 找不到openssl/md5.h

可以尝试安装strawberry-perl,里面有oepnssl的库,然后下载openssl的源码获得其include。
这里也有openssl编译好的链接库: https://indy.fulgan.com/SSL/LinkLibs/
我的下载地址为:https://indy.fulgan.com/SSL/LinkLibs/openssl-1.0.2g-x64_86-win64_LinkLibs.zip

配置完成后比如一些openssl的依赖就需要手动添加了:
F:\BASE_ENV\openSSL\openssl\include
F:\BASE_ENV\openSSL\openssl\lib\ssleay32.lib
F:\BASE_ENV\openSSL\openssl\lib\libeay32.lib

4.8 link2005 error 将所有项目->属性->c+±>代码生成->MD改成MT

1>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: class std::basic_istream<char,struct std::char_traits<char> > & __cdecl std::basic_istream<char,struct std::char_traits<char> >::operator>>(double &)" (??5?$basic_istream@DU?$char_traits@D@std@@@std@@QEAAAEAV01@AEAN@Z) 已经在 pangolin.lib(widgets.obj) 中定义
1>msvcprt.lib(MSVCP140.dll) : error LNK2005: "public: virtual __cdecl std::basic_iostream<char,struct std::char_traits<char> >::~basic_iostream<char,struct std::char_traits<char> >(void)" (??1?$basic_iostream@DU?$char_traits@D@std@@@std@@UEAA@XZ) 已经在 ORB_SLAM3.lib(System.obj) 中定义
1>libcpmt.lib(locale0.obj) : error LNK2038: 检测到“RuntimeLibrary”的不匹配项: 值“MT_StaticRelease”不匹配值“MD_DynamicRelease”(stereo_euroc.obj 中)
1>libcpmt.lib(locale0.obj) : error LNK2005: "void __cdecl std::_Facet_Register(class std::_Facet_base *)" (?_Facet_Register@std@@YAXPEAV_Facet_base@1@@Z) 已经在 msv

4.9 QT5找不到FindQT5.cmake

# step1: 设置qt的安装位置
set(CMAKE_PREFIX_PATH "/opt/Qt/5.12.3/gcc_64") # windows下应该是msvc2015 或者之后的

# step2: 设置你需要的qt的库的dir
set(Qt5_DIR "${CMAKE_PREFIX_PATH}/lib/cmake/Qt5")
set(Qt5Widgets_DIR "${CMAKE_PREFIX_PATH}/lib/cmake/Qt5Widgets")
set(Qt5Network_DIR "${CMAKE_PREFIX_PATH}/lib/cmake/Qt5Network")
set(Qt5LinguistTools_DIR "${CMAKE_PREFIX_PATH}/lib/cmake/Qt5LinguistTools")

find_package(Qt5 COMPONENTS Widgets Network LinguistTools)
上一篇:maven:将第三方jar包上传到本地仓库或远程仓库


下一篇:22.SpringCloud实战项目-整合OSS对象存储