Windows 下编译 GNU barcode 库

发布时间 2023-03-27 15:35:33作者: 一杯清酒邀明月

gnu barcode 是一个用来生成条形码的库。主页在:
https://www.gnu.org/software/barcode/

对这个库就不多介绍了。

因为装了 MSYS2, 本来觉得编译这个库很简单,configure, make, make install 三步就够了。结果 第二步 mingw32-make 时就出了错,提示:

make all-recursive
process_begin: CreateProcess(NULL, make all-recursive, …) failed.
make (e=2): 系统找不到指定的文件。
mingw32-make: *** [Makefile:1271: all] Error 2

没找到解决办法。 只能想别的办法编译了。既然 MSYS2 编译出了点问题,那就用 VS 吧。

仔细翻了翻源代码,发现只要编译几个 C 文件就行:
codabar.c 、code11.c 、 code128.c 、code39.c 、code93.c 、 ean.c、i25.c 、 library.c 、 msi.c 、 plessey.c 、pcl.c 、ps.c、svg.c。

cmd.c main.c 和 sample.c 这三个不用管。

为了编译成 Dll,还需要个 barcode.def 文件。手写了一份:

 1 ; gnu barcode library
 2 EXPORTS
 3 ; basic functions
 4     Barcode_Create
 5     Barcode_Encode
 6     Barcode_Position
 7     Barcode_Print
 8     Barcode_Encode_and_Print
 9     Barcode_Version
10 
11 ; advanced functions

至此,准备工作就差不多了。我没用 VS 的IDE ,用了 qtcreator。 这个 IDE 比较轻量,还可以手写 .pro 文件。

下面是我们写的 barcode.pro 文件:

 1 #-------------------------------------------------
 2 #
 3 # Project created by QtCreator 2020-05-22T21:57:07
 4 #
 5 #-------------------------------------------------
 6 
 7 VERSION = 0.99
 8 QT       -= core gui
 9 
10 CONFIG += dll
11 TEMPLATE = lib
12 
13 #DEFINES += GNUBARCODE_LIBRARY
14 # The following define makes your compiler emit warnings if you use
15 # any feature of Qt which has been marked as deprecated (the exact warnings
16 # depend on your compiler). Please consult the documentation of the
17 # deprecated API in order to know how to port your code away from it.
18 DEFINES += QT_DEPRECATED_WARNINGS
19 
20 # You can also make your code fail to compile if you use deprecated APIs.
21 # In order to do so, uncomment the following line.
22 # You can also select to disable deprecated APIs only up to a certain version of Qt.
23 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
24 
25 SOURCES += \
26         codabar.c \
27         code11.c \
28         code128.c \
29         code39.c \
30         code93.c \
31         ean.c \
32         i25.c \
33         library.c \
34         msi.c \
35         plessey.c \
36         pcl.c \    
37         ps.c \
38         svg.c
39 
40 HEADERS += \
41     barcode.h \
42     config.h
43 
44 unix {
45     target.path = /usr/lib
46     INSTALLS += target
47 }
48        
49 win32{
50 DEF_FILE += barcode.def
51         contains(QT_ARCH, i386) {
52         message("GNU barcode lib 0.99 32-bit")
53         DLLDESTDIR = ./x86/bin
54         #DESTDIR = ./x86/lib
55 CONFIG(release, debug|release): TARGET = barcode-0.99-x86-
56 CONFIG(debug, debug|release): TARGET = barcoded-0.99-x86-
57        
58     } else {
59         message("GNU barcode lib 0.99 64-bit")
60         DLLDESTDIR = ./x64/bin
61         #DESTDIR = ./x64/lib
62 CONFIG(release, debug|release): TARGET = barcode-0.99-x64-
63 CONFIG(debug, debug|release): TARGET = barcoded-0.99-x64-
64     }
65 }

编译时还会提示一些错误,如下:

错误 1:
barcode.h:29: error: C1083: 无法打开包括文件: “gettext.h”: No such file or directory

解决办法:去掉对 gettext.h 的包含。

/// #include // 这行注释掉

/// #define _(X) gettext (X) //改为:#define _(X) (X)

错误 2:
library.c:29: error: C1083: 无法打开包括文件: “unistd.h”: No such file or directory

解决办法:将 config.h 文件中 #define HAVE_UNISTD_H 1
改为:

#undef HAVE_UNISTD_H

之后就可以正常的生成 dll 和 lib 文件了。