返回

如何使用CMake[Windows]将libpng包含到C程序中

发布时间:2022-04-08 15:52:13 580
# node.js

嗨,我正在尝试使用CMake将libpng包含在我的C程序中(p.s我是CMake的新手)。我想用它来生成libqrencode生成的qrcode的png。到目前为止我所做的:

1.从zlib安装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.从libpng 1637安装libpng

// 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.从libqrencode安装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

现在我创建了我的c程序,用CMakeLists调用libpng和libqrencode

│   CMakeLists.txt
│   main.c
│
└───out
    └───build

该文件列出了以下内容:

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)

主要的

#include 
#include 
#include 
#include 

#include "qrencode.h"
#include "png.h"

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;
}

然后我配置并构建:

cmake -S . -B out/build
cmake --build out/build

到目前为止,我没有发现任何问题,但当我调用exe文件时:

.\out\build\Debug\QR

它只是立即终止,没有错误代码。但当我评论writePNG(pQRC, outfile, image_type)重新配置并重新构建,现在显示:

hello fukuchi
bye fukuchi

更新1:

我更新了我的CMakelists。这是主要的。c这样(添加了zlib库并对链接库进行了重新排序),并在writePNG函数被调用

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)

但现在我的writePNG导致调试断言问题的函数

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

断言发生在它到达这一行时:

png_write_info(png_ptr, info_ptr);

完整的write_png函数可以在这里查看:code

请帮帮我

特别声明:以上内容(图片及文字)均为互联网收集或者用户上传发布,本站仅提供信息存储服务!如有侵权或有涉及法律问题请联系我们。
举报
评论区(1)
按点赞数排序
用户头像