Runtime error when running libpng application - c++

I am trying to compile openGL file including "png.h" header file,
I got the following error:
Open GL version 2.1 ATI-3.2.24
libpng warning: Application built with libpng-1.4.12 but running with 1.6.37
error: png_create_read_struct returned 0.
Failed to read image texture from ../images/ceramic.png
My Cmakelists.txt file :
cmake_minimum_required (VERSION 3.13)
project (teapot)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
find_package(OpenGL REQUIRED)
find_package(GLUT REQUIRED)
find_package(PNG REQUIRED)
include_directories(${PNG_INCLUDE_DIR})
add_executable(teapot teapot.cpp)
target_link_libraries(teapot ${OPENGL_gl_LIBRARY} ${GLUT_LIBRARIES} ${PNG_LIBRARIES} )
set(CMAKE_CXX_FLAGS "-I ${CMAKE_OSX_SYSROOT} ${CMAKE_CXX_FLAGS} -std=c++11")
if (APPLE)
set (CMAKE_CXX_FLAGS "-Wno-deprecated-declarations ${CMAKE_CXX_FLAGS}")
endif ()
set_target_properties(teapot PROPERTIES
CXX_STANDARD 11
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
)
cmake . works fine, but when I execute ./teapot the above error occurred. Thanks for any help!

You've probably only have development libraries installed for libpng-1.4.12 and not for libpng-1.6.37 or some other "non-standard" installation.
But the message seems pretty clear you linked against an older version but have a newer version in the runtime path of loadable libraries.
find_package(PNG REQUIRED) is only going to search the "standard" locations via find_library().
You may also want to update your CMakeLists.txt file to use target_link_libraries( ... PNG::PNG). This is simpler than trying to use the PNG variables that get set; which is missing PNG_DEFINITIONS when compiling your project. Refer to the CMake manual buildsystem section about library targets.
You can inspect the variables by using the message() command to print out their values. Some of them will also be stored in CMakeCache.txt.
If libpng isn't in the standard locations then you have to use target_link_libraries( ... /path/to/lib) and target_include_directories( ... /path/to/lib/headers), etcetera to handle it.

Related

LibTorch with OpenCV: version GOMP_5.0 not found

