I'm writing a program in rust that makes use of the zbar library, using rust bindings for zbar from magiclen and want to extend the build script to support building from source. libzbar is normally built with autotools but I had separate issues with that, so I'm trying to use CC in a build script.
cc::Build::new()
.warnings(false)
.extra_warnings(false)
.define("HAVE_CONFIG_H", "1")
.include("./zbar")
.include("./zbar/zbar")
.include("./zbar/include")
.static_flag(true)
.file("./zbar/zbar/config.c")
//...all the other C files needed...
.file("./zbar/zbar/decoder/sq_finder.c")
.compile("zbar");
However when cross-compiling for windows, ld
breaks because of undefined references to iconv
, iconv_close
and iconv_open
:
cargo build --target=x86_64-pc-windows-gnu
#...snip...
error: could not compile `zbar-rust`.
warning: build failed, waiting for other jobs to finish...
error: linking with `/usr/bin/x86_64-w64-mingw32-gcc` failed: exit code: 1
|
= note: "/usr/bin/x86_64-w64-mingw32-gcc" #snip.. long list of options
= note: /usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/target/x86_64-pc-windows-gnu/debug/deps/libzbar_rust-770a0ffd35ecffdf.rlib(qrdectxt.o): in function `qr_code_data_list_extract_text':
/home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:81: undefined reference to `iconv_open'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:83: undefined reference to `iconv_open'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:85: undefined reference to `iconv_open'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:87: undefined reference to `iconv_open'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:261: undefined reference to `iconv'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:301: undefined reference to `iconv'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:319: undefined reference to `iconv'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:403: undefined reference to `iconv_open'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:412: undefined reference to `iconv_close'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:420: undefined reference to `iconv_close'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:483: undefined reference to `iconv_close'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:484: undefined reference to `iconv_close'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:485: undefined reference to `iconv_close'
/usr/bin/x86_64-w64-mingw32-ld: /home/mehow/Dropbox/code_backups/zbar-rust/target/x86_64-pc-windows-gnu/debug/deps/libzbar_rust-770a0ffd35ecffdf.rlib(qrdectxt.o):/home/mehow/Dropbox/code_backups/zbar-rust/./zbar/zbar/qrcode/qrdectxt.c:486: more undefined references to `iconv_close' follow
collect2: error: ld returned 1 exit status
error: aborting due to previous error
How do I include libiconv?
I've set the the x86_64-pc-windows-gnu target to use my system mingw in my cargo config:
[target.x86_64-pc-windows-gnu]
linker = "/usr/bin/x86_64-w64-mingw32-gcc"
ar = "/usr/x86_64-w64-mingw32/bin/ar"
My mingw install definitely has iconv, and it contains the symbols I need:
$ ls /usr/x86_64-w64-mingw32/lib/ | grep iconv
libiconv.a
libiconv.dll.a
$ nm /usr/x86_64-w64-mingw32/lib/libiconv.a | grep iconv
win_iconv.o:
0000000000001df0 T iconv
0000000000001da0 T iconv_close
0000000000001ae0 T iconv_open
00000000000016a0 t win_iconv
0000000000000000 t win_iconv_close
I've tried telling CC to include my mingw lib directory using .include
:
cc::Build::new()
//...
.include("/usr/x86_64-w64-mingw32/lib")
//...
And also explicitly including it via .flag
:
cc::Build::new()
//...
.flag("-I/usr/x86_64-w64-mingw32/lib")
.flag("-liconv")
//...
But I still get the same undefined iconv reference errors in both cases.
Note that the library builds fine when targeting my native target x86_64-unknown-linux-gnu