windows10編譯Open3D


一、相關環境

系統: windows10
CMake: 3.21.4
Visual Studio: 2019
Open3D: 0.12.0

二、源碼編譯

2.1 下載源碼

git clone --recursive https://github.com/intel-isl/Open3D

如果網上太慢,可以采用加速接口

git clone https://github.com.cnpmjs.org/isl-org/Open3D.git
# git clone https://hub.fastgit.org/isl-org/Open3D.git
# git clone https://github.91chi.fun//https://github.com/isl-org/Open3D.git

2.2 切換到指定分支

下載完后進入Open3D目錄,用git tag查看分支,然后切換到指定分支,這里切換至0.12.0

git checkout v0.12.0

2.3 更新第三方庫

  • 修改Open3D目錄下.gitmodules文件中地址,換成加速地址,如
[submodule "3rdparty/pybind11"]
	path = 3rdparty/pybind11
	url = https://github.com/pybind/pybind11.git

替換為

[submodule "3rdparty/pybind11"]
	path = 3rdparty/pybind11
	url = https://github.com.cnpmjs.org/pybind/pybind11.git
  • 執行以下命令
git submodule sync
git submodule update --init --recursive

2.4 編譯

  • 利用CMake,選擇資源路徑和編譯路徑,依次點擊ConfigureGenerate.
  • 在生成目錄用Visual Studio 2019打開Open3D.sln工程

    CMakePredefinedTargets目錄下選擇ALL_BUILD作為啟動項,然后選擇Release,點擊重新生成進行編譯。
    注意
    • 編譯的時候選擇用管理員身份打開VS2019,因為生成目錄默認在C:/Program Files (x86)目錄下。如果不用管理員身份打開,在CMake Configure時可以修改安裝路徑到非系統盤
    • 另外我編譯過程中出現下載filament庫失敗的情況,我是先下載~\Open3D\3rdparty\filament\filament_download.cmake文件中指定的庫文件,然后存放至~\Open3D\3rdparty_downloads\filament,並將filament_download.cmake文件中的鏈接修改為本地保存路徑。

三、安裝

CMakePredefinedTargets目錄下選擇INSTALL作為啟動項,點擊生成。
自動會保存在C:/Program Files (x86)目錄下。如果安裝成功,該目錄下會有bin,CMake,include,lib四個文件夾。

四、測試

參考C++ interface先下載TestVisualizer.cppCMakeLists.txt
存放在自定義目錄下,然后利用CMake進行ConfigureGenerate

我在測試過程中出現了“RuntimeLibrary”的不匹配項: 值“MT_StaticRelease”不匹配值“MD_DynamicRele的問題。MT(多線程靜態的版本)和MD(多線程動態的版本)以及MTd, MDd的區別可以參考VS 運行庫MT、MD的區別
解決辦法如下
選擇工程,右擊屬性,依次點擊C/C++ ==> 代碼生成 ==> 運行庫,選擇MT
或者直接在CMakeLists.txt文件中添加

set(CMAKE_CXX_FLAGS_RELEASE "/MT")
set(CMAKE_CXX_FLAGS_DEBUG "/MTd")

編譯完成后運行以下命令得到結果:

 .\TestVisualizer.exe pointcloud  "D:/test.pcd"

  • TestVisualizer.cpp
點擊展開:TestVisualizer.cpp
// ----------------------------------------------------------------------------
// -                        Open3D: www.open3d.org                            -
// ----------------------------------------------------------------------------
// The MIT License (MIT)
//
// Copyright (c) 2018 www.open3d.org
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
// ----------------------------------------------------------------------------

#include <iostream>
#include <memory>
#include <thread>

#include <Open3D/Open3D.h>

