brew (bottled) and compiled from sourceBuilding CMake projects with PCL 1.9.1 using Xcode provides library not found for -lflann.
Flann to be linked correctly using full path to the library.
-lflann and -lflann_cpp are passed to the linker that cannot find the libraries.
_Note that using make facilities from command line, everything works just fine._
Just try the following updated version of the kdtree tutorial
cmake_minimum_required(VERSION 3.1 FATAL_ERROR)
project(kdtree_search)
find_package(PCL REQUIRED)
add_executable(kdtree_search main.cpp)
target_link_libraries(kdtree_search ${PCL_LIBRARIES})
#include <pcl/point_cloud.h>
#include <pcl/kdtree/kdtree_flann.h>
#include <iostream>
#include <vector>
#include <ctime>
int main (int argc, char** argv)
{
srand (time (NULL));
pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
// Generate pointcloud data
cloud->width = 1000;
cloud->height = 1;
cloud->points.resize (cloud->width * cloud->height);
for (size_t i = 0; i < cloud->points.size (); ++i)
{
cloud->points[i].x = 1024.0f * rand () / (RAND_MAX + 1.0f);
cloud->points[i].y = 1024.0f * rand () / (RAND_MAX + 1.0f);
cloud->points[i].z = 1024.0f * rand () / (RAND_MAX + 1.0f);
}
pcl::KdTreeFLANN<pcl::PointXYZ> kdtree;
kdtree.setInputCloud (cloud);
pcl::PointXYZ searchPoint;
searchPoint.x = 1024.0f * rand () / (RAND_MAX + 1.0f);
searchPoint.y = 1024.0f * rand () / (RAND_MAX + 1.0f);
searchPoint.z = 1024.0f * rand () / (RAND_MAX + 1.0f);
// K nearest neighbor search
int K = 10;
std::vector<int> pointIdxNKNSearch(K);
std::vector<float> pointNKNSquaredDistance(K);
std::cout << "K nearest neighbor search at (" << searchPoint.x
<< " " << searchPoint.y
<< " " << searchPoint.z
<< ") with K=" << K << std::endl;
if ( kdtree.nearestKSearch (searchPoint, K, pointIdxNKNSearch, pointNKNSquaredDistance) > 0 )
{
for (size_t i = 0; i < pointIdxNKNSearch.size (); ++i)
std::cout << " " << cloud->points[ pointIdxNKNSearch[i] ].x
<< " " << cloud->points[ pointIdxNKNSearch[i] ].y
<< " " << cloud->points[ pointIdxNKNSearch[i] ].z
<< " (squared distance: " << pointNKNSquaredDistance[i] << ")" << std::endl;
}
// Neighbors within radius search
std::vector<int> pointIdxRadiusSearch;
std::vector<float> pointRadiusSquaredDistance;
float radius = 256.0f * rand () / (RAND_MAX + 1.0f);
std::cout << "Neighbors within radius search at (" << searchPoint.x
<< " " << searchPoint.y
<< " " << searchPoint.z
<< ") with radius=" << radius << std::endl;
if ( kdtree.radiusSearch (searchPoint, radius, pointIdxRadiusSearch, pointRadiusSquaredDistance) > 0 )
{
for (size_t i = 0; i < pointIdxRadiusSearch.size (); ++i)
std::cout << " " << cloud->points[ pointIdxRadiusSearch[i] ].x
<< " " << cloud->points[ pointIdxRadiusSearch[i] ].y
<< " " << cloud->points[ pointIdxRadiusSearch[i] ].z
<< " (squared distance: " << pointRadiusSquaredDistance[i] << ")" << std::endl;
}
return 0;
}
I did not find a clean solution to the problem yet, and I'm looking for opinions here. I can then fix the issue with a PR.
As a workaround, if I comment out these lines FindFLANN.cmake
https://github.com/PointCloudLibrary/pcl/blob/3a71c5c505c6d58697a218f6184a2a4326ec0157/cmake/Modules/FindFLANN.cmake#L20-L25
and hence avoiding pkg_config to find the library, I can compile the code without problems.
This works because the PCLConfig.cmake has
get_filename_component(FLANN_ROOT "/usr/local/Cellar/flann/1.9.1_6/include" PATH)
that sets FLANN_ROOT to /usr/local/Cellar/flann/1.9.1_6 and is used as hint in the FindFLANN.cmake.
There are quite few things here that I do not understand, but I would go in order trying to understand the specific problem around flann.
Possibly related to #2676 (closed for inactivity).
Just to add that I was able to verify this also on my environment.
By my understandning of the problem, looking for flann using pkg_config is ok, but you need to use the returned information to then look for the library full path. Once you do that, you are good to go.
There is some CMake code to write (look at it here), but were I work we have a module that does that automagically and also creates a CMake target associated to the package. Very helpful!
Here is the file, that we could use in total or in part to address thi problem and potentially provide a standard way to handle dependencies that uses pkg_config.
The FLANN target patch got merged. Let me know once you get to try it just to confirm I can close this.
Ok, I'll test this asap (was out of office).
Finally recompiling and testing!
Fixed! Everything works flawlessly! 馃帀
Most helpful comment
Just to add that I was able to verify this also on my environment.