2

我目前正在使用 C 和 Rust 开发一个应用程序,当它以下列任何一种方式使用时,我都没有任何问题:

  • C -> Rust -> C (swift)
  • C -> Rust -> 示例(控制台 Rust)

但是当我想通过 Yew 中的 wasm 访问时,它变得越来越复杂

  • C -> Rust -> Yew/WASM (cargo web start)

链接器抛出错误:缺少“math.h”

维基百科引用:https ://en.wikipedia.org/wiki/C_mathematical_functions

在 Linux 和 BSD 下,数学函数(如 中声明的)分别捆绑在数学库 libm 中。因此,如果使用这些函数中的任何一个,则必须为链接器提供指令 -lm。

虽然我添加了标志,但它并没有改变任何东西......

下面我包含了build.rs来自我的库文件绑定 C -> Rust 的文件:

use std::env;
use std::path::Path;

fn main() {
    let dir = env::var("CARGO_MANIFEST_DIR").unwrap();
    cc::Build::new()
        .flag("-g")
        //        .flag("-09")
       .flag("-lm") // math.h for wasm (dosen't work or without)
       .flag("-Wall")
       .file(Path::new(&dir).join("src/swisseph/2.08/src/swecl.c"))
       .file(Path::new(&dir).join("src/swisseph/2.08/src/swedate.c"))
       .file(Path::new(&dir).join("src/swisseph/2.08/src/swehel.c"))
       .file(Path::new(&dir).join("src/swisseph/2.08/src/swehouse.c"))
       .file(Path::new(&dir).join("src/swisseph/2.08/src/swejpl.c"))
       .file(Path::new(&dir).join("src/swisseph/2.08/src/swemmoon.c"))
       .file(Path::new(&dir).join("src/swisseph/2.08/src/swemplan.c"))
       .file(Path::new(&dir).join("src/swisseph/2.08/src/swepcalc.c"))
       .file(Path::new(&dir).join("src/swisseph/2.08/src/sweph.c"))
       .file(Path::new(&dir).join("src/swisseph/2.08/src/swephlib.c"))
       .compile("swe")
  }

这是我在 yew (cargo web start) 中编译的错误日志

Compiling libswe-sys v0.1.4
Compiling stdweb v0.4.20
error: failed to run custom build command for libswe-sys v0.1.4

