conan-io / conan

Conan - The open-source C and C++ package manager
https://conan.io
MIT License
8.1k stars 959 forks source link

[bug] Linux OpenCV 4.5.1 from Conan center crashes at run time #11591

Open Ju1He1 opened 2 years ago

Ju1He1 commented 2 years ago

Hey guys, I'm using OpenCV from Conan Center. On Windows everything works fine, however on Linux, my application crashes with

error while loading shared libraries: libopencv_imgproc.so.4.5: cannot open shared object file: No such file or directory

running ldd MyExecutable gives me

grafik

libopencv_imageproc is not found. However libopencv_imgcodecs was properly found. I believe there is something missing in the recipe or my project, since parts of opencv are located correctly

-- Conan: checking conan executable
 -- Conan: Found program /usr/local/bin/conan
 -- Conan: Version found Conan version 1.50.0
 -- Conan executing: /usr/local/bin/conan install . --build missing --settings build_type=Debug --settings compiler=gcc --settings compiler.version=10 --settings compiler.libcxx=libstdc++11 --settings compiler.cppstd=20
 Configuration:
 [settings]
 arch=x86_64
arch_build=x86_64
build_type=Debug
 compiler=gcc
 compiler.cppstd=20
 compiler.libcxx=libstdc++11
 compiler.version=10
 os=Linux
os_build=Linux
 [options]
[build_requires]
 [env]

My (simplified) setup looks like this


  list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR})
  list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR})
  if(NOT EXISTS "${CMAKE_BINARY_DIR}/conan.cmake")
    message(STATUS "Downloading conan.cmake from https://github.com/conan-io/cmake-conan")
    file(DOWNLOAD "https://raw.githubusercontent.com/conan-io/cmake-conan/release/0.18/conan.cmake" "${CMAKE_BINARY_DIR}/conan.cmake")
  endif()
  include(${CMAKE_BINARY_DIR}/conan.cmake)
set(PACKAGE_SHARED_OPTION "shared=True")
 set(OPENCV_ADDITIONAL_OPTIONS
        "opencv:${PACKAGE_SHARED_OPTION}"
        "opencv:with_ade=False"
        "opencv:with_eigen=False"
        "opencv:with_ffmpeg=False"
        "opencv:with_openexr=False"
        "opencv:with_png=True"
        "opencv:with_imgcodec_hdr=True"
        "opencv:with_imgcodec_pfm=True"
        "opencv:with_imgcodec_pxm=True"
        "opencv:with_quirc=False"
        "opencv:with_tiff=False"
        "opencv:with_webp=False"
)

foreach(TYPE ${CMAKE_CONFIGURATION_TYPES})
  conan_cmake_configure(
        REQUIRES
        opencv/4.5.1
        OPTIONS
        "${OPENCV_ADDITIONAL_OPTIONS}"
        GENERATORS
        cmake_find_package_multi
        IMPORTS
        "lib, *.dylib* -> ./bin/${TYPE}"
        "bin, *.dll -> ./bin/${TYPE}"
        "lib, *.so* -> ./bin/${TYPE}"
      )
  conan_cmake_autodetect(settings BUILD_TYPE ${TYPE})
        conan_cmake_install(
          PATH_OR_REFERENCE
          .
          BUILD
          all
          SETTINGS
          ${settings}
   )
endforeach()

find_package(OpenCV CONFIG REQUIRED)

my executable setup is as follows

set(EXE_NAME "TestExe")
add_executable(${EXE_NAME} "foo.cpp")
target_link_libraries(${EXE_NAME} PRIVATE "opencv::opencv")

Am I missing some opencv libs in my target_link_libraries()? Why is libopencv_imgcodecs set but not libopencv_imageproc? When I manually append the build path to the LD_LIBRARY_PATH i can run the executable properly. However I'd like to use a more general solution ;)

Other 3rd parties like Gtest and jsoncpp work fine on windows and linux.

Could you help me out here

Environment Details (include every applicable attribute)

jcar87 commented 2 years ago

Hi @Ju1He1! Thanks for reporting this issue.

One option would be to add the virtualrunenv generator, which generates a file activate_run.sh which will set LD_LIBRARY_PATH for you with the correct values. Documentation here: https://docs.conan.io/en/1.46/reference/generators/virtualrunenv.html

Since you are invoking conan from CMake, you would have to add this to your conan_cmake_configure call.

You mention that it works without issues on Windows, this is because of this line:

"bin, *.dll -> ./bin/${TYPE}"

This causes Conan to copy the required dll files and place them next to the executable. While you are also copying the .so files, it should be noted the runtime linker on Linux does not look for shared libraries next to the executable.

On Linux, for other libraries such like GTest and jsoncpp, these will work fine if they dont' have "indirect" dependencies on other libraries. In the case you are reporting, there is a dependency on libopencv_imgcodecs.so, but an indirect dependency on libopencv_imgproc.so

Please let me know if the virtualrunenv solution works. I would like to investigate this further - could you run readelf -d on libopencv_imgcodecs.so and report the output? Thanks for using Conan and Conan Center :)

Ju1He1 commented 2 years ago

Hey @jcar87 thx for your fast answer and your explanations. This make sense now :)

I'd call readelf on those 2 libraries

grafik

grafik

do you have an idea why this is happening

I also added the virtualrunenv runner to conan_cmake_configure(). Adding this allone did not work. Do ich have the call the generated shell scripts manually as well?

Thx so far for your help :)

Ju1He1 commented 2 years ago

hey @jcar87 Unfortunately using the virtualrunenv didn't work for me. I called the generated activate_run.sh via execute_process(), however the declared variables are not properly propagated.

However I found a workaround for me

However this solution only works for the unit tests. A more general solution would be nice :)