Rust cross compile to ARM:在链接静态C库时未定义对printf的引用

wljmcqd8  于 2024-01-06  发布在  其他
关注(0)|答案(1)|浏览(161)

问题

我正在学习如何在裸金属Rust项目中进行C互操作。我有一个简单的C项目,它接受一个结构并打印它。我用arm-none-eabi-gcc编译它以生成一个libfoo. a并将其链接到我的Rust项目。
Rust项目被配置为使用.cargo.config.toml中的arm gcc和链接器。我使用bindgen生成绑定,并通过绑定调用C函数。
我在编译项目时遇到了问题。当rust试图链接静态C库时,它会抱怨

  1. error: linking with `arm-none-eabi-gcc` failed: exit status: 1
  2. |
  3. = note: LC_ALL="C" PATH="/Users/yuenton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/bin:/Users/yuenton/.pyenv/shims:/Applica
  4. tions/Simplicity Studio.app/Contents/Eclipse/developer/adapter_packs/commander/Commander.app/Contents/MacOS:/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/2.7
  5. .0/bin:/Applications/cov-analysis-macosx-2022-3-0/bin/:/Applications/SEGGER/JLink_V760h:
  6. /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin:/Users/yuenton/.toolbox/bin:/Users/yuenton/.nvm/versions/node/v14.20.0/bin:/Users/yuenton/bin:/usr/local/b
  7. in:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/yuenton/.local/share/nvim/mason/bin:/Users/yuenton/.pyenv/bin:/Applications/Simplicity Studio.app/Contents/Eclips
  8. e/developer/adapter_packs/commander/Commander.app/Contents/MacOS:/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/2.7.0/bin:/Applications/SEGGER/JLink_V760h:/usr/local/opt/gcc-arm-none-eabi-8-2019-q3-
  9. update/bin:/Users/yuenton/.toolbox/bin:/Users/yuenton/.nvm/versions/node/v14.20.0/bin:/Users/yuenton/bin:/usr/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/us
  10. r/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/
  11. var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/yuen
  12. ton/.cargo/bin:/Applications/kitty.app/Contents/MacOS:/usr/local/opt/fzf/bin" VSLANG="1033" "arm-none-eabi-gcc" "/var/folders/lm/n3lqn5fx2qsddzty2389rfgcfkmffy/
  13. T/rustc5AOqmy/symbols.o" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/app-6d1120295b5167b4.44hpxx6ly2g0n
  14. 6p4.rcgu.o" "-Wl,--as-needed" "-L" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps" "-L" "/Volumes/workplac
  15. e/playground/rust_embedded_with_c_interop/app/target/debug/deps" "-L" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/
  16. debug/build/app-fca9b4a0fb47443d/out" "-L" "/usr/local/opt/arm-none-eabi-gcc/gcc/arm-none-eabi/lib" "-L" "./c_proj/build/" "-L" "/Volumes/workplace/playground/r
  17. ust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/build/cortex-m-c1b153b4741594d3/out" "-L" "/Volumes/workplace/playground/rust_embedded_with_c_in
  18. terop/app/target/thumbv7m-none-eabi/debug/build/cortex-m-rt-feabb8431baf6f6c/out" "-L" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/th
  19. umbv7m-none-eabi/debug/build/cortex-m-semihosting-3928837b527a45a9/out" "-L" "/Users/yuenton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/thumbv7m-
  20. none-eabi/lib" "-Wl,-Bstatic" "-lfoo" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libcty-580a16e85c8469
  21. 02.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libcortex_m_rt-c96c0df2d2e53803.rlib" "/Volumes/wo
  22. rkplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libr0-c044695858752b9f.rlib" "/Volumes/workplace/playground/rust_embedd
  23. ed_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libcortex_m-4e3d1e159a5b50d2.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/ta
  24. rget/thumbv7m-none-eabi/debug/deps/libaligned-433f80187af623df.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/d
  25. ebug/deps/libas_slice-7cd7b96cd776cbe0.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libstable_dere
  26. f_trait-d6be1c622a71e64c.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libgeneric_array-2fb71faed80
  27. 7ea2c.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libgeneric_array-fe72171b4f9ffd35.rlib" "/Volum
  28. es/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libgeneric_array-b85e7f6fe2cef964.rlib" "/Volumes/workplace/playgr
  29. ound/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libtypenum-271c16d2a2f77c26.rlib" "/Volumes/workplace/playground/rust_embedded_with_c
  30. _interop/app/target/thumbv7m-none-eabi/debug/deps/libpanic_halt-4d0b3983ed02fa87.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/th
  31. umbv7m-none-eabi/debug/deps/libcortex_m_semihosting-695180d6324c8ba4.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-
  32. eabi/debug/deps/libcortex_m-09751e7d921baf68.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libembed
  33. ded_hal-2809cd32091c3ba8.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libvoid-fb1bf4e02014cb07.rli
  34. b" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libnb-56a04b5218fae863.rlib" "/Volumes/workplace/playgro
  35. und/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libnb-f9897756642f2d1b.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_inter
  36. op/app/target/thumbv7m-none-eabi/debug/deps/libvolatile_register-6c4b892b7612085e.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/t
  37. humbv7m-none-eabi/debug/deps/libvcell-16cd54fe2c2cf158.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/dep
  38. s/libbare_metal-cdf719ed4a7ace5b.rlib" "/Users/yuenton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/thumbv7m-none-eabi/lib/librustc_std_workspace_c
  39. ore-05bb5221ddab705b.rlib" "/Users/yuenton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/thumbv7m-none-eabi/lib/libcore-43d826545f9dd0d1.rlib" "/Use
  40. rs/yuenton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/thumbv7m-none-eabi/lib/libcompiler_builtins-c59fdfa9b274eac2.rlib" "-Wl,-Bdynamic" "-Wl,--e
  41. h-frame-hdr" "-Wl,-z,noexecstack" "-L" "/Users/yuenton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/thumbv7m-none-eabi/lib" "-o" "/Volumes/workplac
  42. e/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/app-6d1120295b5167b4" "-Wl,--gc-sections" "-no-pie" "-nodefaultlibs" "-Wl,-Tl
  43. ink.x" "-nostartfiles"
  44. = note: /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld: /Volumes/workplace/playground/r
  45. ust_embedded_with_c_interop/app/target/debug/deps/libfoo.a(foo.o): in function `print_hello_foo':
  46. foo.c:(.text+0x4c): undefined reference to `printf'
  47. collect2: error: ld returned 1 exit status
  48. = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  49. = note: use the `-l` flag to specify native libraries to link
  50. = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scrip
  51. ts.html#cargorustc-link-libkindname)

