Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link C library with Rust project #2317

Open
MarcAntoine-Arnaud opened this issue Sep 23, 2020 · 3 comments
Open

Link C library with Rust project #2317

MarcAntoine-Arnaud opened this issue Sep 23, 2020 · 3 comments
Labels

Comments

@MarcAntoine-Arnaud
Copy link

Describe the Bug

I have a part of the project in C, so I use emscripten to compile it (the complex Makefile is already present, that's why I don't want to use cc).
With emscripten I generate a lib.a file.

On a second time I have a Rust project to integrate the process.
For that I have defined the C API to call FFI from Rust and in the build.rs I add the link to the lib.a.

Using wasm-pack build everything is builded - not tested yet in web environnement.
But in my case I need to target the Web Worker, so I try to compile with wasm-pack build --no-module which provides me that error:

error: cannot import from modules (`env`) with `--no-modules`

Does I missed something ?
Is it linked to a configuration on Emscripten side ?
Does I need to use Wasm in Wasm ? So generate a first wasm with Emscripten, then include bytes in the Rust code ?

Additional Context

Emscripten is used on Ubuntu (docker), Rust on Mac OSX.

@MarcAntoine-Arnaud
Copy link
Author

I have discovered it appears when I return a pointer from a function.
For example, that works:

  • C part
int wasm_c_add(int a, int b) {
	return a + b;
}
  • Rust side
#[link(name = "wasm_c_add")]
extern {
  pub fn wasm_c_add(a: c_int, b: c_int) -> c_int;
}

#[wasm_bindgen]
pub fn add() {
  let sum = unsafe {
    wasm_c_add(3, 6)
  };
  log::debug!("Addition = {}", sum);
}

But if I wrote:

  • C part
struct Something {
	const char* value;
} Something;

struct Something* wasm_c_something() {
	struct Something* something = malloc(sizeof(Something));
	return something;
}
  • Rust part
#[repr(C)]
pub struct Something {
  pub value: *const c_char,
}

#[link(name = "wasm_c_add")]
extern {
  pub fn wasm_c_something() -> *mut Something;
}


#[wasm_bindgen]
pub fn generate() {
  let _something = unsafe {
    wasm_c_something()
  };
}

it return the error:

error: cannot import from modules (`env`) with `--no-modules`

I don't know if it can have a link with #190. But I have founded some support here

@lights0123
Copy link

lights0123 commented Oct 3, 2020

That means that you're attempting to link to an undefined function. Upload your wasm file here, and look for functions at the top marked as import "env".

@chrysn
Copy link
Contributor

chrysn commented Sep 1, 2022

Linking between C and wasm-bindgen Rust seems to still have several pitfalls discussed in #2209; could you be running into these?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants