rochacbruno / rust-python-example

Example of using Rust to Extend Python
https://developers.redhat.com/blog/2017/11/16/speed-python-using-rust/
789 stars 63 forks source link

ld: symbol(s) not found for architecture x86_64 #15

Open phdoerfler opened 6 years ago

phdoerfler commented 6 years ago

I'm on macOS and ran into this issue:

rustup update
git clone https://github.com/rochacbruno/rust-python-example
cd rust-python-example
make compile-rust

then I was greeted by this:

image

which after a lot of scrolling ended in this:

image
error: linking with `cc` failed: exit code: 1
  |
  = note: "cc" "-m64" "-L" …

…

 "-lSystem" "-lresolv" "-lpthread" "-lc" "-lm" "-dynamiclib" "-Wl,-dylib"
  = note: Undefined symbols for architecture x86_64:
            "_PyModule_GetFilename", referenced from:

…

                cpython::err::result_cast_from_owned_ptr::h1ac7cdf8da86aadd in libcpython-27991f0cb68c1b96.rlib(cpython-27991f0cb68c1b96.cpython0-1ffe5a9ffc73bff7490d17c35d3f3537.rs.rcgu.o)
                ...
          ld: symbol(s) not found for architecture x86_64
          clang: error: linker command failed with exit code 1 (use -v to see invocation)

What am I missing?

tcullum-gpsw commented 5 years ago

@phdoerfler did you ever figure this one out? Having same issue.

phdoerfler commented 5 years ago

@tcullum-gpsw Nope unfortunately I did not but went back to the frustration that is Cython instead

rochacbruno commented 5 years ago

I think cpython crate should be replaced with PyO3 in this repo, PyO3 is more updated and maybe solved this issue.

rochacbruno commented 5 years ago

https://github.com/PyO3/pyo3

ra0x3 commented 5 years ago

Getting the same error on MacOSX 10.14.2 using rustc 1.27.2 (58cc626de 2018-07-18), following the tutorial exactly, but it seems breaking changes were made since this initial tutorial was done

gabrielibagon commented 4 years ago

I made the needed changes to get the tutorial working on recent versions of Rust.

This works on rustc 1.42.0-nightly (0de96d37f 2019-12-19) and MacOSX 10.14.5.

As @rochacbruno mentioned, it involves using PyO3 instead of CPython.

Switch to Rust nightly (see this link):

cd <path/to/project>
rustup nightly
rustup override set nightly

Change the following files (combining the README example and the PyO3 example):

Cargo.toml:

[package]
name = "pyext-myrustlib"
version = "0.1.0"
authors = ["Bruno Rocha <rochacbruno@gmail.com>"]
edition = "2018"

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

[dependencies.pyo3]
version = "0.8.4"
features = ["extension-module"]

src/lib.rs:

use pyo3::prelude::*;
use pyo3::wrap_pyfunction;

#[pyfunction]
fn count_doubles(val: &str) -> PyResult<u64> {
    let mut total = 0u64;

    for (c1, c2) in val.chars().zip(val.chars().skip(1)) {
        if c1 == c2 {
            total += 1;
        }
    }

    Ok(total)
}

#[pymodule]
fn myrustlib(_py: Python, m: &PyModule) -> PyResult<()> {
    m.add_wrapped(wrap_pyfunction!(count_doubles))?;

    Ok(())
}

Use the following build command on MacOSX:

cargo rustc --release -- -C link-arg=-undefined -C link-arg=dynamic_lookup

The rest of the tutorial should be the same.

To fix the make command, PyO3 will have to be implemented for all of the functions (which shouldn't be too hard, following the convention shown above).

pathcl commented 1 year ago

@gabrielibagon thanks for the final command! it just worked for me. Just out of curiosity: where did get it? is there a way to have it configure globally?