SZlibraco SZlibraco - 1 year ago 96
Python Question

Error when calling Rust from Python

Rust Code:

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

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


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

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

I build it to get
, and create
in this path:

from ctypes import cdll

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



Executing, and Error:

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

How to solve this problem?

Answer Source

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

pub extern fn process() { ... }

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download