Compile libtorch with pcl cause segmentation fault

I try to compile libtorch with PCL library. Both the libraries work fine when compiled dependently, but segmentation fault occurs when complie these libraries together.

  • cmake file:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(custom_ops)
SET(CMAKE_BUILD_TYPE "Debug")

list(APPEND CMAKE_PREFIX_PATH ${CMAKE_CURRENT_SOURCE_DIR}/libtorch)

find_package(Torch REQUIRED)
find_package(PCL 1.8 REQUIRED COMPONENTS common io visualization)

include_directories(
        ${PCL_INCLUDE_DIRS}
)

add_definitions(${PCL_DEFINITIONS})

add_executable(pytorch2pt src/main.cpp)
target_link_libraries(pytorch2pt ${PCL_LIBRARIES} ${TORCH_LIBRARIES})
set_property(TARGET pytorch2pt PROPERTY CXX_STANDARD 11)
  • main.cpp (I remove the code related to libtorch, but the seg fault still occur)

// #include <torch/script.h>
#include <iostream>
#include <memory>
#include <vector>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/cloud_viewer.h>

int main() {
    std::string test_data = "./test.pcd"; # path to your pcd file

    pcl::PointCloud<pcl::PointXYZI>::Ptr in_cloud_ptr(new pcl::PointCloud<pcl::PointXYZI>);
    if (pcl::io::loadPCDFile<pcl::PointXYZI>(test_data.c_str(), *in_cloud_ptr) == -1) # segmetation fault here
    {
        PCL_ERROR ("Couldn't read pcd file \n");
        return (-1);
    }

}

Besides, when I compile the project with “Release” mode, I get the information:

CMakeFiles/pytorch2pt.dir/src/main.cpp.o: In function `main':
main.cpp:(.text.startup+0x1da): undefined reference to `pcl::PCDReader::read(std::string const&, pcl::PCLPointCloud2&, Eigen::Matrix<float, 4, 1, 0, 4, 1>&, Eigen::Quaternion<float, 0>&, int&, int)'
collect2: error: ld returned 1 exit status
CMakeFiles/pytorch2pt.dir/build.make:345: recipe for target 'pytorch2pt' failed
make[2]: *** [pytorch2pt] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/pytorch2pt.dir/all' failed
make[1]: *** [CMakeFiles/pytorch2pt.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
  • The environment of my computer:
OS: ubuntu 16.04
libtorch: nightly version with CPU only
PCL: 1.7.2 (I have tried v1.8.1, but I got the same issue) 
Boost: 1.58.0
CUDA: 9.0
CUDNN: 7.1.3

Any idea about this issue?

Could you check this thread as your problem seems to be related to it.