Windows的交叉编译zbar C库

我正在编写一个使用zbar library的rust程序,对magiclen中的zbar使用rust绑定,并希望扩展构建脚本以支持从源代码构建。 libzbar通常是用自动工具构建的,但是与此同时我有单独的问题,所以我试图在构建脚本中使用CC

    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");

但是,对于Windows进行交叉编译时,ld会由于对iconviconv_closeiconv_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

我如何包含libiconv?

我将x86_64-pc-windows-gnu目标设置为在我的货物配置中使用系统mingw:

[target.x86_64-pc-windows-gnu]
linker = "/usr/bin/x86_64-w64-mingw32-gcc"
ar = "/usr/x86_64-w64-mingw32/bin/ar"

我的mingw安装中肯定有iconv,并且其中包含我需要的符号:

$ 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

我试图告诉CC使用.include来包含我的mingw lib目录:

    cc::Build::new()
        //...
        .include("/usr/x86_64-w64-mingw32/lib")
        //...

并且还通过.flag明确包含了它:

    cc::Build::new()
        //...
        .flag("-I/usr/x86_64-w64-mingw32/lib")
        .flag("-liconv")
        //...

但是在两种情况下,我仍然遇到相同的未定义iconv参考错误。

请注意,以我的原生目标x86_64-unknown-linux-gnu

为目标库可以很好地构建
xiaoyr062285 回答:Windows的交叉编译zbar C库

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/2822072.html

大家都在问