简体   繁体   中英

CMake - How to include and link Pylon library with OpenCV-C++ project

I am trying to access images from a Basler camera interfaced with a Jetson TX1 (Ubuntu 16.04). I am using OpenCV-C++ along with Pylon library to do so. I am trying to link the Pylon using cmake . I have the following CMakeLists.txt file:

cmake_minimum_required(VERSION 3.5.1)
project(basler_test)
set(CMAKE_CXX_STANDARD 14)
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl, -E")
find_package(OpenCV REQUIRED)
include_directories(/opt/pylon5/include)
link_directories(/opt/pylon5/lib64)
add_executable(basler_test basler_test.cpp)
target_link_libraries(basler_test ${OpenCV_LIBS} /opt/pylon5/include/pylon/PylonIncludes.h)

The cmake . command works fine but when I do make , it gives:

fatal error: pylon/Platform.h: No such file or directory compilation terminated

I checked for the above file and it does exist in the same directory as PylonIncludes.h . So, I believe this error is because something has not been set properly in the CMakeLists.txt . I don't have enough experience creating them to identify what's wrong. Kindly help.

Here is the relevant part of the source file: basler_test.cpp

//This is a test program to check the functionality of Basler dart daA2500-14uc Camera.  

#define saveImages 0
#define recordVideo 1

#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
#include <pylon/PylonIncludes.h>
#ifdef PYLON_WIN_BUILD
#include <pylon/PylonGUI.h>
#endif

static const uint32_t c_countOfImagesToGrab = 10;

int main(int argc, char* argv[])
{
         ...................................
         ..................................
}

I have got the camera working. I had to add a few files from /opt/pylon5/lib64 as arguments to target_link_libraries() . My CMakeLists.txt file looks like:

cmake_minimum_required(VERSION 3.5.1)
project(basler_test)
set(CMAKE_CXX_STANDARD 14)
#set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl, -E")
find_package(OpenCV REQUIRED)
include_directories(/opt/pylon5/include)
link_directories(/opt/pylon5/lib64)
add_executable(basler_test basler_test.cpp)
#target_include_directories(basler_test /opt/pylon5/include)
target_link_libraries(basler_test ${OpenCV_LIBS} pylonutility pylonbase GCBase_gcc_v3_0_Basler_pylon_v5_0)

I guess this is the best way to include Pylon library.

In the CMakeLists.txt define where to find Pylon library this way,

find_package(Pylon QUIET)
if (NOT ${Pylon_FOUND})
    include("${CMAKE_CURRENT_SOURCE_DIR}/cmake/FindPylon.cmake")
endif()

Here FindPylon.cmake can be written in this way,

        set(PYLON_ROOT $ENV{PYLON_ROOT})
        if (NOT DEFINED ENV{PYLON_ROOT})
            set(PYLON_ROOT "/opt/pylon5")
        endif()

        set(_PYLON_CONFIG "${PYLON_ROOT}/bin/pylon-config")
        if (EXISTS "${_PYLON_CONFIG}")
            set(Pylon_FOUND TRUE)
            execute_process(COMMAND ${_PYLON_CONFIG} --cflags-only-I OUTPUT_VARIABLE HEADERS_OUT)
            execute_process(COMMAND ${_PYLON_CONFIG} --libs-only-l OUTPUT_VARIABLE LIBS_OUT)
            execute_process(COMMAND ${_PYLON_CONFIG} --libs-only-L OUTPUT_VARIABLE LIBDIRS_OUT)
            string(REPLACE " " ";" HEADERS_OUT "${HEADERS_OUT}")
            string(REPLACE "-I" "" HEADERS_OUT "${HEADERS_OUT}")
            string(REPLACE "\n" "" Pylon_INCLUDE_DIRS "${HEADERS_OUT}")

            string(REPLACE " " ";" LIBS_OUT "${LIBS_OUT}")
            string(REPLACE "-l" "" LIBS_OUT "${LIBS_OUT}")
            string(REPLACE "\n" "" Pylon_LIBRARIES "${LIBS_OUT}")

            string(REPLACE " " ";" LIBDIRS_OUT "${LIBDIRS_OUT}")
            string(REPLACE "-L" "" LIBDIRS_OUT "${LIBDIRS_OUT}")
            string(REPLACE "\n" "" LIBDIRS_OUT "${LIBDIRS_OUT}")

            set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)
            foreach (LIBDIR ${LIBDIRS_OUT})
                link_directories(${LIBDIR})
            endforeach()
        else()
            set(Pylon_FOUND FALSE)
        endif()

Then you can include the Pylon includes files into your lib or executable binary in the following way,

include_directories(
        #add other includes directories 
        ${Pylon_INCLUDE_DIRS}
)

Same way you can link Pylon Lib directory by referring to this: $Pylon_LIBRARIES . Hope this may helpful who will get into this issue again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM