window7 x64 vs2015 如何编译 libqr 二维码生成库?

1.下载libqr库
下载地址:https://github.com/rsky/qrcode
注:因 libqr 依赖 zlib 库,所以首先编译 zlib库
zlib 库编译指南:http://www.cnblogs.com/cheungxiongwei/p/7493286.html
2.解压下载的 libqr 库
3.新建一个 win32 工程,选择 dll 动态库

添加libqr中的12个文件到 vs 项目中,vs项目视图最终如下所示:
libqr
        crc.h
        libqr.vcxproj
        libqr.vcxproj.filters
        qr.c
        qr.h
        qrcnv.c
        qrcnv.h
        qrcnv_bmp.c
        qrcnv_png.c
        qrcnv_svg.c
        qrcnv_tiff.c
        qr_dwtable.h
        qr_private.h
        qr_util.h

4.添加 预处理器定义 QR_DLL_BUILD

添加位置:配置属性 -> C/C++ -> 预处理器

//qr.h
#if defined(WIN32) && !defined(QR_STATIC_BUILD)
#ifdef QR_DLL_BUILD
#define QR_API __declspec(dllexport)
#else
#define QR_API __declspec(dllimport)
#endif
#else
#define QR_API
#endif

5.添加 zlib 依赖

│  libqr.sln
│
├─libqr
│  │  crc.h
│  │  libqr.vcxproj
│  │  libqr.vcxproj.filters
│  │  qr.c
│  │  qr.h
│  │  qrcnv.c
│  │  qrcnv.h
│  │  qrcnv_bmp.c
│  │  qrcnv_png.c
│  │  qrcnv_svg.c
│  │  qrcnv_tiff.c
│  │  qr_dwtable.h
│  │  qr_private.h
│  │  qr_util.h
│
└─zlib  
        zconf.h
        zlib.h
        zlibwapi.dll
        zlibwapi.lib
配置属性 -> 链接器 -> 常规 -> 附加依赖库 ->  ./libqr.sln/zlib 目录,具体路径自己调整.
配置属性 -> 链接器 -> 输入 -> 附加依赖项 ->  添加 `zlibwapi.lib`

点击编译生成报错:
错误	C1083	无法打开包括文件: “zlib.h”: No such file or directory

找到错误的地方 修改  #include <zlib.h> 为 #include "zlib.h"
把 zconf.h 、zlib.h 头文件添加到 libqr 目录中,然后添加现有项到项目中即可.

点击编译,报错 无法链接

//zlib.h row:331
#if defined(WINDOWS) || defined(WIN32)
   /* If building or using zlib as a DLL, define ZLIB_DLL.
    * This is not mandatory, but it offers a little performance increase.
    */
#  ifdef ZLIB_DLL
#    if defined(WIN32) && (!defined(__BORLANDC__) || (__BORLANDC__ >= 0x500))
#      ifdef ZLIB_INTERNAL
#        define ZEXTERN extern __declspec(dllexport)
#      else
#        define ZEXTERN extern __declspec(dllimport)
#      endif
#    endif
#  endif  /* ZLIB_DLL */
   /* If building or using zlib with the WINAPI/WINAPIV calling convention,
    * define ZLIB_WINAPI.
    * Caution: the standard ZLIB1.DLL is NOT compiled using ZLIB_WINAPI.
    */
#  ifdef ZLIB_WINAPI
#    ifdef FAR
#      undef FAR
#    endif
#    include <windows.h>
     /* No need for _export, use ZLIB.DEF instead. */
     /* For complete Windows compatibility, use WINAPI, not __stdcall. */
#    define ZEXPORT WINAPI
#    ifdef WIN32
#      define ZEXPORTVA WINAPIV
#    else
#      define ZEXPORTVA FAR CDECL
#    endif
#  endif
#endif

根据英文提示,添加 预定义宏 ZLIB_WINAPI 即可
#define ZLIB_WINAPI 
posted @ 2017-09-08 10:50  學海無涯  阅读(962)  评论(0编辑  收藏  举报