From d2db20106d292d732684fb9c365328a52ed9d115 Mon Sep 17 00:00:00 2001 From: Mateo de Mayo Date: Mon, 15 Aug 2022 15:52:17 +0000 Subject: [PATCH] Fix eigen ODR violations by limiting exported symbols on libbasalt.so --- CMakeLists.txt | 52 ++++++++++++++++++------------ README.md | 8 ++--- cmake_modules/basalt.map | 25 +++++++++++++++ src/monado/slam_tracker.cpp | 58 ++++++++++++---------------------- src/monado/slam_tracker_ui.hpp | 3 +- test/CMakeLists.txt | 12 +++---- thirdparty/basalt-headers | 2 +- 7 files changed, 88 insertions(+), 72 deletions(-) create mode 100644 cmake_modules/basalt.map diff --git a/CMakeLists.txt b/CMakeLists.txt index 71cc8f4..9c47706 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -48,7 +48,6 @@ set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME_LOWERCASE}_${CPACK_PACKAGE_VERSION_M set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE") include(CPack) - # Configure CCache if available if (NOT CMAKE_C_COMPILER_LAUNCHER AND NOT CMAKE_CXX_COMPILER_LAUNCHER) find_program(CCACHE_PROGRAM ccache) @@ -289,12 +288,12 @@ add_library(basalt::magic_enum INTERFACE IMPORTED) set_property(TARGET basalt::magic_enum PROPERTY INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/magic_enum/include) # main library -add_library(basalt SHARED) +add_library(basalt_internal STATIC) # List all header and source files with target_sources. # This ensures, e.g., that QtCreator >= 6.0 properly recognize these headers to belong to the project. # To support cmake < 3.13, use absolute paths (see: https://crascit.com/2016/01/31/enhanced-source-file-handling-with-target_sources/) -target_sources(basalt +target_sources(basalt_internal PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include/basalt/calibration/aprilgrid.h ${CMAKE_CURRENT_SOURCE_DIR}/include/basalt/calibration/calibration_helper.h @@ -365,7 +364,6 @@ target_sources(basalt ${CMAKE_CURRENT_SOURCE_DIR}/src/linearization/linearization_abs_sc.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/linearization/linearization_base.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/linearization/linearization_rel_sc.cpp - ${CMAKE_CURRENT_SOURCE_DIR}/src/monado/slam_tracker.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/optical_flow/optical_flow.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/keypoints.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/utils/system_utils.cpp @@ -382,45 +380,48 @@ target_sources(basalt ${CMAKE_CURRENT_SOURCE_DIR}/src/vi_estimator/vio_estimator.cpp ) -target_link_libraries(basalt +target_link_libraries(basalt_internal PUBLIC ${STD_CXX_FS} basalt::opencv basalt::basalt-headers TBB::tbb pangolin basalt::cli11 basalt::monado PRIVATE basalt::magic_enum rosbag apriltag opengv nlohmann::json fmt::fmt) -target_include_directories(basalt PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) -target_compile_definitions(basalt PUBLIC ${BASALT_COMPILE_DEFINITIONS}) -#target_compile_definitions(basalt PUBLIC BASALT_DISABLE_ASSERTS) +target_include_directories(basalt_internal PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include) +target_compile_definitions(basalt_internal PUBLIC ${BASALT_COMPILE_DEFINITIONS}) +#target_compile_definitions(basalt_internal PUBLIC BASALT_DISABLE_ASSERTS) +add_library(basalt SHARED) +target_sources(basalt PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/monado/slam_tracker.cpp) +target_link_libraries(basalt basalt_internal) add_executable(basalt_calibrate src/calibrate.cpp src/calibration/cam_calib.cpp) -target_link_libraries(basalt_calibrate basalt pangolin basalt::cli11) +target_link_libraries(basalt_calibrate basalt_internal pangolin basalt::cli11) add_executable(basalt_calibrate_imu src/calibrate_imu.cpp src/calibration/cam_imu_calib.cpp) -target_link_libraries(basalt_calibrate_imu basalt pangolin basalt::cli11) +target_link_libraries(basalt_calibrate_imu basalt_internal pangolin basalt::cli11) add_executable(basalt_vio_sim src/vio_sim.cpp) -target_link_libraries(basalt_vio_sim basalt pangolin basalt::cli11) +target_link_libraries(basalt_vio_sim basalt_internal pangolin basalt::cli11) add_executable(basalt_mapper_sim src/mapper_sim.cpp) -target_link_libraries(basalt_mapper_sim basalt pangolin basalt::cli11) +target_link_libraries(basalt_mapper_sim basalt_internal pangolin basalt::cli11) # mapper sim native doesn't use template free interface if(BASALT_INSTANTIATIONS_DOUBLE) add_executable(basalt_mapper_sim_naive src/mapper_sim_naive.cpp) - target_link_libraries(basalt_mapper_sim_naive basalt pangolin basalt::cli11) + target_link_libraries(basalt_mapper_sim_naive basalt_internal pangolin basalt::cli11) endif() add_executable(basalt_mapper src/mapper.cpp) -target_link_libraries(basalt_mapper basalt pangolin basalt::cli11) +target_link_libraries(basalt_mapper basalt_internal pangolin basalt::cli11) add_executable(basalt_opt_flow src/opt_flow.cpp) -target_link_libraries(basalt_opt_flow basalt pangolin basalt::cli11) +target_link_libraries(basalt_opt_flow basalt_internal pangolin basalt::cli11) add_executable(basalt_vio src/vio.cpp) -target_link_libraries(basalt_vio basalt pangolin basalt::cli11) +target_link_libraries(basalt_vio basalt_internal pangolin basalt::cli11) add_executable(basalt_time_alignment src/time_alignment.cpp) -target_link_libraries(basalt_time_alignment basalt pangolin basalt::cli11) +target_link_libraries(basalt_time_alignment basalt_internal pangolin basalt::cli11) add_executable(basalt_kitti_eval src/kitti_eval.cpp) target_link_libraries(basalt_kitti_eval basalt::basalt-headers basalt::cli11) @@ -428,15 +429,24 @@ target_link_libraries(basalt_kitti_eval basalt::basalt-headers basalt::cli11) find_package(realsense2 QUIET) if(realsense2_FOUND) add_executable(basalt_rs_t265_record src/rs_t265_record.cpp src/device/rs_t265.cpp) - target_link_libraries(basalt_rs_t265_record basalt realsense2::realsense2 pangolin basalt::cli11) + target_link_libraries(basalt_rs_t265_record basalt_internal realsense2::realsense2 pangolin basalt::cli11) add_executable(basalt_rs_t265_vio src/rs_t265_vio.cpp src/device/rs_t265.cpp) - target_link_libraries(basalt_rs_t265_vio basalt realsense2::realsense2 pangolin basalt::cli11) + target_link_libraries(basalt_rs_t265_vio basalt_internal realsense2::realsense2 pangolin basalt::cli11) endif() +# Let's export only the slam_tracker.hpp functions for libORB_SLAM3.so, +# But will let a libORB_SLAM3_EXAMPLES.so for the regular ORB-SLAM3 examples to use +# if the user builds with -DBUILD_EXAMPLES=On +set_target_properties(${PROJECT_NAME} PROPERTIES + C_VISIBILITY_PRESET hidden + CXX_VISIBILITY_PRESET hidden + VISIBILITY_INLINES_HIDDEN YES + LINK_DEPENDS ${CMAKE_CURRENT_LIST_DIR}/cmake_modules/basalt.map + LINK_FLAGS "-Wl,--version-script=${CMAKE_CURRENT_LIST_DIR}/cmake_modules/basalt.map" +) - -install(TARGETS basalt_calibrate basalt_calibrate_imu basalt_vio_sim basalt_mapper_sim basalt_mapper basalt_opt_flow basalt_vio basalt_kitti_eval basalt_time_alignment basalt +install(TARGETS basalt basalt_calibrate basalt_calibrate_imu basalt_vio_sim basalt_mapper_sim basalt_mapper basalt_opt_flow basalt_vio basalt_kitti_eval basalt_time_alignment EXPORT BasaltTargets RUNTIME DESTINATION ${CMAKE_INSTALL_PREFIX}/bin LIBRARY DESTINATION ${CMAKE_INSTALL_PREFIX}/lib diff --git a/README.md b/README.md index ae47aed..8c070b1 100644 --- a/README.md +++ b/README.md @@ -106,12 +106,8 @@ This step is optional but you can try Basalt without Monado with one of the foll ### Monado Specifics -You'll need to compile Monado with the same Eigen used in Basalt, and with the -same flags. For that, set these with CMake: -`-DEIGEN3_INCLUDE_DIR=$bsltdeps/basalt/thirdparty/basalt-headers/thirdparty/eigen --DCMAKE_C_FLAGS="-march=native" -DCMAKE_CXX_FLAGS="-march=native"` otherwise -Monado will automatically use your system's Eigen, and having mismatched Eigen -version/flags can cause a lot of headaches. +Note: be careful when manually enabling ASan when building Monado as some +crashes have been reported. I'm still trying to figure out why those happen. Run an OpenXR app like `hello_xr` with the following environment variables set diff --git a/cmake_modules/basalt.map b/cmake_modules/basalt.map new file mode 100644 index 0000000..d9d9411 --- /dev/null +++ b/cmake_modules/basalt.map @@ -0,0 +1,25 @@ +LIBXRTSLAM_BASALT_4.0.0 { + global: + extern "C++" { + xrt::auxiliary::tracking::slam::slam_tracker::slam_tracker*; + xrt::auxiliary::tracking::slam::slam_tracker::*slam_tracker*; + + xrt::auxiliary::tracking::slam::slam_tracker::initialize*; + xrt::auxiliary::tracking::slam::slam_tracker::start*; + xrt::auxiliary::tracking::slam::slam_tracker::is_running*; + xrt::auxiliary::tracking::slam::slam_tracker::stop*; + xrt::auxiliary::tracking::slam::slam_tracker::finalize*; + + xrt::auxiliary::tracking::slam::slam_tracker::push_imu_sample*; + xrt::auxiliary::tracking::slam::slam_tracker::push_frame*; + xrt::auxiliary::tracking::slam::slam_tracker::try_dequeue_pose*; + + xrt::auxiliary::tracking::slam::slam_tracker::supports_feature*; + xrt::auxiliary::tracking::slam::slam_tracker::use_feature*; + + xrt::auxiliary::tracking::slam::IMPLEMENTATION_VERSION_MAJOR*; + xrt::auxiliary::tracking::slam::IMPLEMENTATION_VERSION_MINOR*; + xrt::auxiliary::tracking::slam::IMPLEMENTATION_VERSION_PATCH* + }; + local: *; +}; diff --git a/src/monado/slam_tracker.cpp b/src/monado/slam_tracker.cpp index 65d9897..bf35445 100644 --- a/src/monado/slam_tracker.cpp +++ b/src/monado/slam_tracker.cpp @@ -22,11 +22,17 @@ #include #include "basalt/utils/vis_utils.h" +#if defined(_WIN32) || defined(__CYGWIN__) +#define EXPORT __declspec(dllexport) +#else +#define EXPORT __attribute__((visibility("default"))) +#endif + namespace xrt::auxiliary::tracking::slam { -const int IMPLEMENTATION_VERSION_MAJOR = HEADER_VERSION_MAJOR; -const int IMPLEMENTATION_VERSION_MINOR = HEADER_VERSION_MINOR; -const int IMPLEMENTATION_VERSION_PATCH = HEADER_VERSION_PATCH; +EXPORT extern const int IMPLEMENTATION_VERSION_MAJOR = HEADER_VERSION_MAJOR; +EXPORT extern const int IMPLEMENTATION_VERSION_MINOR = HEADER_VERSION_MINOR; +EXPORT extern const int IMPLEMENTATION_VERSION_PATCH = HEADER_VERSION_PATCH; using std::cout; using std::make_shared; @@ -41,28 +47,6 @@ using std::unordered_set; using std::vector; using namespace basalt; -string imu2str(const imu_sample &s) { - string str = "imu_sample "; - str += "t=" + to_string(s.timestamp) + " "; - str += "a=[" + to_string(s.ax) + ", " + to_string(s.ay) + ", " + to_string(s.az) + "] "; - str += "w=[" + to_string(s.wx) + ", " + to_string(s.wy) + ", " + to_string(s.wz) + "]"; - return str; -} - -string img2str(const img_sample &s) { - string str = "img_sample "; - str += s.is_left ? "left " : "right "; - str += "t=" + to_string(s.timestamp); - return str; -} - -string pose2str(const pose &p) { - string str = "pose "; - str += "p=[" + to_string(p.px) + ", " + to_string(p.py) + ", " + to_string(p.pz) + "] "; - str += "r=[" + to_string(p.rx) + ", " + to_string(p.ry) + ", " + to_string(p.rz) + ", " + to_string(p.rw) + "]"; - return str; -} - static const vector timing_titles{ "frame_ts", "tracker_received", @@ -547,29 +531,29 @@ struct slam_tracker::implementation { void enable_pose_ext_features(bool enable) { pose_features_enabled = enable; } }; -slam_tracker::slam_tracker(const string &config_file) { impl = make_unique(config_file); } +EXPORT slam_tracker::slam_tracker(const string &config_file) { impl = make_unique(config_file); } -slam_tracker::~slam_tracker() = default; +EXPORT slam_tracker::~slam_tracker() = default; -void slam_tracker::initialize() { impl->initialize(); } +EXPORT void slam_tracker::initialize() { impl->initialize(); } -void slam_tracker::start() { impl->start(); } +EXPORT void slam_tracker::start() { impl->start(); } -void slam_tracker::stop() { impl->stop(); } +EXPORT void slam_tracker::stop() { impl->stop(); } -void slam_tracker::finalize() { impl->finalize(); } +EXPORT void slam_tracker::finalize() { impl->finalize(); } -bool slam_tracker::is_running() { return impl->is_running(); } +EXPORT bool slam_tracker::is_running() { return impl->is_running(); } -void slam_tracker::push_imu_sample(const imu_sample &s) { impl->push_imu_sample(s); } +EXPORT void slam_tracker::push_imu_sample(const imu_sample &s) { impl->push_imu_sample(s); } -void slam_tracker::push_frame(const img_sample &sample) { impl->push_frame(sample); } +EXPORT void slam_tracker::push_frame(const img_sample &sample) { impl->push_frame(sample); } -bool slam_tracker::try_dequeue_pose(pose &pose) { return impl->try_dequeue_pose(pose); } +EXPORT bool slam_tracker::try_dequeue_pose(pose &pose) { return impl->try_dequeue_pose(pose); } -bool slam_tracker::supports_feature(int feature_id) { return impl->supports_feature(feature_id); } +EXPORT bool slam_tracker::supports_feature(int feature_id) { return impl->supports_feature(feature_id); } -bool slam_tracker::use_feature(int feature_id, const shared_ptr ¶ms, shared_ptr &result) { +EXPORT bool slam_tracker::use_feature(int feature_id, const shared_ptr ¶ms, shared_ptr &result) { return impl->use_feature(feature_id, params, result); } diff --git a/src/monado/slam_tracker_ui.hpp b/src/monado/slam_tracker_ui.hpp index 98cf51d..a2c8efd 100644 --- a/src/monado/slam_tracker_ui.hpp +++ b/src/monado/slam_tracker_ui.hpp @@ -1,5 +1,6 @@ #pragma once +#include "pangolin/display/display.h" #include #include @@ -230,7 +231,7 @@ class slam_tracker_ui { pangolin::FinishFrame(); } - pangolin::DestroyWindow(window_name); + pangolin::QuitAll(); cout << "Finished ui_runner\n"; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7583fd7..12d4c13 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -6,22 +6,22 @@ include_directories(../thirdparty/basalt-headers/test/include) add_executable(test_spline_opt src/test_spline_opt.cpp) -target_link_libraries(test_spline_opt gtest gtest_main basalt) +target_link_libraries(test_spline_opt gtest gtest_main basalt_internal) add_executable(test_vio src/test_vio.cpp) -target_link_libraries(test_vio gtest gtest_main basalt) +target_link_libraries(test_vio gtest gtest_main basalt_internal) add_executable(test_nfr src/test_nfr.cpp) -target_link_libraries(test_nfr gtest gtest_main basalt) +target_link_libraries(test_nfr gtest gtest_main basalt_internal) add_executable(test_qr src/test_qr.cpp) -target_link_libraries(test_qr gtest gtest_main basalt) +target_link_libraries(test_qr gtest gtest_main basalt_internal) add_executable(test_linearization src/test_linearization.cpp) -target_link_libraries(test_linearization gtest gtest_main basalt) +target_link_libraries(test_linearization gtest gtest_main basalt_internal) add_executable(test_patch src/test_patch.cpp) -target_link_libraries(test_patch gtest gtest_main basalt) +target_link_libraries(test_patch gtest gtest_main basalt_internal) enable_testing() diff --git a/thirdparty/basalt-headers b/thirdparty/basalt-headers index ff99f3d..233c6b9 160000 --- a/thirdparty/basalt-headers +++ b/thirdparty/basalt-headers @@ -1 +1 @@ -Subproject commit ff99f3d040fb6aeb3535338ba69c6300191c9bda +Subproject commit 233c6b901771f50a6a135b583a38885f0c3d6ed9