I am working on a project that requires me to use CImg which in turn needs libpng. I am using CLion and cmake with MinGW-W64 GCC-8.1.0. I have been following this answer - https://stackoverflow.com/a/41956016/11382155 to get it set up.
This is how my CMakeLists.txt looks
cmake_minimum_required(VERSION 3.15)
project(CImgProject)
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
add_executable(CImgProject ${SOURCE_FILES})
# You can alter these according to your needs, e.g if you don't need to display images - set(YOU_NEED_X11 0)
set(YOU_NEED_X11 1)
set(YOU_NEED_PNG 1)
if(${YOU_NEED_X11} EQUAL 1)
message(STATUS "Looking for X11...")
find_package(X11 REQUIRED)
include_directories(${X11_INCLUDE_DIR})
target_link_libraries(CImgProject ${X11_LIBRARIES})
else()
target_compile_definitions(CImgProject PRIVATE cimg_display=0)
endif()
if(${YOU_NEED_PNG} EQUAL 1)
message(STATUS "Looking for libpng...")
set(ZLIB_ROOT "CImg283/zlib-1.2.11")
set(ZLIB_LIBRARY "CImg283/zlib-1.2.11")
set(PNG_ROOT "CImg283/lpng1637")
set(PNG_LIBRARY "CImg283/lpng1637")
find_package(PNG REQUIRED)
include_directories(${PNG_INCLUDE_DIR})
target_link_libraries (CImgProject ${PNG_LIBRARY})
target_compile_definitions(CImgProject PRIVATE cimg_use_png=1)
endif()
This is my main.cpp
#include "CImg283/CImg.h"
using namespace cimg_library;
int main() {
CImg<unsigned char> img(640,400,1,3); // Define a 640x400 color image with 8 bits per color component.
img.fill(0); // Set pixel values to 0 (color : black)
unsigned char purple[] = { 255,0,255 }; // Define a purple color
img.draw_text(100,100,"Hello World",purple); // Draw a purple "Hello world" at coordinates (100,100).
img.display("Window Title"); // Display the image in a display window.
img.save_png("test.png"); // Save as PNG to prove we linked correctly
return 0;
}
The problem I have is that when I hit build, I get the following error
====================[ Build | CImgProject | Debug ]=============================
"C:\Program Files\JetBrains\CLion 2019.1\bin\cmake\win\bin\cmake.exe" --build C:\Users\bhave\OneDrive\Desktop\untitled\cmake-build-debug --target CImgProject -- -j 6
Scanning dependencies of target CImgProject
[ 50%] Building CXX object CMakeFiles/CImgProject.dir/main.cpp.obj
In file included from C:\Users\bhave\OneDrive\Desktop\untitled\main.cpp:3:
C:\Users\bhave\OneDrive\Desktop\untitled\CImg283/CImg.h:447:10: fatal error: png.h: No such file or directory
#include "png.h"
^~~~~~~
compilation terminated.
mingw32-make.exe[3]: *** [CMakeFiles\CImgProject.dir\build.make:63: CMakeFiles/CImgProject.dir/main.cpp.obj] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:75: CMakeFiles/CImgProject.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:82: CMakeFiles/CImgProject.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: CImgProject] Error 2
I don't know why this error is popping up when cmake is able to find libpng and I checked that png.h definitely exists.
I resolved the problem by getting rid of
set(ZLIB_ROOT "CImg283/zlib-1.2.11")
set(ZLIB_LIBRARY "CImg283/zlib-1.2.11")
set(PNG_ROOT "CImg283/lpng1637")
set(PNG_LIBRARY "CImg283/lpng1637")
and just grabbing the mingw-64 library distribution from https://packages.msys2.org/package/mingw-w64-x86_64-libpng and putting the files in the correct location inside my mingw distribution. Be sure to put it in both mingw folders inside the 64 bit installation.