mingw 在 Windows 上找不到 libpng 的 png.h
mingw cannot find png.h for libpng on Windows
我正在做一个需要我使用 CImg 的项目,而 CImg 又需要 libpng。我将 CLion 和 cmake 与 MinGW-W64 GCC-8.1.0 一起使用。我一直在关注这个答案 - 来设置它。
这就是我的 CMakeLists.txt 的样子
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()
这是我的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;
}
我遇到的问题是,当我点击构建时,出现以下错误
====================[ 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
我不知道为什么当 cmake 能够找到 libpng 时弹出这个错误,我检查过 png.h 确实存在。
我通过删除
解决了这个问题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")
并从 https://packages.msys2.org/package/mingw-w64-x86_64-libpng 中获取 mingw-64 库分发并将文件放在我的 mingw 分发中的正确位置。务必将其放入 64 位安装内的两个 mingw 文件夹中。