0

I am trying to compile a pcl-included c++ function into a shared library using g++(Of course, I can use cmake but I found results not changing).

The test code is quite simple:

#include <iostream>
#include <pcl/ModelCoefficients.h>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>
#include <pcl/sample_consensus/method_types.h>
#include <pcl/sample_consensus/model_types.h>
#include <pcl/segmentation/sac_segmentation.h>
#include <pcl/filters/voxel_grid.h>
#include <pcl/filters/extract_indices.h>
extern "C" int add_one(int i)
{
    // pcl::PCLPointCloud2::Ptr cloud_blob (new pcl::PCLPointCloud2), cloud_filtered_blob (new pcl::PCLPointCloud2);
    return i+1;
}

int main()
{
  return 0;
}

The g++ compilation command is:

g++ -shared -L/usr/lib/x86_64-linux-gnu/ -I/usr/include/pcl-1.7 -I/usr/include/eigen3 -o testlib.so -fPIC extract_indices.cpp

I can successfully compile it. I write a Python wrapper as :

import sys
import os
from ctypes import cdll
lib = cdll.LoadLibrary('./testlib.so')

print lib.add_one(5)

When I run this, it shows :

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    lib = cdll.LoadLibrary('./testlib.so')
  File "/home/weizhang/anaconda2/lib/python2.7/ctypes/__init__.py", line 444, in LoadLibrary
    return self._dlltype(name)
  File "/home/weizhang/anaconda2/lib/python2.7/ctypes/__init__.py", line 366, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: ./testlib.so: undefined symbol: _ZN5boost6system15system_categoryEv

After a little debugging, I found out #include <pcl/io/pcd_io.h> this line is the one causing the problem. Any other inclusion is fine.

I have no clue of why that happens.

Any idea is helpful and appreciated!

Wei
  • 305
  • 5
  • 18
  • The method complained about demangles to `boost::system::system_category()`. Maybe try adding `-lboost_system -lboost_filesystem` to the `g++` command, per https://stackoverflow.com/questions/43424167/boost-undefined-references? – Ollin Boer Bohan Nov 09 '18 at 23:27
  • @OllinBoerBohanThanks for the reply. Unfortunately, that doesn't seem to work. Now, my command is `g++ -shared -lboost_system -lboost_filesystem -L/usr/lib/x86_64-linux-gnu/ -I/usr/include/pcl-1.7 -I/usr/include/eigen3 -o testlib.so -fPIC extract_indices.cpp`. It still returns the same error. Also, I think both files are in `-L/usr/lib/x86_64-linux-gnu/`, which is included in the original command. – Wei Nov 09 '18 at 23:45
  • @OllinBoerBohanAlso, when I compile it as executable , it has no problem. It only happens after I compile it as shared lib. – Wei Nov 10 '18 at 00:10

1 Answers1

0

It turns out that there is simple solution to this issue. Essentially, this is resulted from the order g++ compiler. A good explanation of the order of g++ can be found here.

After I change the order to be :

g++ -Wall -shared -fPIC extract_indices.cpp -o testlib.so -L/usr/lib/x86_64-linux-gnu/ -I/usr/include/pcl-1.7 -I/usr/include/eigen3 -lboost_system -lboost_filesystem

It then works out.

Wei
  • 305
  • 5
  • 18