使用 MinGW-w64 构建 mupdf 时出现链接错误

Linking error when building mupdf with MinGW-w64

我尝试使用 MinGW-w64 构建 mupdf 库。编译和ar操作完成,但是在链接exec文件的时候,报这样的错误

build/mingw64-cross/release/libmupdf-third.a(jbig2_symbol_dict.o):jbig2_symbol_dict.c:(.text$_Exit+0x0): multiple definition of `_Exit'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$_Exit+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_symbol_dict.o):jbig2_symbol_dict.c:(.text$llabs+0x0): multiple definition of `llabs'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$llabs+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_symbol_dict.o):jbig2_symbol_dict.c:(.text$atoll+0x0): multiple definition of `atoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$atoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_symbol_dict.o):jbig2_symbol_dict.c:(.text$lltoa+0x0): multiple definition of `lltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_symbol_dict.o):jbig2_symbol_dict.c:(.text$ulltoa+0x0): multiple definition of `ulltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_symbol_dict.o):jbig2_symbol_dict.c:(.text$wtoll+0x0): multiple definition of `wtoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$wtoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_symbol_dict.o):jbig2_symbol_dict.c:(.text$lltow+0x0): multiple definition of `lltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_symbol_dict.o):jbig2_symbol_dict.c:(.text$ulltow+0x0): multiple definition of `ulltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_text.o):jbig2_text.c:(.text$_Exit+0x0): multiple definition of `_Exit'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$_Exit+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_text.o):jbig2_text.c:(.text$llabs+0x0): multiple definition of `llabs'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$llabs+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_text.o):jbig2_text.c:(.text$atoll+0x0): multiple definition of `atoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$atoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_text.o):jbig2_text.c:(.text$lltoa+0x0): multiple definition of `lltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_text.o):jbig2_text.c:(.text$ulltoa+0x0): multiple definition of `ulltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_text.o):jbig2_text.c:(.text$wtoll+0x0): multiple definition of `wtoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$wtoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_text.o):jbig2_text.c:(.text$lltow+0x0): multiple definition of `lltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_text.o):jbig2_text.c:(.text$ulltow+0x0): multiple definition of `ulltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_iaid.o):jbig2_arith_iaid.c:(.text$_Exit+0x0): multiple definition of `_Exit'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$_Exit+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_iaid.o):jbig2_arith_iaid.c:(.text$llabs+0x0): multiple definition of `llabs'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$llabs+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_iaid.o):jbig2_arith_iaid.c:(.text$atoll+0x0): multiple definition of `atoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$atoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_iaid.o):jbig2_arith_iaid.c:(.text$lltoa+0x0): multiple definition of `lltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_iaid.o):jbig2_arith_iaid.c:(.text$ulltoa+0x0): multiple definition of `ulltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_iaid.o):jbig2_arith_iaid.c:(.text$wtoll+0x0): multiple definition of `wtoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$wtoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_iaid.o):jbig2_arith_iaid.c:(.text$lltow+0x0): multiple definition of `lltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_iaid.o):jbig2_arith_iaid.c:(.text$ulltow+0x0): multiple definition of `ulltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_int.o):jbig2_arith_int.c:(.text$_Exit+0x0): multiple definition of `_Exit'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$_Exit+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_int.o):jbig2_arith_int.c:(.text$llabs+0x0): multiple definition of `llabs'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$llabs+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_int.o):jbig2_arith_int.c:(.text$atoll+0x0): multiple definition of `atoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$atoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_int.o):jbig2_arith_int.c:(.text$lltoa+0x0): multiple definition of `lltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_int.o):jbig2_arith_int.c:(.text$ulltoa+0x0): multiple definition of `ulltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_int.o):jbig2_arith_int.c:(.text$wtoll+0x0): multiple definition of `wtoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$wtoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_int.o):jbig2_arith_int.c:(.text$lltow+0x0): multiple definition of `lltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_arith_int.o):jbig2_arith_int.c:(.text$ulltow+0x0): multiple definition of `ulltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_generic.o):jbig2_generic.c:(.text$_Exit+0x0): multiple definition of `_Exit'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$_Exit+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_generic.o):jbig2_generic.c:(.text$llabs+0x0): multiple definition of `llabs'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$llabs+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_generic.o):jbig2_generic.c:(.text$atoll+0x0): multiple definition of `atoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$atoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_generic.o):jbig2_generic.c:(.text$lltoa+0x0): multiple definition of `lltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_generic.o):jbig2_generic.c:(.text$ulltoa+0x0): multiple definition of `ulltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_generic.o):jbig2_generic.c:(.text$wtoll+0x0): multiple definition of `wtoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$wtoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_generic.o):jbig2_generic.c:(.text$lltow+0x0): multiple definition of `lltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_generic.o):jbig2_generic.c:(.text$ulltow+0x0): multiple definition of `ulltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_halftone.o):jbig2_halftone.c:(.text$_Exit+0x0): multiple definition of `_Exit'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$_Exit+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_halftone.o):jbig2_halftone.c:(.text$llabs+0x0): multiple definition of `llabs'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$llabs+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_halftone.o):jbig2_halftone.c:(.text$atoll+0x0): multiple definition of `atoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$atoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_halftone.o):jbig2_halftone.c:(.text$lltoa+0x0): multiple definition of `lltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_halftone.o):jbig2_halftone.c:(.text$ulltoa+0x0): multiple definition of `ulltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_halftone.o):jbig2_halftone.c:(.text$wtoll+0x0): multiple definition of `wtoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$wtoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_halftone.o):jbig2_halftone.c:(.text$lltow+0x0): multiple definition of `lltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_halftone.o):jbig2_halftone.c:(.text$ulltow+0x0): multiple definition of `ulltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_mmr.o):jbig2_mmr.c:(.text$llabs+0x0): multiple definition of `llabs'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$llabs+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_mmr.o):jbig2_mmr.c:(.text$atoll+0x0): multiple definition of `atoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$atoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_mmr.o):jbig2_mmr.c:(.text$lltoa+0x0): multiple definition of `lltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_mmr.o):jbig2_mmr.c:(.text$ulltoa+0x0): multiple definition of `ulltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_mmr.o):jbig2_mmr.c:(.text$wtoll+0x0): multiple definition of `wtoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$wtoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_mmr.o):jbig2_mmr.c:(.text$lltow+0x0): multiple definition of `lltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_mmr.o):jbig2_mmr.c:(.text$ulltow+0x0): multiple definition of `ulltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_refinement.o):jbig2_refinement.c:(.text$_Exit+0x0): multiple definition of `_Exit'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$_Exit+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_refinement.o):jbig2_refinement.c:(.text$llabs+0x0): multiple definition of `llabs'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$llabs+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_refinement.o):jbig2_refinement.c:(.text$atoll+0x0): multiple definition of `atoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$atoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_refinement.o):jbig2_refinement.c:(.text$lltoa+0x0): multiple definition of `lltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_refinement.o):jbig2_refinement.c:(.text$ulltoa+0x0): multiple definition of `ulltoa'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltoa+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_refinement.o):jbig2_refinement.c:(.text$wtoll+0x0): multiple definition of `wtoll'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$wtoll+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_refinement.o):jbig2_refinement.c:(.text$lltow+0x0): multiple definition of `lltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$lltow+0x0): first defined here
build/mingw64-cross/release/libmupdf-third.a(jbig2_refinement.o):jbig2_refinement.c:(.text$ulltow+0x0): multiple definition of `ulltow'
build/mingw64-cross/release/libmupdf-third.a(jbig2_segment.o):jbig2_segment.c:(.text$ulltow+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
mingw32-make: *** [Makefile:247: build/mingw64-cross/release/mutool] Error 1

这些错误都是关于 jbig2dec 库的。似乎那些程序多次定义了类似 _Exit 的符号,但在源文件中,我不喜欢使用此类符号。我用nm命令分析了jbig2_segment.o文件,然后我发现

...
0000000000000000 t .text
0000000000000000 t .text$_Exit
0000000000000000 t .text$atoll
0000000000000000 t .text$jbig2_find_segment
0000000000000000 t .text$jbig2_free_segment
0000000000000000 t .text$jbig2_get_region_segment_info
0000000000000000 t .text$jbig2_parse_segment
0000000000000000 t .text$jbig2_parse_segment_header
0000000000000000 t .text$llabs
0000000000000000 t .text$lltoa
0000000000000000 t .text$lltow
0000000000000000 t .text$ulltoa
0000000000000000 t .text$ulltow
0000000000000000 t .text$wtoll
...

说明确实定义了_Exit、atoll等符号,但程序源文件中并没有这些符号。除jbig2_xxx.o外,其他.o文件无此符号。

然后,我用linux构建了同样的项目,也通过了。那么是什么导致了这个问题呢?我该如何解决?

解决方法是使用链接器标志 -Wl,--allow-multiple-definition,例如 make LDFLAGS="-Wl,--allow-multiple-definition".

我个人使用 MinGW-w64 构建 MuPDF,没有提供任何第三方代码。相反,我已经在我的系统上编译了所有依赖库,并使用以下命令构建了 MuPDF:

make NOX11=yes CC=gcc USE_SYSTEM_LIBS=yes SYS_FREETYPE_LIBS=-lfreetype CFLAGS="$(pkg-config --cflags libopenjp2) -I$(pwd)/include -I$(pwd)/fitz -I$(pwd)/pdf" SYS_GUMBO_LIBS="-lgumbo" LDFLAGS="-Wl,--as-needed -lfreetype -ljbig2dec -ljpeg -lopenjp2 -lz"