1 year ago
#377012
of32 inc
Include and use libpng to C program using CMake [Windows]
Hi im trying to include libpng in my C program using CMake (p.s im new to CMake). I want to use it to generate png of the qrcode generated by libqrencode. What I have done so far:
1. Install zlib from zlib
// extract to zlib
cd zlib
cmake -DCMAKE_INSTALL_PREFIX=C:\zlib -S . -B out/build/
cmake --build out/build --target INSTALL --config Release
2. Install libpng from libpng 1637
// extract to libpng
cd libpng
cmake -S . -DCMAKE_PREFIX_PATH=C:/zlib -DCMAKE_INSTALL_PREFIX=C:/libpng -B out/build/
cmake --build out/build --target INSTALL --config Release
3. Install libqrencode from libqrencode
cd libqrencode-master
// configure
cmake -DCMAKE_INSTALL_PREFIX=C:/qrencode-fukuchi -DWITH_TOOLS=NO
-DZLIB_LIBRARY=C:/zlib/bin -DZLIB_INCLUDE_DIR=C:/zlib/include
-DPNG_LIBRARY=C:/libpng/bin -DPNG_PNG_INCLUDE_DIR=C:/libpng/include
-DICONV_LIBRARY=C:/libiconv/bin -DICONV_INCLUDE_DIR=C:/libiconv/include
-S . -B out/build
// install
cmake --build out/build --target INSTALL --config Release
Now i create my c program to call libpng and libqrencode using CMakeLists
│ CMakeLists.txt
│ main.c
│
└───out
└───build
The CMakesLists content:
cmake_minimum_required(VERSION 3.23.0)
project(QR)
add_executable(${PROJECT_NAME} main.c)
target_include_directories(${PROJECT_NAME}
PUBLIC C:/qrencode-fukuchi/include
PUBLIC C:/libpng/include)
target_link_libraries(${PROJECT_NAME}
C:/qrencode-fukuchi/lib/qrencode.lib
C:/libpng/lib/libpng16.lib)
The main.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "qrencode.h"
#include "png.h"
enum imageType {
PNG_TYPE,
PNG32_TYPE,
};
static enum imageType image_type = PNG_TYPE;
int main(){
printf("hello fukuchi\n");
char* szSourceSring = "Helloo";
QRcode* pQRC;
char* outfile = "test.png";
// Compute QRCode
pQRC = QRcode_encodeString(szSourceSring, 0, QR_ECLEVEL_H, QR_MODE_8, 1);
writePNG(pQRC, outfile, image_type);
QRcode_free(pQRC);
printf("bye fukuchi\n");
return 0;
}
Then i configure and build:
cmake -S . -B out/build
cmake --build out/build
I found no problem so far but when i call the exe file:
.\out\build\Debug\QR
It just terminates instantly with no error code. But when i commented the writePNG(pQRC, outfile, image_type)
and reconfigure and build it again now it show:
hello fukuchi
bye fukuchi
Update 1:
I updated my CMakelists.txt of the main.c like this (added zlib lib and reorder the linking libraries) and managed the executables to compile and executed properly when the writePNG
function is called
cmake_minimum_required(VERSION 3.23.0)
project(QR)
add_executable(${PROJECT_NAME} main.c)
target_include_directories(${PROJECT_NAME}
PUBLIC C:/qrencode-fukuchi/include
PUBLIC C:/libpng/include)
target_link_libraries(${PROJECT_NAME}
C:/zlib/lib/zlib.lib
C:/libpng/lib/libpng16.lib
C:/qrencode-fukuchi/lib/qrencode.lib)
#target_link_libraries(${PROJECT_NAME} C:/libpng/lib/libpng16.lib)
But now my writePNG
function causing debug assertion problem
crtlsValidHeapPointer(block)
is_block_type_valid(header->_block_use)
HEAP_CORRECTION_DETECTED
CRT detected that the application wrote to memory before start of the heap
CRT detected that the application wrote to memory after end of the heap
The assertion occurs when it hit this line:
png_write_info(png_ptr, info_ptr);
The full writePNG function can be viewed here: code
Please help me
c
windows
cmake
zlib
libpng
0 Answers
Your Answer