一、下载RealSense源码
下载地址看 五、参考资料及下载地址
二、下载c++编译器
三、下载cmake
四、编写CMakeLists.txt文件及运行cpp文件
解压RealSense源码压缩包,在所在的目录下创建CMakeLists.txt文件:
# License: Apache 2.0. See LICENSE file in root directory.
# Copyright(c) 2019 Intel Corporation. All Rights Reserved.
# minimum required cmake version: 3.1.0
cmake_minimum_required(VERSION 3.1.0)
project(RealsenseExamplesHelloRealSense)
# 设置.lib和.dll文件所在的目录
if(WIN32)
if("${CMAKE_SIZEOF_VOID_P}" STREQUAL "8")
set(LIBRARY_DIR "C:\\Program Files (x86)\\Intel RealSense SDK 2.0\\lib\\x64") # TODO: Update this variable to correct path - folder where realsense2.lib is found
set(DLL_DIR "C:\\Program Files (x86)\\Intel RealSense SDK 2.0\\bin\\x64") # TODO: Update this variable to correct path - folder where realsense2.dll is found
else()
set(LIBRARY_DIR "C:\\Program Files (x86)\\Intel RealSense SDK 2.0\\lib\\x86") # TODO: Update this variable to correct path - folder where realsense2.lib is found
set(DLL_DIR "C:\\Program Files (x86)\\Intel RealSense SDK 2.0\\bin\\x86") # TODO: Update this variable to correct path - folder where realsense2.dll is found
endif()
set(PROJECT_BINARY_OUTPUT_PATH "${PROJECT_BINARY_DIR}/\$\(Configuration\)") # TODO: Update this variable to correct path - folder to which your project will compile
set(ADDITIONAL_INCLUDE_DIRS "C:\\Program Files (x86)\\Intel RealSense SDK 2.0\\include") # TODO: Update this variable to correct path - folder where librealsense2 folder is found
endif()
# 在.lib中找realsense2库
find_library(REALSENSE2_FOUND realsense2 HINTS ${LIBRARY_DIR} REQUIRED)
if(NOT REALSENSE2_FOUND)
SET(REALSENSE2_FOUND "realsense2")
message(WARN "Failed to find_library(realsense2)")
endif()
# 添加可执行文件
add_executable(rs-hello-realsense rs-hello-realsense.cpp)
target_link_libraries(rs-hello-realsense ${DEPENDENCIES})
target_link_libraries(rs-hello-realsense ${REALSENSE2_FOUND})
# 设置头文件所在的目录
set(ADDITIONAL_INCLUDE_DIRS "C:\\Program Files (x86)\\Intel RealSense SDK 2.0\\include")
include_directories(rs-hello-realsense ${ADDITIONAL_INCLUDE_DIRS})
# set_property(TARGET ...) 相当于set_target_properties(...),在给定的作用域中设置一个命名的属性。
set_property(TARGET rs-hello-realsense PROPERTY CXX_STANDARD 11)
set_target_properties (rs-hello-realsense PROPERTIES FOLDER "Examples")
# 把realsense2.dll复制到当前目录下
if(WIN32)
message(STATUS "Adding Post build script to copy realsense2.dll to project's binary folder")
message(STATUS "Will try to copy from ${DLL_DIR} to ${PROJECT_BINARY_OUTPUT_PATH}")
add_custom_command(TARGET rs-hello-realsense POST_BUILD # Adds a post-build event to rs-hello-realsense
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${DLL_DIR}/realsense2.dll" # <--this is in-file
${PROJECT_BINARY_OUTPUT_PATH}) # <--this is out-file path
endif()
# install(TARGETS MyLib
# EXPORT MyLibTargets
# LIBRARY DESTINATION lib # 动态库安装路径
# ARCHIVE DESTINATION lib # 静态库安装路径
# RUNTIME DESTINATION bin # 可执行文件安装路径
# PUBLIC_HEADER DESTINATION include # 头文件安装路径
# )
install(TARGETS rs-hello-realsense RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
文件目录:
构建程序及运行:
mkdir build && cd build
cmake -G "MinGW Makefiles" ../
mingw32-make.exe all
./rs-hello-realsense.exe