字符串
我尝试链接arm-none-eabi提供的libc. a以获取对printf定义的引用,但它引入了更多未定义的引用

  1. error: linking with `arm-none-eabi-gcc` failed: exit status: 1
  2. |
  3. = note: LC_ALL="C" PATH="/Users/yuenton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/x86_64-apple-darwin/bin:/Users/yuenton/.pyenv/shims:/Applica
  4. tions/Simplicity Studio.app/Contents/Eclipse/developer/adapter_packs/commander/Commander.app/Contents/MacOS:/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/2.7
  5. .0/bin:/Applications/cov-analysis-macosx-2022-3-0/bin/:/Applications/SEGGER/JLink_V760h:
  6. /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin:/Users/yuenton/.toolbox/bin:/Users/yuenton/.nvm/versions/node/v14.20.0/bin:/Users/yuenton/bin:/usr/local/b
  7. in:/opt/homebrew/bin:/opt/homebrew/sbin:/Users/yuenton/.local/share/nvim/mason/bin:/Users/yuenton/.pyenv/bin:/Applications/Simplicity Studio.app/Contents/Eclips
  8. e/developer/adapter_packs/commander/Commander.app/Contents/MacOS:/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/2.7.0/bin:lpay/Applications/SEGGER/JLink_V760h:/usr/local/opt/gcc-arm-none-eabi-8-2019-q3-
  9. update/bin:/Users/yuenton/.toolbox/bin:/Users/yuenton/.nvm/versions/node/v14.20.0/bin:/Users/yuenton/bin:/usr/local/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/us
  10. r/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/
  11. var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/yuen
  12. ton/.cargo/bin:/Applications/kitty.app/Contents/MacOS:/usr/local/opt/fzf/bin" VSLANG="1033" "arm-none-eabi-gcc" "/var/folders/lm/n3lqn5fx2qsddzty2389rfgcfkmffy/
  13. T/rustcjzQWtq/symbols.o" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/app-6d1120295b5167b4.44hpxx6ly2g0n
  14. 6p4.rcgu.o" "-Wl,--as-needed" "-L" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps" "-L" "/Volumes/workplac
  15. e/playground/rust_embedded_with_c_interop/app/target/debug/deps" "-L" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/
  16. debug/build/app-fca9b4a0fb47443d/out" "-L" "/usr/local/opt/arm-none-eabi-gcc/gcc/arm-none-eabi/lib" "-L" "./c_proj/build/" "-L" "/Volumes/workplace/playground/r
  17. ust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/build/cortex-m-c1b153b4741594d3/out" "-L" "/Volumes/workplace/playground/rust_embedded_with_c_in
  18. terop/app/target/thumbv7m-none-eabi/debug/build/cortex-m-rt-feabb8431baf6f6c/out" "-L" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/th
  19. umbv7m-none-eabi/debug/build/cortex-m-semihosting-3928837b527a45a9/out" "-L" "/Users/yuenton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/thumbv7m-
  20. none-eabi/lib" "-Wl,-Bstatic" "-lfoo" "-lc" "-lc_nano" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libc
  21. ty-580a16e85c846902.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libcortex_m_rt-c96c0df2d2e53803.r
  22. lib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libr0-c044695858752b9f.rlib" "/Volumes/workplace/playg
  23. round/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libcortex_m-4e3d1e159a5b50d2.rlib" "/Volumes/workplace/playground/rust_embedded_with
  24. _c_interop/app/target/thumbv7m-none-eabi/debug/deps/libaligned-433f80187af623df.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thu
  25. mbv7m-none-eabi/debug/deps/libas_slice-7cd7b96cd776cbe0.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/de
  26. ps/libstable_deref_trait-d6be1c622a71e64c.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libgeneric_
  27. array-2fb71faed807ea2c.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libgeneric_array-fe72171b4f9ff
  28. d35.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libgeneric_array-b85e7f6fe2cef964.rlib" "/Volumes
  29. /workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libtypenum-271c16d2a2f77c26.rlib" "/Volumes/workplace/playground/rus
  30. t_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libpanic_halt-4d0b3983ed02fa87.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_inte
  31. rop/app/target/thumbv7m-none-eabi/debug/deps/libcortex_m_semihosting-695180d6324c8ba4.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/targ
  32. et/thumbv7m-none-eabi/debug/deps/libcortex_m-09751e7d921baf68.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/de
  33. bug/deps/libembedded_hal-2809cd32091c3ba8.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libvoid-fb1
  34. bf4e02014cb07.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libnb-56a04b5218fae863.rlib" "/Volumes/
  35. workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libnb-f9897756642f2d1b.rlib" "/Volumes/workplace/playground/rust_embe
  36. dded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/libvolatile_register-6c4b892b7612085e.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_int
  37. erop/app/target/thumbv7m-none-eabi/debug/deps/libvcell-16cd54fe2c2cf158.rlib" "/Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-no
  38. ne-eabi/debug/deps/libbare_metal-cdf719ed4a7ace5b.rlib" "/Users/yuenton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/thumbv7m-none-eabi/lib/librust
  39. c_std_workspace_core-05bb5221ddab705b.rlib" "/Users/yuenton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/thumbv7m-none-eabi/lib/libcore-43d826545f9
  40. dd0d1.rlib" "/Users/yuenton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/thumbv7m-none-eabi/lib/libcompiler_builtins-c59fdfa9b274eac2.rlib" "-Wl,-B
  41. dynamic" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/Users/yuenton/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/thumbv7m-none-eabi/lib" "-o" "
  42. /Volumes/workplace/playground/rust_embedded_with_c_interop/app/target/thumbv7m-none-eabi/debug/deps/app-6d1120295b5167b4" "-Wl,--gc-sections" "-no-pie" "-nodefa
  43. ultlibs" "-Wl,-Tlink.x" "-nostartfiles"
  44. = note: /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld: /usr/local/opt/arm-none-eabi-gc
  45. c/gcc/arm-none-eabi/lib/libc.a(lib_a-sbrkr.o): in function `_sbrk_r':
  46. sbrkr.c:(.text._sbrk_r+0x18): undefined reference to `_sbrk'
  47. /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld: /usr/local/opt/arm-none-eabi-gc
  48. c/gcc/arm-none-eabi/lib/libc.a(lib_a-writer.o): in function `_write_r':
  49. writer.c:(.text._write_r+0x24): undefined reference to `_write'
  50. /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld: /usr/local/opt/arm-none-eabi-gc
  51. c/gcc/arm-none-eabi/lib/libc.a(lib_a-closer.o): in function `_close_r':
  52. closer.c:(.text._close_r+0x18): undefined reference to `_close'
  53. /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld: /usr/local/opt/arm-none-eabi-gc
  54. c/gcc/arm-none-eabi/lib/libc.a(lib_a-fstatr.o): in function `_fstat_r':
  55. fstatr.c:(.text._fstat_r+0x1c): undefined reference to `_fstat'
  56. /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld: /usr/local/opt/arm-none-eabi-gc
  57. c/gcc/arm-none-eabi/lib/libc.a(lib_a-isattyr.o): in function `_isatty_r':
  58. isattyr.c:(.text._isatty_r+0x18): undefined reference to `_isatty'
  59. /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld: /usr/local/opt/arm-none-eabi-gc
  60. c/gcc/arm-none-eabi/lib/libc.a(lib_a-lseekr.o): in function `_lseek_r':
  61. lseekr.c:(.text._lseek_r+0x24): undefined reference to `_lseek'
  62. /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld: /usr/local/opt/arm-none-eabi-gc
  63. c/gcc/arm-none-eabi/lib/libc.a(lib_a-readr.o): in function `_read_r':
  64. readr.c:(.text._read_r+0x24): undefined reference to `_read'
  65. /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld: /usr/local/opt/arm-none-eabi-gc
  66. c/gcc/arm-none-eabi/lib/libc.a(lib_a-abort.o): in function `abort':
  67. abort.c:(.text.abort+0x10): undefined reference to `_exit'
  68. /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld: /usr/local/opt/arm-none-eabi-gc
  69. c/gcc/arm-none-eabi/lib/libc.a(lib_a-signalr.o): in function `_kill_r':
  70. signalr.c:(.text._kill_r+0x1c): undefined reference to `_kill'
  71. /usr/local/opt/gcc-arm-none-eabi-8-2019-q3-update/bin/../lib/gcc/arm-none-eabi/8.3.1/../../../../arm-none-eabi/bin/ld: /usr/local/opt/arm-none-eabi-gc
  72. c/gcc/arm-none-eabi/lib/libc.a(lib_a-signalr.o): in function `_getpid_r':
  73. signalr.c:(.text._getpid_r+0x4): undefined reference to `_getpid'
  74. collect2: error: ld returned 1 exit status
  75. = note: some `extern` functions couldn't be found; some native libraries may need to be installed or have their path specified
  76. = note: use the `-l` flag to specify native libraries to link
  77. = note: use the `cargo:rustc-link-lib` directive to specify the native libraries to link with Cargo (see https://doc.rust-lang.org/cargo/reference/build-scrip
  78. ts.html#cargorustc-link-libkindname)


我希望glib函数定义是由编译器自动链接的,我不应该提供库。我在设置C静态库时错过了一个步骤吗?
我很困惑,当与Rust for C库交叉编译时,本机库应该如何链接。非常感谢任何帮助!

项目设置

这是我的C文件

  1. // foo.h
  2. typedef struct {
  3. int x;
  4. float y;
  5. char str[20];
  6. } foo_s;
  7. void print_hello_foo(const foo_s *foo);
  8. // foo.c
  9. #include "include/foo.h"
  10. #include <stdio.h>
  11. void print_hello_foo(const foo_s *foo) {
  12. printf("foo says hello, x: %i, y: %f, str: %s\n", foo->x, foo->y, foo->str);
  13. }


我有一个编译它的cmake项目

  1. // CMakeLists.txt
  2. cmake_minimum_required(VERSION 3.23)
  3. set(CMAKE_C_COMPILER_FORCED TRUE)
  4. set(CMAKE_CXX_COMPILER_FORCED TRUE)
  5. project(foo)
  6. add_library(foo foo.c)
  7. target_include_directories(foo PUBLIC include)
  8. message(styuen "$(CMAKE_INSTALL_INCLUDEDIR)")
  9. install(TARGETS foo PUBLIC_HEADER DESTINATION $(CMAKE_INSTALL_INCLUDEDIR)/foo)
  10. // toolchain.cmake
  11. set(CMAKE_SYSTEM_NAME Generic CACHE STRING "The operating system to target. Generic for cross-platform" FORCE)
  12. set(CMAKE_SYSTEM_PROCESSOR arm CACHE STRING "The target CPU architecture" FORCE)
  13. set(CMAKE_CROSSCOMPILING 1 CACHE STRING "" FORCE)
  14. set(TOOLCHAIN_PREFIX arm-none-eabi-)
  15. find_program(BINUTILS_PATH ${TOOLCHAIN_PREFIX}gcc NO_CACHE)
  16. if (NOT BINUTILS_PATH)
  17. message(FATAL_ERROR "ARM GCC toolchain not found")
  18. endif ()
  19. get_filename_component(ARM_TOOLCHAIN_DIR ${BINUTILS_PATH} DIRECTORY)
  20. # Without that flag CMake is not able to pass test compilation check
  21. if (${CMAKE_VERSION} VERSION_EQUAL "3.6.0" OR ${CMAKE_VERSION} VERSION_GREATER "3.6")
  22. set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
  23. else ()
  24. set(CMAKE_EXE_LINKER_FLAGS_INIT "--specs=nosys.specs")
  25. endif ()
  26. set(CMAKE_C_COMPILER ${TOOLCHAIN_PREFIX}gcc CACHE STRING "" FORCE)
  27. set(CMAKE_ASM_COMPILER ${CMAKE_C_COMPILER} CACHE STRING "" FORCE)
  28. set(CMAKE_CXX_COMPILER ${TOOLCHAIN_PREFIX}g++ CACHE STRING "" FORCE)
  29. set(CMAKE_AR ${TOOLCHAIN_PREFIX}gcc-ar CACHE STRING "" FORCE)
  30. set(CMAKE_RANLIB ${TOOLCHAIN_PREFIX}gcc-ranlib CACHE STRING "" FORCE)
  31. set(CMAKE_LINKER ${TOOLCHAIN_PREFIX}/arm-none-eabi-ld CACHE STRING "" FORCE)
  32. execute_process(COMMAND ${CMAKE_C_COMPILER} -print-sysroot
  33. OUTPUT_VARIABLE ARM_GCC_SYSROOT OUTPUT_STRIP_TRAILING_WHITESPACE)


在我的rust项目中,我使用了以下构建配置

  1. # .cargo/config.toml
  2. [target.thumbv7m-none-eabi]
  3. # uncomment this to make `cargo run` execute programs on QEMU
  4. runner = "qemu-system-arm -cpu cortex-m3 -machine lm3s6965evb -nographic -semihosting-config enable=on,target=native -kernel"
  5. [target.'cfg(all(target_arch = "arm", target_os = "none"))']
  6. # uncomment ONE of these three option to make `cargo run` start a GDB session
  7. # which option to pick depends on your system
  8. runner = "arm-none-eabi-gdb -q -x openocd.gdb"
  9. # runner = "gdb-multiarch -q -x openocd.gdb"
  10. # runner = "gdb -q -x openocd.gdb"
  11. rustflags = [
  12. # Previously, the linker arguments --nmagic and -Tlink.x were set here.
  13. # They are now set by build.rs instead. The linker argument can still
  14. # only be set here, if a custom linker is needed.
  15. # By default, the LLD linker is used, which is shipped with the Rust
  16. # toolchain. If you run into problems with LLD, you can switch to the
  17. # GNU linker by uncommenting this line:
  18. "-C", "linker=arm-none-eabi-ld",
  19. # If you need to link to pre-compiled C libraries provided by a C toolchain
  20. # use GCC as the linker by uncommenting the three lines below:
  21. "-C", "linker=arm-none-eabi-gcc",
  22. "-C", "link-arg=-Wl,-Tlink.x",
  23. "-C", "link-arg=-nostartfiles",
  24. ]
  25. [build]
  26. # Pick ONE of these default compilation targets
  27. # target = "thumbv6m-none-eabi" # Cortex-M0 and Cortex-M0+
  28. target = "thumbv7m-none-eabi" # Cortex-M3
  29. # target = "thumbv7em-none-eabi" # Cortex-M4 and Cortex-M7 (no FPU)
  30. # target = "thumbv7em-none-eabihf" # Cortex-M4F and Cortex-M7F (with FPU)
  31. # target = "thumbv8m.base-none-eabi" # Cortex-M23
  32. # target = "thumbv8m.main-none-eabi" # Cortex-M33 (no FPU)
  33. # target = "thumbv8m.main-none-eabihf" # Cortex-M33 (with FPU)
  34. // build.rs
  35. //! This build script copies the `memory.x` file from the crate root into
  36. //! a directory where the linker can always find it at build time.
  37. //! For many projects this is optional, as the linker always searches the
  38. //! project root directory -- wherever `Cargo.toml` is. However, if you
  39. //! are using a workspace or have a more complicated build setup, this
  40. //! build script becomes required. Additionally, by requesting that
  41. //! Cargo re-run the build script whenever `memory.x` is changed,
  42. //! updating `memory.x` ensures a rebuild of the application with the
  43. //! new memory settings.
  44. //!
  45. //! The build script also sets the linker flags to tell it which link script to use.
  46. use cmake::Config;
  47. use std::env;
  48. use std::fs::File;
  49. use std::io::Write;
  50. use std::path::PathBuf;
  51. fn main() {
  52. // Put `memory.x` in our output directory and ensure it's
  53. // on the linker search path.
  54. let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
  55. File::create(out.join("memory.x"))
  56. .unwrap()
  57. .write_all(include_bytes!("memory.x"))
  58. .unwrap();
  59. println!("cargo:rustc-link-search={}", out.display());
  60. // By default, Cargo will re-run a build script whenever
  61. // any file in the project changes. By specifying `memory.x`
  62. // here, we ensure the build script is only re-run when
  63. // `memory.x` is changed.
  64. println!("cargo:rerun-if-changed=memory.x");
  65. println!(
  66. "cargo:rustc-link-search=native=/usr/local/opt/arm-none-eabi-gcc/gcc/arm-none-eabi/lib"
  67. );
  68. println!("cargo:rustc-link-search=native=./c_proj/build/");
  69. println!("cargo:rustc-link-lib=static=foo");
  70. // println!("cargo:rustc-link-lib=static=c");
  71. println!("cargo:rerun-if-changed=c_proj/include/foo.h");
  72. println!("cargo:rerun-if-changed=c_proj/foo.c");
  73. let bindings = bindgen::Builder::default()
  74. .header("c_proj/include/foo.h")
  75. .use_core()
  76. .ctypes_prefix("cty")
  77. .parse_callbacks(Box::new(bindgen::CargoCallbacks))
  78. .generate()
  79. .expect("Unable to generate bindings");
  80. let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
  81. bindings
  82. .write_to_file(out_path.join("bindings.rs"))
  83. .expect("Couldn't write bindings!");
  84. }


rust main.rs

  1. #![no_std]
  2. #![no_main]
  3. #![allow(non_upper_case_globals)]
  4. #![allow(non_camel_case_types)]
  5. #![allow(non_snake_case)]
  6. include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
  7. use cortex_m_semihosting::{debug, hprintln};
  8. // pick a panicking behavior
  9. use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics
  10. // use panic_abort as _; // requires nightly
  11. // use panic_itm as _; // logs messages over ITM; requires ITM support
  12. // use panic_semihosting as _; // logs messages to the host stderr; requires a debugger
  13. use cortex_m::asm;
  14. use cortex_m_rt::entry;
  15. #[entry]
  16. fn main() -> ! {
  17. hprintln!("Hello, world!");
  18. let foo = foo_s {
  19. x: 3,
  20. y: 3.14,
  21. str_: [
  22. 116, 101, 115, 116, 105, 110, 103, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  23. ],
  24. };
  25. unsafe {
  26. print_hello_foo(&foo);
  27. }
  28. // exit QEMU
  29. // NOTE do not run this on hardware; it can corrupt OpenOCD state
  30. debug::exit(debug::EXIT_SUCCESS);
  31. loop {
  32. // your code goes here
  33. }
  34. }

hts6caw3

hts6caw31#

库具有平台依赖性,必须由您需要提供的特定于平台的移植层来解决。这是必要的,因为I/O和内存管理是平台依赖性的。
对于arm-none-eabi-gcc通常使用的Newlib C库,这是“syscalls”层。
大多数系统调用都可以作为不起作用的存根来实现。如果你只需要支持printf,那么为stdout实现write就足够了。
如果你使用动态内存分配也sbrk
如果你的应用程序是多线程的,那么你应该实现后缀为_r的可重入版本。
文档中提供了这些存根的最小实现。例如,您需要做的就是创建一个C源代码syscalls. c,包含这些存根,然后编译并将其链接到您的项目。有些需要重新实现才能正常工作,但这将允许您的代码链接。
作为对Carl Norum的评论的回应,newlib的内部部分使用动态分配。这可能是用第三方“轻量级”实现覆盖printf()的一个很好的理由。为了确保它得到支持,您至少需要有一个小堆。通常链接器脚本将创建一个堆空间(通常是静态分配后的所有可用空间),在这种情况下,下面的sbrk实现将提供一个堆:

  1. register char *stack_ptr asm("sp");
  2. void *_sbrk(ptrdiff_t incr)
  3. {
  4. extern char end asm("end"); // Defined by the linker.
  5. static char *heap_end;
  6. char *prev_heap_end;
  7. if (heap_end == NULL)
  8. heap_end = &end;
  9. prev_heap_end = heap_end;
  10. if (heap_end + incr > stack_ptr)
  11. {
  12. errno = ENOMEM;
  13. return (void *)-1;
  14. }
  15. heap_end += incr;
  16. return (void *)prev_heap_end;
  17. }

字符串
_sbrk_r()如果需要的话(多线程代码)会有一个线程本地的errno(或者只是不依赖于errno是正确的)。此外,在多线程环境中,你需要覆盖__malloc_lock()__malloc_unlock()存根来使动态内存分配线程安全。

展开查看全部

相关问题