學習日記1——ubuntu環境下學習C++以及安裝PCL庫

buyuanjin發表於2020-09-27

配置環境

安裝build-essential,可以在新立得搜尋然後安裝或者在終端裡輸入:

sudo apt-get install build-essential

Hello Ubuntu C++!

建立一個hello.cpp的檔案,編寫程式碼:

#include <iostream>
using namespace std;
int main()
{
cout << “Hello Ubuntu C++!” << endl;
return 0;
}

第一次就儲存在主目錄。
不然就要尋找路徑cd,否則會報錯

報錯!!
然後在終端輸入:

g++ hello.cpp -o hello
./hello

即可看到程式輸出:Hello Ubuntu C++!

Ubuntu18.04下PCL庫的安裝與測試

安裝依賴庫

sudo apt-get update
sudo apt-get install git build-essential linux-libc-dev
sudo apt-get install cmake cmake-gui
sudo apt-get install libusb-1.0-0-dev libusb-dev libudev-dev
sudo apt-get install mpi-default-dev openmpi-bin openmpi-common
sudo apt-get install libflann1.9 libflann-dev
sudo apt-get install libeigen3-dev
sudo apt-get install libboost-all-dev

安裝metslib

wget https://www.coin-or.org/download/source/metslib/metslib-0.5.3.tgz
tar xzvf metslib-0.5.3.tgz
cd metslib-0.5.3
./configure
make -j{執行緒數}
sudo make install

安裝VTK-8.2.0

sudo apt-get install cmake-curses-gui
sudo apt-get install freeglut3-dev

tar xzvf VTK-8.2.0.tar.gz
cd VTK-8.2.0
mkdir build
cd build
cmake …
make -j6
sudo make install

安裝qt5

sudo apt-get install libqhull* libgtest-dev
sudo apt-get install pkg-config
sudo apt-get install libxmu-dev libxi-dev
sudo apt-get install mono-complete

從github上把PCL原始碼clone下來

git clone https://github.com/PointCloudLibrary/pcl.git

編譯原始碼

cd pcl
mkdir release
cd release
cmake -DCMAKE_BUILD_TYPE=None -DCMAKE_INSTALL_PREFIX=/usr
-DBUILD_GPU=ON -DBUILD_apps=ON -DBUILD_examples=ON
-DCMAKE_INSTALL_PREFIX=/usr …
make

安裝

sudo make install

CMakeLists.txt 語法介紹

1.cmake版本要求
在這句新增滿足你對Cmake特徵需求的最小版本號。

cmake_minimum_required( VERSION 2.8 )

版本查詢
在這裡插入圖片描述
2.設定生成專案名稱(pcl_test)
建立一個工程,括號內pcl_test為自己工程的名字

project(pcl_test)

3.由於我們是建立一個PCL專案,因此需要找到對應的PCL package,如果找不到則專案建立失敗。除此之外,我們還可以使用一下方式:

1)如果是需要某一個PCL的某一個元件:

find_package(PCL 1.9 REQUIRED COMPONENTS io)

2)如果是幾個元件:

find_package(PCL 1.9 REQUIRED COMPONENTS io common)

3)如果需要整個安裝包:

find_package(PCL 1.9 REQUIRED)

如:

find_package( PCL REQUIRED COMPONENT common io )
include_directories( ${PCL_INCLUDE_DIRS} )
add_definitions( ${PCL_DEFINITIONS} )

add_executable:編譯可執行程式,指定編譯,好像也可以新增.o檔案

add_executable (helloDemo demo.cxx demo_b.cxx) #將cxx編譯成可執行檔案——

3.設定變數
當PCL安裝包找到之後,就需要新增對應的包含目錄和依賴庫了。我們需要設定幾個相關的變數:

PCL_FOUND: set to 1 if PCL is found, otherwise unset
PCL_INCLUDE_DIRS: set to the paths to PCL installed headers and the dependency headers
PCL_LIBRARIES: set to the file names of the built and installed PCL libraries
PCL_LIBRARY_DIRS: set to the paths to where PCL libraries and 3rd party dependencies reside
PCL_VERSION: the version of the found PCL
PCL_COMPONENTS: lists all available components
PCL_DEFINITIONS: lists the needed preprocessor definitions and compiler flags

測試程式碼pcl_test.cpp如下:

#include <iostream>
#include <pcl/common/common_headers.h>
#include <pcl/io/pcd_io.h>
#include <pcl/visualization/pcl_visualizer.h>
#include <pcl/visualization/cloud_viewer.h>
#include <pcl/console/parse.h>
 
 
int main(int argc, char **argv) {
    std::cout << "Test PCL !!!" << std::endl;
    
    pcl::PointCloud<pcl::PointXYZRGB>::Ptr point_cloud_ptr (new pcl::PointCloud<pcl::PointXYZRGB>);
    uint8_t r(255), g(15), b(15);
    for (float z(-1.0); z <= 1.0; z += 0.05)
    {
      for (float angle(0.0); angle <= 360.0; angle += 5.0)
      {
	pcl::PointXYZRGB point;
	point.x = 0.5 * cosf (pcl::deg2rad(angle));
	point.y = sinf (pcl::deg2rad(angle));
	point.z = z;
	uint32_t rgb = (static_cast<uint32_t>(r) << 16 |
		static_cast<uint32_t>(g) << 8 | static_cast<uint32_t>(b));
	point.rgb = *reinterpret_cast<float*>(&rgb);
	point_cloud_ptr->points.push_back (point);
      }
      if (z < 0.0)
      {
	r -= 12;
	g += 12;
      }
      else
      {
	g -= 12;
	b += 12;
      }
    }
    point_cloud_ptr->width = (int) point_cloud_ptr->points.size ();
    point_cloud_ptr->height = 1;
    
    pcl::visualization::CloudViewer viewer ("test");
    viewer.showCloud(point_cloud_ptr);
    while (!viewer.wasStopped()){ };
    return 0;
}

簡單的CMakeList.txt

cmake_minimum_required(VERSION 3.10.2)
project(pcl_test)
//set(PCL_DIR //home/baoyuanjin/pcl-pcl-1.9.1)
find_package(PCL 1.9 REQUIRED )

include_directories(${PCL_INCLUDE_DIRS})
link_directories(${PCL_LIBRARY_DIRS})
add_definitions(${PCL_DEFINITIONS})

add_executable(pcl_test pcl_test.cpp)

target_link_libraries (pcl_test ${PCL_LIBRARIES})

install(TARGETS pcl_test RUNTIME DESTINATION bin)

把pcl_test.cpp和CMakeLists.txt發在同一個資料夾後

執行如下命令

cmake .
make
./pcl_test

可以看到:
在這裡插入圖片描述

相關文章