Windows 下编译 GNU barcode 库

Windows 下编译 GNU barcode 库

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 文件。手写了一份:

; gnu barcode library
EXPORTS
; basic functions
    Barcode_Create
    Barcode_Encode
    Barcode_Position
    Barcode_Print
    Barcode_Encode_and_Print
    Barcode_Version

; advanced functions

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

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

#-------------------------------------------------
#
# Project created by QtCreator 2020-05-22T21:57:07
#
#-------------------------------------------------

VERSION = 0.99
QT       -= core gui

CONFIG += dll
TEMPLATE = lib

#DEFINES += GNUBARCODE_LIBRARY
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
        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

HEADERS += \
    barcode.h \
    config.h

unix {
    target.path = /usr/lib
    INSTALLS += target
}
        
win32{
DEF_FILE += barcode.def
        contains(QT_ARCH, i386) {
        message("GNU barcode lib 0.99 32-bit")
        DLLDESTDIR = ./x86/bin
        #DESTDIR = ./x86/lib
CONFIG(release, debug|release): TARGET = barcode-0.99-x86-
CONFIG(debug, debug|release): TARGET = barcoded-0.99-x86-
		
    } else {
        message("GNU barcode lib 0.99 64-bit")
        DLLDESTDIR = ./x64/bin
        #DESTDIR = ./x64/lib
CONFIG(release, debug|release): TARGET = barcode-0.99-x64-
CONFIG(debug, debug|release): TARGET = barcoded-0.99-x64-
    }
}

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

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

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

/// #include <gettext.h> // 这行注释掉

/// #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 文件了。

猜你喜欢

转载自blog.csdn.net/liyuanbhu/article/details/106300298