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

posted @ 2022-01-17 19:11  半夜打老虎  阅读(2161)  评论(0编辑  收藏  举报