1

I'm trying to read and display an image using OpenCV c++ version in vscode but I keep getting this error:

libpng warning: Application built with libpng-1.4.12 but running with 1.6.37 libc++abi: terminating with uncaught exception of type cv::Exception: OpenCV(4.5.3) /opencv/opencv-4.5.3/modules/highgui/src/window.cpp:1006: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'imshow'

This is my CmakeLists:

cmake_minimum_required(VERSION 3.0.0)
project(C++ VERSION 0.1.0)

include(CTest)
include(FindPkgConfig)
enable_testing()

pkg_check_modules(LIBPNG libpng16 REQUIRED)
if (NOT LIBPNG_FOUND)
   message(FATAL ERROR "libpng16 development libaries not installed")
endif()

message(PNG_LIBRARIES = "${LIBPNG_LIBRARIES}")

find_package(OpenCV REQUIRED)

include_directories(${OpenCV_INCLUDE_DIRS} ${LIBPNG_INCLUDE_DIRS})
link_directories(${LIBPNG_LIBRARY_DIRS})

add_executable(C++ main.cpp)

target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${LIBPNG_LIBRARIES})

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

The make file builds without error and finds the required but I still get the error. This is the build output: build_output

Any suggestions on this issue?

Bane
  • 99
  • 7
  • It's hard to tell why exactly that error occurs. What can be said, is that the application is linked against libpng version 1.6.37, but parts of the application or opencv is built against version 1.4.12. The first thing you could do is to use `cv::getBuildInformation()` and check the Media I/O section to see again which libpng version opencv was built. – t.niese Oct 07 '21 at 07:12
  • Just checked. `PNG: libpng (ver 1.6.37)` so I have no idea why the warning is there – Bane Oct 07 '21 at 07:21
  • `so I have no idea why the warning is there` well that was just one starting point to rule one possible problem out. So given that opencv uses `1.6.37` indicates, that you have installed both `1.4.12` and `1.6.37` on your system. It might be that while cmake finds the version `1.6.37` for the library, it might wrongly identify the `1.4.12` headers as the correct one. that might be either because you have installed `1.6.37` without headers, or that the cmake finds the one of `1.4.12` before the one of `1.6.37`. Next step would be to figure out the installation location of both libpng versions. – t.niese Oct 07 '21 at 07:26
  • I know the location of both versions, hence why I specified `LIBPNG libpng16 REQUIRED` instead of `PNG REQUIRED`. I also used `message()` to check if the directory was correct before including it. Is there another way I could specifiy the location? – Bane Oct 07 '21 at 09:36
  • A library consists of two parts, the binary of the library itself and the headers. With `message(PNG_LIBRARIES = "${LIBPNG_LIBRARIES}")` you log the library to which you link. Which is the `1.6.37` version, that's what `libpng warning: Application built with libpng-1.4.12 but running with 1.6.37` also tells you. The `Application built with libpng-1.4.12` indicates that for some reason the headers of the `1.4.12` version are used when the `#include` in your source files is resolved. – t.niese Oct 07 '21 at 10:13
  • You could try [Listing include_directories in CMake](https://stackoverflow.com/questions/6902149) after your `target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} ${LIBPNG_LIBRARIES})` to check the order of the paths listed there, and check in which of those the `1.4.12` and `1.6.37` headers can be found, and which if the first on to appear. – t.niese Oct 07 '21 at 10:16

1 Answers1

0

In my case I was running the build on a Github Action and a preinstalled Mono was cluttering the build with many unwanted libraries. I fixed by passing -DCMAKE_FIND_FRAMEWORK=NEVER to cmake, as discussed here.

ceztko
  • 14,736
  • 5
  • 58
  • 73