4

Rust Code:

#[no_mangle]
use std::thread;

pub extern fn process() {
    let handles: Vec<_> = (0..10).map(|_| {
        thread::spawn(|| {
            let mut _x = 0;
            for _ in (0..5_000_001) {
                _x += 1
            }
        })
    }).collect();

    for h in handles {
        h.join().ok().expect("Could not join a thread!");
    }
}

Cargo.toml:

[package]
name = "embed"
version = "0.1.0"
authors = ["hustlibraco <[email protected]>"]

[lib]
name = "embed"
crate-type = ["dylib"]

I build it to get target/release/libembed.so, and create invoke.py in this path:

from ctypes import cdll

lib = cdll.LoadLibrary("target/release/libembed.so")

lib.process()

print("done!")

Executing, and Error:

-bash-4.2# python invoke.py 
Traceback (most recent call last):
  File "invoke.py", line 5, in <module>
    lib.process()
  File "/usr/lib64/python2.7/ctypes/__init__.py", line 373, in __getattr__
    func = self.__getitem__(name)
  File "/usr/lib64/python2.7/ctypes/__init__.py", line 378, in __getitem__
    func = self._FuncPtr((name_or_ordinal, self))
AttributeError: target/release/libembed.so: undefined symbol: process

How to solve this problem?

1 Answer 1

4

The #[no_mangle] goes on the function, not on the use.

#[no_mangle]
pub extern fn process() { ... }

For a complete, working example, see the Rust FFI Omnibus "Integers" example.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.