I'm trying to use OpenCV and LibTorch in the same project. Libtorch is installed in /usr/include/libtorch, downloaded from the PyTorch website. I'm using the cxx11 ABI version for CUDA 11.3.
Here's my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.23 FATAL_ERROR)
project(chess-rl VERSION 1.0)
find_package( OpenCV REQUIRED )
set(CMAKE_PREFIX_PATH /usr/include/libtorch/share/cmake/)
find_package(Torch REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories(${OpenCV_LIBS})
include_directories(${TORCH_INCLUDE_DIRS})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
file (GLOB SOURCE_FILES
${PROJECT_SOURCE_DIR}/src/*.cc
${PROJECT_SOURCE_DIR}/src/chess/*.cc
)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBS} "${TORCH_LIBRARIES}" )
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20)
The program has a #include <opencv2/opencv.hpp> line at the top.
Compilation works fine, but running the executable gives me this error:
/usr/include/libtorch/lib/libgomp-52f2fd74.so.1: version `GOMP_5.0' not found (required by /usr/lib/libvtkCommonCore.so.1)
I believe libtorch is trying to use a library that is incompatible with OpenCV.
If I run the program with LD_PRELOAD=/usr/lib/libgomp.so ./build/my-program, it runs fine.
How can I fix this error without having to use that environment variable? Is there a way to link that particular library in CMake?
I faced the same problem, and your thread contributed to one solution.
You need to add a shared library in your CMakelists.txt file.
add_library(libName SHARED IMPORTED)
set_property(TARGET libName PROPERTY IMPORTED_LOCATION "/usr/lib/libgomp.so")
target_link_libraries(PROJECT_NAME
libName)
I hope I could help you
With kind regards

Qt Creator Error: cannot find -lopencv_imgcodecs

I have installed opencv, qt, qt creator, cmake on ubuntu 15.10 through VMware on windows.
The opencv is installed in this directory: /home/majidalaeinia/opencv/
The project repository is cloned in this directory: /home/majidalaeinia/Desktop/imgwarp-opencv/
I want to run the project through its CMakeLists.txt in qt creator and when I press Build now on qt creator, I get the following errors:
error: cannot find -lopencv_imgcodecs
error: collect2: error: ld returned 1 exit status
Where is the problem and how can I solve it?
# Majid Alaeinia, from the CMakeLists.txt file you posted it is not specified how CMAKE should find the libraries requested from your project. Also there are no target_link_libraries declared so CMAKE does not know where to link them. Hopefully the following small example template should be helpful for your project:
cmake_minimum_required (VERSION 3.1)
project(yourProject)
find_package( OpenCV REQUIRED )
find_package( Qt5 REQUIRED COMPONENTS Sql )
### this is for c++11
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
### QT stuff if you want a GUI
set(CMAKE_AUTOMOC ON) # autogenerate qt gui features
set(CMAKE_AUTORCC ON) # used for QT resource Files (if you need)
## Additional operation...
# From here you are specifically linking all OpenCV libraries and executables
### Add executables
add_executable(yourExecutable main/main.cpp ui/res/res.qrc ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_link_libraries (yourProject example Qt5::Widgets ${OpenCV_LIBS} Qt5::Sql)
### Add Library
add_library(yourProject_lib SHARED ${SRCS} ${UI_HDRS})
target_link_libraries (yourProject_lib example Qt5::Widgets ${OpenCV_LIBS})
# Majid Alaeinia,I uploaded the repository and went through the code. if you go inside the demo folder and you change the present CMakeLists.txt file with the one I provided below it should compile (It does compile on mine with the provided changes):
project(demo)
cmake_minimum_required(VERSION 2.6)
find_package(Qt5 REQUIRED COMPONENTS Widgets Core)
FIND_PACKAGE( OpenCV REQUIRED )
include_directories(${QT_INCLUDES} ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_SOURCE_DIR}/lib ${CMAKE_CURRENT_SOURCE_DIR})
set(demo_SRCS main.cpp projfile.cpp deformwin.cpp myimage.cpp singlephotoview.cpp pointspaint.cpp)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
#qt5_automoc(${demo_SRCS})
QT5_WRAP_CPP(QOBJ_CPP ${demo_SRCS})
qt5_wrap_ui(helloworld_FORMS_HEADERS deformwin.ui)
add_executable(demo ${demo_SRCS} ${helloworld_FORMS_HEADERS})
target_link_libraries(demo ${QT_QTCORE_LIBRARY} ${QT_QTGUI_LIBRARY} imgwarp-lib opencv_core opencv_imgproc opencv_imgcodecs)
The code in the repository is an old code and still carries Qt4 as main wrappers. I think you probably have Qt5 installed on your computer and in fact the code I provided it will work for Qt5. Use it as a guideline for the other CMakeLists.txt file present inside src folder and change accordingly.
CMake will compile but because it was used Qt4 you need to figure out the most important modules to add, for example the new standard for including QtGui/QApplication is usually substituted by QtWidgets/QApplication
I also wanted to leave my previous answer in case you need a starting point or a initial template. I hope this clarifies a bit more and can get you move forward for your project.

proper way to write a CMakeLists referencing HDF5 in Windows

I've already downloaded an built HDF5 under Windows using CMake, I also generated an installer to install it under Program Files.
Below the CMakeLists.txt I wrote to be able to use HDF5 in a program I already wrote under Linux :
cmake_minimum_required(VERSION 2.8)
project(Hdf5DataFeed)
add_definitions(-DWINDOWS)
find_package(HDF5)
FIND_LIBRARY(HDF5_HL_LIBRARY hdf5_hl)
FIND_LIBRARY(ZLIB zlib)
find_library(ZMQ_LIB zmq)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
include_directories(${ZMQ_LIB_INCLUDE})
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${ZLIB} "C:/Program Files/HDF_Group/HDF5/1.10.1/lib/libszip.lib" ${VTK_LIBRARIES} ${ZMQ_LIB} ${HDF5_LIBRARIES} ${HDF5_HL_LIBRARY} Qt5::Core Qt5::Gui Qt5::Widgets)
target_include_directories(${PROJECT_NAME} PRIVATE ${HDF5_INCLUDE_DIRS})
As you can see above, to link HDF5 under Visual Studio, I needed Zlib, Szip (that I had to enter an absolute path to it, I don't like that), HDF5 library and the HDF5 High Level (Lite) library.
These libraries are located under C:\Program Files\HDF_Group\HDF5\1.10.1\lib :
libhdf5.lib <============
libhdf5.settings
libhdf5_cpp.lib
libhdf5_hl.lib <=====
libhdf5_hl_cpp.lib
libhdf5_tools.lib
libszip.lib <=== ????
libzlib.lib <====
I use CMake-Gui to inform CMake of the libraries path (except for Szip, I don't know why CMake doesn't know about it, and why I don't have the possibility to just feed CMake the library directory instead of indicating the path of few of them).
I want to use CMake-GUI to inform CMake of Szip library path, but this last doesn't create an entry of it, I only have these entries related to HDF5 :
I'm having troubles with HDF5 also under Ubuntu (see this question : hdf5.h no such file or directory under Ubuntu and CMake).
For now, it's only under CentOS 7 that I didn't encounter any issues with HDF5.
If someone can give me/us a final solution that works both on Windows and Ubuntu that would be great !
Does this solution work for you?
cmake_minimum_required(VERSION 2.8)
project(Hdf5DataFeed)
# necessary?
add_definitions(-DWINDOWS)
find_package(HDF5 REQUIRED COMPONENTS C CXX HL)
find_package(ZLIB REQUIRED)
find_package(VTK REQUIRED)
include(${VTK_USE_FILE})
find_library(ZMQ_LIB zmq)
include_directories(${ZMQ_LIB_INCLUDE} ${HDF5_INCLUDE_DIR}
${ZLIB_INCLUDE_DIRS})
aux_source_directory(. SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
target_link_libraries(${PROJECT_NAME} ${HDF5_LIBRARIES}
${HDF5_HL_LIBRARIES} ${ZLIB_LIBRARIES} ${VTK_LIBRARIES}
Qt5::Core Qt5::Gui Qt5::Widgets ${ZMQ_LIB})
Recommendation 1: Surely, there is a way to find Qt5 via find_package, i.e.,
find_package(Qt5 COMPONENTS Core Widgets REQUIRED)
and then add the correct variables to include_directories and target_link_libraries. Not sure whether such a possibility exists for the zmq library, though.
Recommendation 2: I think the call the aux_source_directory should be avoided in most cases. Create an explicit list of your source files instead.
For libszip, adding a find_library is better than putting an absolute link to it. For ZLib, it is preferable to use find_library as find_package will require you to feed CMake with an include directory which is not required for HDF5. Finally, it is preferable to use find_package for ZMQ, otherwise, we need to add manually the entry "ZMQ_LIB_INCLUDE".

Using Qt inside Clion

I'm trying to use Clion IDE to compile a simple program using Qt library, but I can't figure out how to configure CMakeLists.txt file. (I'm not familiar with cmake and toolchain)
this is my current CMakeLists.txt file:
cmake_minimum_required(VERSION 3.2)
project(MyTest)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(MyTest ${SOURCE_FILES})
# Define sources and executable
set(EXECUTABLE_NAME "MySFML")
add_executable(${EXECUTABLE_NAME} main.cpp)
# Detect and add SFML
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()
It's configured to use SFML library with a "FindSFML.cmake" file and it works fine. (I have copied these files from some tutorial) now I want some help regarding proper CMakeLists.txt configuration to compile programs that are using Qt library (it's more helpful if the files and explanations are provided).
P.S: my current OS is manjaro 0.8.13 and all I could find was explaining configurations in windows environment so I was unable to implement those tutorials.
In addition to #tomvodi's answer, you can use a simpler syntax :
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui).
Then, you don't call qt5_use_modules but instead use the standard command to link :
target_link_libraries(MyTest Qt5::Core Qt5::Widgets Qt5::Gui)
Your CMake project file is missing the Qt packages. You have to add:
find_package( Qt5Core REQUIRED )
find_package( Qt5Widgets REQUIRED )
find_package( Qt5Gui REQUIRED )
and then
qt5_use_modules( MyTest Core Widgets Gui )

Cannot find QCursor in Qt CMake Project

I'm trying to build an application on Linux with Qt where I can set the Cursor position. The project is managed with CMake.
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8.4)
project(Project)
add_definitions(-std=gnu++14 -std=c++14 -Wall -Wextra)
set(CMAKE_PREFIX_PATH "/home/elmewo/Libraries/Qt/5.3/gcc_64")
set(CMAKE_AUTOMOC ON)
find_package(Qt5Core REQUIRED)
find_package(Qt5Quick REQUIRED)
find_package(Qt5Gui REQUIRED)
include_directories(${CMAKE_SOURCE_DIR}/src)
set(SOURCE_FILES src/main.cpp)
add_executable(Project ${SOURCE_FILES})
qt5_use_modules(Project Core Quick Gui)
The packages are found by CMake. But when I try to
#include <QCursor>
my compiler says
fatal error: QCursor: file or directory not found
I was able to compile another basic QGuiApplication on the same machine.
The QCursor file is situated in ${CMAKE_PREFIX_PATH}/include/QtGui.
Am I missing something?
It seems that you are depending on 2.8.4, so at least you either need to change your build rules based on this or you will need to upgrade the dependency to at least cmake version 2.8.9:
Using Qt 5 with CMake older than 2.8.9
If using CMake older than 2.8.9, the qt5_use_modules macro is not available. Attempting to use it will result in an error.
To use Qt 5 with versions of CMake older than 2.8.9, it is necessary to use the target_link_libraries, include_directories, and add_definitions commands, and to manually specify moc requirements with either qt5_generate_moc or qt5_wrap_cpp:
Therefore, please add these if you stick with old cmake:
# Add the include directories for the Qt 5 Widgets module to
# the compile lines.
include_directories(${Qt5Core_INCLUDE_DIRS} ${Qt5Gui_INCLUDE_DIRS} ${Qt5Quick_INCLUDE_DIRS})
#Link the helloworld executable to the Qt 5 widgets library.
target_link_libraries(helloworld Qt5::Core Qt5::Gui Qt5::Quick)