Configure C++ Samples With CMake and Build Them in Visual Studio in Windows

이 자습서는 CMake로 Zivid C++ 샘플을 구성한 다음 Windows에서 Visual Studio로 빌드하는 방법을 보여줍니다.

Requirements

Optional requirements

일부 샘플은 다음과 같은 외부 라이브러리가 필요합니다.

참고

The following instructions have been tested with these versions of the dependencies:

  • Visual Studio 2019 and higher

  • CMake 3.24 and higher

  • PCL 1.12.1

  • Eigen 3.4.0

  • OpenCV 4.7.0

If you are using other versions, you may encounter issues.

Instructions

Clone C++ Samples

Clone the GitHub Zivid C++ Samples repository. To clone it, launch the Command Prompt by pressing Win + R keys on the keyboard, then type cmd and press Enter.

Navigate to a location where you want to clone the repository, e.g.

cd C:\Users\Public

그런 다음 다음 명령을 실행합니다.

git clone https://github.com/zivid/zivid-cpp-samples

Configure the solution with CMake

Run CMake either through the Command Prompt or CMake GUI.

Create a build directory in the directory of zivid-cpp-samples and configure the solution by running:

mkdir build && cd build
cmake -DUSE_EIGEN3=OFF -DUSE_OPENCV=OFF -DUSE_PCL=OFF ../source

Make sure cmake is in your system PATH to use the above command. See the below dropdown for how to add CMake to your system PATH.

Add CMake to the system PATH

On your PC, navigate to Control PanelSystem and SecuritySystemAdvanced System SettingsEnvironment Variables. Under System variables, select the Path variable and click Edit.

Windows의 시스템 정보 스크린샷.

In the Edit environment variable window, click New and add the path to CMake binaries, e.g.

C:\Program Files\CMake\bin

if you have installed CMake in Program Files.

경고

You may have to sign out of your Windows account and in again for the changes to take place.

Open the generated Visual Studio solution ZividCppSamples.sln in File Explorer to further build the samples.

Open CMake GUI and select the source folder and destination for the build folder.

Zivid C++ 샘플에 대한 구성이 포함된 CMake GUI의 스크린샷.

Click Configure. Specify your generator for the project and make sure the (optional) platform is x64. Click Finish.

생성기를 지정하는 CMake GUI의 스크린샷.

Configure 을 다시 클릭합니다.

Screenshot of CMake GUI with dependency errors.

Click OK on the CMake error and disable the variables USE_EIGEN3, USE_OPENCV and USE_PCL to configure the solution without these dependencies.

종속성을 구성할 수 있는 CMake GUI의 스크린샷.

Finally click Configure one last time and click Generate and Open Project to open the Visual Studio solution.

Build the solution

In Visual Studio, change the build configuration from Debug to Release. Build the solution by pressing Ctrl + Shift + B.

C++ 샘플에 대한 Visual Studio 빌드 이벤트의 스크린샷.

Run the sample program

Navigate to the Release folder and run a sample program either by double clicking the exe in File Explorer or using the Command Prompt, e.g.

.\CaptureVis3D.exe
CaptureVis3D 샘플의 스크린샷.

Running samples that require Sample Data

Some of the samples require additional data to run, like point clouds, file cameras, transformations, etc. Go to Sample Data for instructions on how to download and install these.

Configuring the samples with optional dependencies

Some of the samples depend on Point Cloud Library, Eigen, OpenCV and/or Halcon. If you want to run samples with one or more of these dependencies, follow the instructions in the below dropdown.

Optional dependencies

Install the required dependencies based on the sample programs you want to run; see the table below.

Sample program

PCL

Eigen

OpenCV

Halcon

ReadPCLVis3D

YES

CaptureWritePCLVis3D

YES

CaptureHDRVisNormals

YES

StitchByTransformation

YES

StitchByTransformationFromZDF

YES

MaskPointCloud

YES

YES

ROIBoxViaCheckerboard

YES

UtilizeHandEyeCalibration

YES

PoseConversions

YES

ROIBoxViaArucoMarker

YES

YES

CaptureUndistort2D

YES

CreateDepthMap

YES

ProjectAndFindMarker

YES

ReprojectPoints

YES

ReadAndProjectImage

YES

GammaCorrection

YES

AllocateMemoryForPointCloudData

YES

TransformPointCloudViaCheckerboard

YES

TransformPointCloudViaArucoMarker

YES

CaptureHalconViaGenICam

YES

CaptureHalconViaZivid

YES

Download the latest stable release of Eigen from http://eigen.tuxfamily.org/ and extract the folder to your desired destination. When configuring the CMake project, include Eigen to configure the samples that depend on it.

Navigate to the build directory you created in zivid-cpp-samples and run

cmake --fresh -DEIGEN3_INCLUDE_DIR=<path> -DUSE_OPENCV=OFF -DUSE_PCL=OFF ../source

where <path> is the full path to where you installed Eigen, e.g. C:/Program Files/Eigen.

In CMake GUI, click Add Entry to add the path to Eigen. Add EIGEN3_INCLUDE_DIR and point the value to where you extracted the folder.

Eigen 3 포함 디렉토리를 입력할 수 있는 CMake GUI의 스크린샷.

Ensure that the box USE_EIGEN3 is checked and click Configure. Then click Generate and Open Project to open the Visual Studio solution.