// A simplified version of examples/Cpp/Visualizer.cpp to demonstrate linking
// an external project to Open3D.
int main(int argc, char *argv[]) {
    using namespace open3d;

    utility::SetVerbosityLevel(utility::VerbosityLevel::Debug);
    if (argc < 3) {
        utility::LogInfo("Open3D {}\n", OPEN3D_VERSION);
        utility::LogInfo("\n");
        utility::LogInfo("Usage:\n");
        utility::LogInfo("    > TestVisualizer [mesh|pointcloud] [filename]\n");
        // CI will execute this file without input files, return 0 to pass
        return 0;
    }

    std::string option(argv[1]);
    if (option == "mesh") {
        auto mesh_ptr = std::make_shared<geometry::TriangleMesh>();
        if (io::ReadTriangleMesh(argv[2], *mesh_ptr)) {
            utility::LogInfo("Successfully read {}\n", argv[2]);
        } else {
            utility::LogWarning("Failed to read {}\n\n", argv[2]);
            return 1;
        }
        mesh_ptr->ComputeVertexNormals();
        visualization::DrawGeometries({mesh_ptr}, "Mesh", 1600, 900);
    } else if (option == "pointcloud") {
        auto cloud_ptr = std::make_shared<geometry::PointCloud>();
        if (io::ReadPointCloud(argv[2], *cloud_ptr)) {
            utility::LogInfo("Successfully read {}\n", argv[2]);
        } else {
            utility::LogWarning("Failed to read {}\n\n", argv[2]);
            return 1;
        }
        cloud_ptr->NormalizeNormals();
        visualization::DrawGeometries({cloud_ptr}, "PointCloud", 1600, 900);
    } else {
        utility::LogWarning("Unrecognized option: {}\n", option);
        return 1;
    }
    utility::LogInfo("End of the test.\n");

    return 0;
}

  • CMakeLists.txt
點擊展開:CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(TestVisualizer)

set(CMAKE_CXX_STANDARD 11)

set(OPEN3D_ROOT "C:/Program Files (x86)/Open3D")

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${Open3D_C_FLAGS}")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${Open3D_CXX_FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${Open3D_EXE_LINKER_FLAGS}")
set(CMAKE_CXX_FLAGS_RELEASE "/MT")
set(CMAKE_CXX_FLAGS_DEBUG "/MTd")

# # 方式一
#link_directories(${OPEN3D_ROOT}/lib)
#add_executable(${PROJECT_NAME} TestVisualizer.cpp)
#target_link_libraries(${PROJECT_NAME} Open3D::Open3D)   # release
#target_include_directories(${PROJECT_NAME} PUBLIC ${OPEN3D_ROOT}/include)

# # 方式二
find_package(Open3D HINTS ${OPEN3D_ROOT}/CMake)
# Set OS-specific things here
if(WIN32)
elseif(CYGWIN)
elseif(APPLE)
elseif(UNIX)
	add_definitions(-DUNIX)
	add_compile_options(-Wno-deprecated-declarations)
	add_compile_options(-Wno-unused-result)
    add_definitions(-O3)
endif(WIN32)

# Open3D
if (Open3D_FOUND)
    message(STATUS "Found Open3D ${Open3D_VERSION}")

    # link_directories must be before add_executable
    link_directories(${Open3D_LIBRARY_DIRS})

    add_executable(TestVisualizer TestVisualizer.cpp)

    target_link_libraries(TestVisualizer ${Open3D_LIBRARIES})

    target_include_directories(TestVisualizer PUBLIC ${Open3D_INCLUDE_DIRS})

    # Hot fix windows dll not found issue, assumming we're using the Release build
    option(BUILD_SHARED_LIBS "Whether Open3D was build as shared library" OFF)
    if(WIN32 AND BUILD_SHARED_LIBS)
        message("Will copy Open3D.dll to ${CMAKE_CURRENT_BINARY_DIR}/Release")
        add_custom_command(TARGET TestVisualizer POST_BUILD
                        COMMAND ${CMAKE_COMMAND} -E copy
                                ${CMAKE_INSTALL_PREFIX}/bin/Open3D.dll
                                ${CMAKE_CURRENT_BINARY_DIR}/Release)
    endif()

else ()
    message(SEND_ERROR "Open3D not found")
endif ()

五、參考鏈接

Compiling from source
C++ interface


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM