Skip to content

Instantly share code, notes, and snippets.

@AirManH
Created May 31, 2021 14:55
Show Gist options
  • Save AirManH/563c87370baf7396e3d71764cf12e3ff to your computer and use it in GitHub Desktop.
Save AirManH/563c87370baf7396e3d71764cf12e3ff to your computer and use it in GitHub Desktop.
Use PCL in modern CMake: A simple example

Motivation

The example CMakeLists.txt in PCL’s doc is a bit outdated. Commands like include_directories, link_directories, add_definitions should be replaced or avoid. Instead, in modern CMake we should use target-specific commands like target_include_directories, target_link_libraries.

Files

File tree:

.
├── CMakeLists.txt
└── main.cpp

CMakeLists.txt

cmake_minimum_required(VERSION 3.16)

project(PCL_TEST)

find_package(PCL REQUIRED)

add_executable(main)

target_sources(main PRIVATE main.cpp)
target_include_directories(main PRIVATE ${PCL_INCLUDE_DIRS})
target_link_libraries(main PRIVATE ${PCL_LIBRARIES})
target_compile_options(main PRIVATE ${PCL_DEFINITIONS})

main.cpp (from this page)

#include <iostream>
#include <pcl/io/pcd_io.h>
#include <pcl/point_types.h>

int
  main (int argc, char** argv)
{
  pcl::PointCloud<pcl::PointXYZ> cloud;

  // Fill in the cloud data
  cloud.width    = 5;
  cloud.height   = 1;
  cloud.is_dense = false;
  cloud.points.resize (cloud.width * cloud.height);

  for (auto& point: cloud)
  {
    point.x = 1024 * rand () / (RAND_MAX + 1.0f);
    point.y = 1024 * rand () / (RAND_MAX + 1.0f);
    point.z = 1024 * rand () / (RAND_MAX + 1.0f);
  }

  pcl::io::savePCDFileASCII ("test_pcd.pcd", cloud);
  std::cerr << "Saved " << cloud.size () << " data points to test_pcd.pcd." << std::endl;

  for (const auto& point: cloud)
    std::cerr << "    " << point.x << " " << point.y << " " << point.z << std::endl;

  return (0);
}

Usage

A cli example:

cmake -B build /path/to/dir/contains/CMakeListsTXT
cd build
make
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment