libqrencode学习笔记(一): VS2015编译qrencode-3.4.4

来源:互联网 发布:mysql group by 多列 编辑:程序博客网 时间:2024/06/02 04:06

0 前言

因为无聊的时间看了一个2分钟短视频介绍了下二维码的原理,觉得还蛮有意思的。作为一个程序员,就想写写代码搞下看看。先是百度了下C/C++实现二维码,发现已经有成熟的二维码开源库,于是下载源码,按照源码中README文件中说明的Compile & install在Cygwin中操作,发现只能生成libqrencode.a静态库,没有libqrencode.lib静态库供Windows下VS编程使用。再然后,就是再搜索了下相关资料,最后,成功编译出了供Windows下VS编程使用的libqrencode.lib静态库。

1 准备

a) 如果只是想体验按自己输入的内容(支持中文)生成对应二维码,可以直接在此qrcodegui_setup.exe 下载官方已经做好带有GUI界面的.exe文件。
用法非常的简单直接,稍演示下:
运行QRCodeGUI后:


点击Generate按钮,即可生成二维码,点击save as...可保存成图片。


b) 想动手在Windows直接用VS编译源码的,继续往下看,我的编译环境:
Cygwin + Win10 + VS2015
下载源码,官网最新版本qrencode-3.4.4:https://fukuchi.org/works/qrencode/
只所以需要用到Cygwin,是因为VS编译源码时要用到的config.h,可用Cygwin执行源码中README文件中说明的Compile & install操作命令自动生成。当然,如果不想安装Cygwin,可以直接新建一个config.h文件,然后复制下面内容:
/* config.h.  Generated from config.h.in by configure.  *//* config.h.in.  Generated from configure.ac by autoheader.  *//* Define to 1 if you have the <dlfcn.h> header file. */#define HAVE_DLFCN_H 1/* Define if you have the iconv() function and it works. *//* #undef HAVE_ICONV *//* Define to 1 if you have the <inttypes.h> header file. */#define HAVE_INTTYPES_H 1/* Define to 1 if using pthread is enabled. *///#define HAVE_LIBPTHREAD 1/* Define to 1 if you have the <memory.h> header file. */#define HAVE_MEMORY_H 1/* Define to 1 if you have the <stdint.h> header file. */#define HAVE_STDINT_H 1/* Define to 1 if you have the <stdlib.h> header file. */#define HAVE_STDLIB_H 1/* Define to 1 if you have the `strdup' function. */#define HAVE_STRDUP 1/* Define to 1 if you have the <strings.h> header file. */#define HAVE_STRINGS_H 1/* Define to 1 if you have the <string.h> header file. */#define HAVE_STRING_H 1/* Define to 1 if you have the <sys/stat.h> header file. */#define HAVE_SYS_STAT_H 1/* Define to 1 if you have the <sys/types.h> header file. */#define HAVE_SYS_TYPES_H 1/* Define to 1 if you have the <unistd.h> header file. */#define HAVE_UNISTD_H 1/* Define to the sub-directory in which libtool stores uninstalled libraries.   */#define LT_OBJDIR ".libs/"/* Major version number */#define MAJOR_VERSION 3/* Micro version number */#define MICRO_VERSION 4/* Minor version number */#define MINOR_VERSION 4/* Name of package */#define PACKAGE "qrencode"/* Define to the address where bug reports for this package should be sent. */#define PACKAGE_BUGREPORT ""/* Define to the full name of this package. */#define PACKAGE_NAME "QRencode"/* Define to the full name and version of this package. */#define PACKAGE_STRING "QRencode 3.4.4"/* Define to the one symbol short name of this package. */#define PACKAGE_TARNAME "qrencode"/* Define to the home page for this package. */#define PACKAGE_URL ""/* Define to the version of this package. */#define PACKAGE_VERSION "3.4.4"/* Define to 1 if you have the ANSI C header files. */#define STDC_HEADERS 1/* Version number of package */#define VERSION "3.4.4"/* Define to empty if `const' does not conform to ANSI C. *//* #undef const *//* Define to `__inline__' or `__inline' if that's what the C compiler   calls it, or to nothing if 'inline' is not supported under any name.  */#ifndef __cplusplus/* #undef inline */#endif/* Define to 'static' if no test programs will be compiled. */#define __STATIC static/* #undef WITH_TESTS */   

2 编译

1)解压qrencode-3.4.4源码后,只保留.h和.c文件,如下图只保留蓝框中的文件,红框中的删除。其中,蓝框中已经把上面准备步骤中的config.h文件加进去了,同把蓝框中有被小红框标出的qrenc.c文件删除,这个文件只是一个测试文件,打开此文件可看到其中的main函数,如果保留此测试文件,编译生成.lib静态库时会发生错误。


2)在VS中创建一个 win32 项目,选择生成静态库,不使用预编译头。将 步骤1)中操作保留qrencode 的源文件(.c 和 .h)全部拷到项目目录中。然后,选中项目,点击右键,属性 --> 配置属性 --> C/C++ --> 预处理器,在“预处理器定义”中添加HAVE_CONFIG_H

最后,编译项目生成解决方案。

出现下面“无法打开包括文件:“pthread.h””的错误,而Pthread是由POSIX提出的一套通用的线程库,在Linux平台下,它被广泛的支持,而windows平台下,却并不被支持,所以打不到这个头文件。点击错误,打到错误所在行,可以发现有:

#ifdef HAVE_LIBPTHREAD
#include <pthread.h>
#endif

所以,这是因为定义了HAVE_LIBPTHREAD变量导致的。而这个变量的定义正是在用Cygwin模拟Linux环境自动生成的config.h文件中有:#define HAVE_LIBPTHREAD 1。故因此,把这句注释掉,再编译,将成功在Debug文件夹下生成libqrencode.lib静态库。

如果不是自己用Cygwin自动生成的config.h文件,而是直接复制我上面贴出来的config.h内容,编译会直接成功,不会出错,因为我贴出来时已经把这句定义注释了。


3 使用

使用静态库比较简单,只需要拷贝生成的libqrencode.lib和源码中的qrencode.h两个文件到你的项目中。

然后配置属性 --> C/C++ --> 常规 --> 附加包含目录,加入qrencode.h所在路径

配置属性 --> 链配置属性 --> 链接器 --> 常规 --> 附加库目录,加入libqrencode.lib所在路径

配置属性 --> 链配置属性 --> 链接器 --> 输入 --> 附加依赖项,加入libqrencode.lib


参考文献:

1. http://blog.csdn.net/liyuanbhu/article/details/44647139


阅读全文
0 0
原创粉丝点击