--- stdout
TARGET = Some("wasm32-unknown-unknown")
OPT_LEVEL = Some("0")
HOST = Some("x86_64-apple-darwin")
CC_wasm32-unknown-unknown = None
CC_wasm32_unknown_unknown = None
TARGET_CC = None
CC = None
CFLAGS_wasm32-unknown-unknown = None
CFLAGS_wasm32_unknown_unknown = None
TARGET_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("true")
running: "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-Wall" "-Wextra" "-g" "-lm" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-2679bddccf8cd805/out/swedate.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swedate.c"
running: "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-Wall" "-Wextra" "-g" "-lm" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-2679bddccf8cd805/out/swecl.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swecl.c"
running: "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-Wall" "-Wextra" "-g" "-lm" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-2679bddccf8cd805/out/swehel.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swehel.c"
cargo:warning=clang: warning: -lm: 'linker' input unused [-Wunused-command-line-argument]
cargo:warning=clang: warning: -lm: 'linker' input unused [-Wunused-command-line-argument]
cargo:warning=clang: warning: -lm: 'linker' input unused [-Wunused-command-line-argument]
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swedate.c:86:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swephexp.h:83:
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/sweodef.h:167:10: fatal error: 'math.h' file not found
cargo:warning=#include <math.h>
cargo:warning=         ^~~~~~~~
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swecl.c:62:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swejpl.h:67:
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/sweodef.h:167:10: fatal error: 'math.h' file not found
cargo:warning=#include <math.h>
cargo:warning=         ^~~~~~~~
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swehel.c:68:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swephexp.h:83:
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/sweodef.h:167:10: fatal error: 'math.h' file not found
cargo:warning=#include <math.h>
cargo:warning=         ^~~~~~~~`
cargo:warning=1 error generated.
exit code: 1
running: "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-Wall" "-Wextra" "-g" "-lm" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-2679bddccf8cd805/out/swehouse.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swehouse.c"
cargo:warning=1 error generated.
exit code: 1
cargo:warning=clang: warning: -lm: 'linker' input unused [-Wunused-command-line-argument]
cargo:warning=1 error generated.
exit code: 1
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swehouse.c:63:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swephexp.h:83:
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/sweodef.h:167:10: fatal error: 'math.h' file not found
cargo:warning=#include <math.h>
cargo:warning=         ^~~~~~~~
cargo:warning=1 error generated.
exit code: 1

--- stderr


error occurred: Command "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-Wall" "-Wextra" "-g" "-lm" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-2679bddccf8cd805/out/swecl.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.4/src/swisseph/2.08/src/swecl.c" with args "clang" did not execute successfully (status code exit code: 1).



warning: build failed, waiting for other jobs to finish...

你有什么建议?

  • 找到一个 math.c/math.h 文件并将其包含在我的库中 c -> rust
  • 其他建议???

编辑:

我尝试了两种不起作用的方法:

  • 在我的 Mac 上从 /Library/Developer/CommandLineTools/SDKs/MacOSX10.15.sdk/usr/include/math.h 手动添加一个 math.h
  • 在我的库的 build.rs 中添加 .include("/usr/include")

这两种方式编译我的 libswe-sys(0.1.5 和 0.1.6)

这是 wasm 的 yew 日志:

  • 使用 0.1.5(手动添加 math.h)
error: failed to run custom build command for `libswe-sys v0.1.5`

--- stdout
TARGET = Some("wasm32-unknown-unknown")
OPT_LEVEL = Some("0")
HOST = Some("x86_64-apple-darwin")
CC_wasm32-unknown-unknown = None
CC_wasm32_unknown_unknown = None
TARGET_CC = None
CC = None
CFLAGS_wasm32-unknown-unknown = None
CFLAGS_wasm32_unknown_unknown = None
TARGET_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("true")
running: "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-Wall" "-Wextra" "-g" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-b52a4354b3363d0b/out/swedate.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swedate.c"
running: "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-Wall" "-Wextra" "-g" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-b52a4354b3363d0b/out/swecl.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swecl.c"
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swedate.c:86:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swephexp.h:83:
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/sweodef.h:167:10: error: 'math.h' file not found with <angled> include; use "quotes" instead
cargo:warning=#include <math.h>
cargo:warning=         ^~~~~~~~
cargo:warning=         "math.h"
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swecl.c:62:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swejpl.h:67:
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/sweodef.h:167:10: error: 'math.h' file not found with <angled> include; use "quotes" instead
cargo:warning=#include <math.h>
cargo:warning=         ^~~~~~~~
cargo:warning=         "math.h"
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swedate.c:86:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swecl.c:62:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swejpl.h:67:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swephexp.h:83:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/sweodef.h:167:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/sweodef.h:167:
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/math.h:295:10: fatal error: '__config' file not found
cargo:warning=#include <__config>
cargo:warning=         ^~~~~~~~~~
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/math.h:295:10: fatal error: '__config' file not found
cargo:warning=#include <__config>
cargo:warning=         ^~~~~~~~~~
cargo:warning=2 errors generated.
exit code: 1
running: "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-Wall" "-Wextra" "-g" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-b52a4354b3363d0b/out/swehel.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swehel.c"
cargo:warning=2 errors generated.
exit code: 1
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swehel.c:68:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swephexp.h:83:
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/sweodef.h:167:10: error: 'math.h' file not found with <angled> include; use "quotes" instead
cargo:warning=#include <math.h>
cargo:warning=         ^~~~~~~~
cargo:warning=         "math.h"
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swehel.c:68:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swephexp.h:83:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/sweodef.h:167:
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/math.h:295:10: fatal error: '__config' file not found
cargo:warning=#include <__config>
cargo:warning=         ^~~~~~~~~~
cargo:warning=2 errors generated.
exit code: 1

--- stderr


error occurred: Command "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-Wall" "-Wextra" "-g" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-b52a4354b3363d0b/out/swecl.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.5/src/swisseph/2.08/src/swecl.c" with args "clang" did not execute successfully (status code exit code: 1).



warning: build failed, waiting for other jobs to finish...
error: build failed
error: build failed
  • 0.1.6(在我的库的 build.rs 中添加 .include("/usr/include")
error: failed to run custom build command for `libswe-sys v0.1.6`

--- stdout
TARGET = Some("wasm32-unknown-unknown")
OPT_LEVEL = Some("0")
HOST = Some("x86_64-apple-darwin")
CC_wasm32-unknown-unknown = None
CC_wasm32_unknown_unknown = None
TARGET_CC = None
CC = None
CFLAGS_wasm32-unknown-unknown = None
CFLAGS_wasm32_unknown_unknown = None
TARGET_CFLAGS = None
CFLAGS = None
CRATE_CC_NO_DEFAULTS = None
DEBUG = Some("true")
running: "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "/usr/include" "-Wall" "-Wextra" "-g" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-08f220b2455dd53e/out/swecl.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/swecl.c"
running: "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "/usr/include" "-Wall" "-Wextra" "-g" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-08f220b2455dd53e/out/swedate.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/swedate.c"
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/swedate.c:86:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/swephexp.h:83:
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/sweodef.h:167:10: fatal error: 'math.h' file not found
cargo:warning=#include <math.h>
cargo:warning=         ^~~~~~~~
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/swecl.c:62:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/swejpl.h:67:
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/sweodef.h:167:10: fatal error: 'math.h' file not found
cargo:warning=#include <math.h>
cargo:warning=         ^~~~~~~~
cargo:warning=1 error generated.
exit code: 1
running: "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "/usr/include" "-Wall" "-Wextra" "-g" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-08f220b2455dd53e/out/swehel.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/swehel.c"
cargo:warning=1 error generated.
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/swehel.c:68:
cargo:warning=In file included from /Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/swephexp.h:83:
cargo:warning=/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/sweodef.h:167:10: fatal error: 'math.h' file not found
cargo:warning=#include <math.h>
cargo:warning=         ^~~~~~~~
exit code: 1
cargo:warning=1 error generated.
exit code: 1

--- stderr


error occurred: Command "clang" "clang" "-O0" "-ffunction-sections" "-fdata-sections" "-fPIC" "-g" "-fno-omit-frame-pointer" "--target=wasm32-unknown-unknown" "-I" "/usr/include" "-Wall" "-Wextra" "-g" "-Wall" "-o" "/Users/stephanebressani/Code/Rust/yewastrology/target/wasm32-unknown-unknown/debug/build/libswe-sys-08f220b2455dd53e/out/swecl.o" "-c" "/Users/stephanebressani/.cargo/registry/src/github.com-1ecc6299db9ec823/libswe-sys-0.1.6/src/swisseph/2.08/src/swecl.c" with args "clang" did not execute successfully (status code exit code: 1).



warning: build failed, waiting for other jobs to finish...
error: build failed
error: build failed

这是我的 yew 设置(非常小,就像一个 hello world 项目):

货运.toml

[package]
name = "yewastrology"
version = "0.1.0"
authors = ["stephaneworkspace <s.bressani@bluewin.ch>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
yew = "0.11.0"
astrology = "0.1.24"

src/main.rs

use astrology::{WorkingStorageYew, YewAction, YewAstro};
use yew::{
    html, Callback, ClickEvent, Component, ComponentLink, Html, ShouldRender,
};

struct App {
    clicked: bool,
    onclick: Callback<ClickEvent>,
}

enum Msg {
    Click,
}

impl Component for App {
    type Message = Msg;
    type Properties = ();

    fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
        App {
            clicked: false,
            onclick: link.callback(|_| Msg::Click),
        }
    }

    fn update(&mut self, msg: Self::Message) -> ShouldRender {
        match msg {
            Msg::Click => {
                self.clicked = true;
                true // Indicate that the Component should re-render
            },
        }
    }

    fn view(&self) -> Html {
        let button_text: String = if self.clicked {
            // Ephem file and better path todo later !
            let ws = WorkingStorageYew::new(
                "/Users/stephanebressani/Code/Rust/yewastrology/data.json",
                "",
                550,
            );
            let ok = ws.yew_draw_chart(YewAction::Chart);
            let result = ok.0;
            result
        } else {
            "Click me!".to_string()
        };

        if !self.clicked {
            html! {
                <button onclick=&self.onclick>{ button_text }</button>
            }
        } else {
            html! {
                <h1>{ button_text }</h1>
            }
        }
    }
}

fn main() {
    yew::start_app::<App>();
}

和 data.json

{
        "year": 1999,
        "month": 12,
        "day": 31,
        "hourf64": 23.59,
        "hour": 23,
        "min": 59,
        "sec": 0.0,
        "lat": 46.0222,
        "lng": 6.14569
}

对于 0.1.5(手动添加 math.h)源不兼容,你有兼容的吗?

我刚刚在我的 mac book 上的搜索中尝试了第一个(7 在不同路径的搜索结果中)

编辑 2:

我找到了这个 :

https://github.com/rust-lang/libc/issues/858

根据定义,wasm32-unknown-unknown 没有 libc。如果您想使用 emscripten 或 wasi,您可以使用 wasm32-unknown-emscripten 或 wasm32-wasi,这两种方法都已在 libc crate 中得到支持。

我尝试使用 emscripter (问题编译,因为我的货物中的 lib、ansi_term 库是不兼容的)和 wasi 与 wasm32-unknown-unknown 相同的问题,math.h 的问题

4

1 回答 1

1

您将两件事混为一谈:-lm链接器的标志是链接libm.so.6库;未math.h找到的错误是针对头文件的。

libc6-dev很可能,您的 Linux 机器上缺少软件包。

由于您没有提供最小的可重现示例,因此这是我尝试过的示例,它按预期工作。项目布局:

├── build.rs
├── Cargo.toml
└── src
    ├── cmath.c
    └── main.rs

货物 toml 文件:

[package]
name = "math-ffi"
version = "0.1.0"
authors = ["rustacean"]
edition = "2018"

[dependencies]
libc = "0.2"

[build-dependencies]
cc = "1.0"

build.rs: _

fn main() {
    cc::Build::new()
        .include("/usr/include")  // standard location, YMMV
        .flag("-g")
        .flag("-Wall")
        .file("src/cmath.c")
        .compile("math_ffi");
}

C文件:

#include <math.h>

#define PI 3.14159265

double cos_radian(double x) {
    return cos(x * PI / 180);
}

还有 Rust 入口点:

use libc::c_double;

extern "C"  {
    fn cos_radian(_: c_double) -> c_double;
}

fn main() {
    let res = unsafe {
        cos_radian(180.0)
    };
    println!("{}", res);
}

它成功构建并-1正确返回。并ldd显示它应该链接到libm

$ ldd target/debug/math-ffi
    linux-vdso.so.1 (0x00007ffc60db1000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f8e889e3000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f8e889c0000)
    libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f8e889a6000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8e887b5000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f8e88a43000)
    libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8e88666000)
于 2020-02-12T17:01:37.903 回答