Download and install prebuilt PCL binaries by choosing the .exe at https://github.com/PointCloudLibrary/pcl/releases. Check the option to install 3rd Party Libraries.

Windows의 PCL 설치 마법사 스크린샷.

Some settings must be included in the PATH system variable before you can configure the Zivid PCL code samples. On your PC, navigate to Control PanelSystem and SecuritySystemAdvanced System SettingsEnvironment Variables. Under System variables, select the Path variable and click Edit.

Windows의 시스템 정보 스크린샷.

In the Edit environment variable window, click New and add the paths to the PCL binaries and OpenNI2 (3rd party library), e.g.

C:\Program Files\PCL 1.12.1\bin
C:\Program Files\OpenNI2\Tools

if you have installed PCL 1.12.1 and OpenNI2 in Program Files. You may have to sign out of your Windows account and in again for the changes to take place.

경고

You may have to manually link to some of the third-party PCL libraries, like VTK. If you encounter errors, add the path to the binaries of the library in the PATH system variable. E.g. for VTK, you would add C:/Program Files/PCL 1.12.1/3rdParty/VTK/bin if you installed PCL 1.12.1 in Program Files.

When configuring the CMake project, the paths to PCL should already be configured.

Navigate to the build directory you created in zivid-cpp-samples and run

cmake --fresh -DUSE_PCL=ON -DUSE_EIGEN3=OFF -DUSE_OPENCV=OFF ../source

In CMake GUI, ensure that the box USE_PCL is checked and click Configure.

../../../_images/cmake_gui_screenshot_add_pcl.png

Click Generate and Open Project to open the Visual Studio solution.

Download and install prebuilt OpenCV binaries from https://opencv.org/releases/. Extract them to your desired destination, e.g.

C:\Users\Public

Some settings must be included in the PATH system variable before you can configure the Zivid OpenCV code samples. On your PC, navigate to Control PanelSystem and SecuritySystemAdvanced System SettingsEnvironment Variables. Under System variables, select the Path variable and click Edit.

Windows의 시스템 정보 스크린샷.

In the Edit environment variable window, click New and add the path to the OpenCV binaries, e.g.

C:\Program Files\opencv\build\x64\vc16\bin

if you have installed OpenCV 4.7.0 and extracted it to C:/Program Files/opencv.

경고

You may have to sign out of your Windows account and in again for the changes to take place.

When configuring the CMake project, include OpenCV to configure the samples that depend on it.

Navigate to the build directory you created in zivid-cpp-samples and run

cmake --fresh -DOpenCV_DIR=<path> -DUSE_EIGEN3=OFF -DUSE_PCL=OFF ../source

where <path> is the full path to the OpenCV build folder, e.g. C:/Program Files/opencv/build.

In CMake GUI, click Add Entry to add the path to OpenCV. Add OpenCV_DIR and point the value to the OpenCV build folder.

Screenshot of CMake GUI where you can enter OpenCV include directory.

Ensure that the box USE_OPENCV is checked and click Configure. Then click Generate and Open Project to open the Visual Studio solution.

You need Halcon installed on your PC to run the Halcon C++ samples. Go to Install Zivid + HALCON for Windows for further instructions.

After you have installed Halcon, configure CMake to use Halcon samples.

Navigate to the build directory you created in zivid-cpp-samples and run

cmake --fresh -DUSE_HALCON=ON -DUSE_EIGEN3=OFF -DUSE_OPENCV=OFF -DUSE_PCL=OFF ../source

Ensure that the box USE_HALCON is checked and click Configure.

../../../_images/cmake_gui_screenshot_add_halcon.png

Then click Generate and Open Project to open the Visual Studio solution.

If you want to configure multiple dependencies at once, you can simply add multiple dependencies at the same time in CMake. For example, if you want to use all samples that require Eigen, PCL and OpenCV, you can run

cmake --fresh -DEIGEN3_INCLUDE_DIR=<path-to-eigen> -DUSE_PCL=ON -DOpenCV_DIR=<path-to-opencv> ../source

where <path-to-eigen> and <path-to-opencv> are the paths to your Eigen and OpenCV folders.

Debug the sample program

To debug a sample you must add C:/Program Files/Zivid/bin_debug to your PATH environment variable. Follow the instructions in the below dropdown to do this.

Debugging a sample program

PC에서 다음으로 이동합니다. Control PanelSystem and SecuritySystemAdvanced System SettingsEnvironment Variables.

Windows의 시스템 정보 스크린샷.

Under System variables, select Path variable and click Edit.

Windows의 시스템 정보 스크린샷.

Environment variables 창에서 New (창의 오른쪽 상단 모서리, 사용자 변수 섹션)를 클릭하고 다음 값을 추가합니다.

C:\Program Files\Zivid\bin_debug
PATH 변수에 :code:`C:\Program Files\Zivid\bin_debug` 경로를 추가하는 방법을 보여주는 스크린샷.

참고

If you want to debug a program that depends on an input file you must change Working Directory to ${TargetDir}.

Visual Studio에서 디버그용 작업 디렉터리를 설정하는 방법을 보여주는 GIF.

빌드 구성을 Debug 에서 Release 로 변경합니다. 그런 다음 F5 를 클릭하여 디버그로 빌드하고 실행합니다.