zzeroo / libmodbus-rs

Libmodbus bindings for Rust
http://zzeroo.github.io/libmodbus-rs/
GNU Lesser General Public License v2.1
10 stars 5 forks source link

Many "unresolved import" and "cannot find value" errors #4

Closed kinghajj closed 7 years ago

kinghajj commented 7 years ago

I just made a new, empty project and added libmodbus-rs to my Cargo dependencies. After installing a few tools (clang, LLVM, automake), I eventually ran into these errors:

    error[E0432]: unresolved import `libmodbus_sys::MODBUS_TCP_MAX_ADU_LENGTH`
       --> $HOME/.cargo/registry/src/github.com-1ecc6299db9ec823/libmodbus-rs-0.4.3/src/lib.rs:203:9
        |
    203 | pub use libmodbus_sys::MODBUS_TCP_MAX_ADU_LENGTH;
        |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `MODBUS_TCP_MAX_ADU_LENGTH` in the root. Did you mean to use `MODBUS_MAX_ADU_LENGTH`?

    error[E0432]: unresolved import `libmodbus_sys::MODBUS_RTU_MAX_ADU_LENGTH`
       --> $HOME/.cargo/registry/src/github.com-1ecc6299db9ec823/libmodbus-rs-0.4.3/src/lib.rs:204:9
        |
    204 | pub use libmodbus_sys::MODBUS_RTU_MAX_ADU_LENGTH;
        |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ no `MODBUS_RTU_MAX_ADU_LENGTH` in the root. Did you mean to use `MODBUS_MAX_ADU_LENGTH`?

    error[E0425]: cannot find value `MODBUS_RTU_RS485` in module `libmodbus_sys`
      --> $HOME/.cargo/registry/src/github.com-1ecc6299db9ec823/libmodbus-rs-0.4.3/src/modbus_rtu.rs:13:39
       |
    13 |     MODBUS_RTU_RS485 = libmodbus_sys::MODBUS_RTU_RS485 as isize,
       |                                       ^^^^^^^^^^^^^^^^ did you mean `MODBUS_RTU_RS232`?
       |
       = help: possible candidate is found in another module, you can import it into scope:
                 `use modbus_rtu::SerialMode::MODBUS_RTU_RS485;`

    error[E0425]: cannot find value `MODBUS_RTU_RTS_DOWN` in module `libmodbus_sys`
      --> $HOME/.cargo/registry/src/github.com-1ecc6299db9ec823/libmodbus-rs-0.4.3/src/modbus_rtu.rs:20:42
       |
    20 |     MODBUS_RTU_RTS_DOWN = libmodbus_sys::MODBUS_RTU_RTS_DOWN as isize,
       |                                          ^^^^^^^^^^^^^^^^^^^ did you mean `MODBUS_RTU_RTS_NONE`?
       |
       = help: possible candidate is found in another module, you can import it into scope:
                 `use modbus_rtu::RequestToSendMode::MODBUS_RTU_RTS_DOWN;`

    error[E0531]: cannot find unit struct/variant or constant `MODBUS_RTU_RS485` in module `libmodbus_sys`
       --> $HOME/.cargo/registry/src/github.com-1ecc6299db9ec823/libmodbus-rs-0.4.3/src/modbus_rtu.rs:194:32
        |
    194 |                 libmodbus_sys::MODBUS_RTU_RS485 => Ok(SerialMode::MODBUS_RTU_RS485),
        |                                ^^^^^^^^^^^^^^^^ did you mean `MODBUS_RTU_RS232`?
        |
        = help: possible candidate is found in another module, you can import it into scope:
                  `use modbus_rtu::SerialMode::MODBUS_RTU_RS485;`

    error[E0531]: cannot find unit struct/variant or constant `MODBUS_RTU_RTS_DOWN` in module `libmodbus_sys`
       --> $HOME/.cargo/registry/src/github.com-1ecc6299db9ec823/libmodbus-rs-0.4.3/src/modbus_rtu.rs:233:32
        |
    233 |                 libmodbus_sys::MODBUS_RTU_RTS_DOWN => Ok(RequestToSendMode::MODBUS_RTU_RTS_DOWN),
        |                                ^^^^^^^^^^^^^^^^^^^ did you mean `MODBUS_RTU_RTS_NONE`?
        |
        = help: possible candidate is found in another module, you can import it into scope:
                  `use modbus_rtu::RequestToSendMode::MODBUS_RTU_RTS_DOWN;`

    error[E0531]: cannot find unit struct/variant or constant `MODBUS_RTU_RTS_DOWN` in module `libmodbus_sys`
       --> $HOME/.cargo/registry/src/github.com-1ecc6299db9ec823/libmodbus-rs-0.4.3/src/modbus_rtu.rs:269:32
        |
    269 |                 libmodbus_sys::MODBUS_RTU_RTS_DOWN => Ok(RequestToSendMode::MODBUS_RTU_RTS_DOWN),
        |                                ^^^^^^^^^^^^^^^^^^^ did you mean `MODBUS_RTU_RTS_NONE`?
        |
        = help: possible candidate is found in another module, you can import it into scope:
                  `use modbus_rtu::RequestToSendMode::MODBUS_RTU_RTS_DOWN;`

    error: aborting due to 7 previous errors

    error: Could not compile `libmodbus-rs`.

Any ideas? Are features of the C library being elided because of other dependencies I'm missing, causing bindgen's output to exclude them?

zzeroo commented 7 years ago

Do you have a libmodbus installation on your system? This implementation needs a very recent libmodbus version, bf3004743cb5dcab6937946c7791770ed5d1f410 at the moment.

The build.rs is so constructed that it first tries to find a libmodbus via pkg-config. If successful build.rs use this one. If no libmodbus is found the build.rs compiles the libmodbus from the git submodule part of this crate.

zzeroo commented 7 years ago

But the more I think about your error, I think there must be an other problem.

I think I need more infos about your setup...

kinghajj commented 7 years ago

I don't believe I have installed any libmodbus; I thought from the repository layout that one was included as a submodule and would be built along with libmodbus-sys. I'm on Fedora and just dnf install libmodbus-devel, which is version 3.0.6-1, but got the same errors. I'll try building the library at that SHA1 and report back what happens then.

zzeroo commented 7 years ago

Sure that's the problem. Your dnf install libmodbus-devel installs version 3.0.6-1 of libmodbus. But this crate needs a libmodbus > version 3.1.4.

So you have two options. 1) install libmodbus from source https://github.com/stephane/libmodbus#installation. This should overwrite your local 3.0.6-1 installation. or 2) deinstall your local 3.0.6-1 libmodbus dnf remove libmodbus-devel. If you have no local libmodbus the crates build.rs file compiles the right one from the crates sub module.

Both solution should work for you.

kinghajj commented 7 years ago

I tried both options, and keep getting the same errors. I'm looking at the bindings in the target directory, and it indeed looks like some parts are just getting skipped. For instance, here's a section from modbus-rtu.h:

#define MODBUS_RTU_RS232 0
#define MODBUS_RTU_RS485 1
#define MODBUS_RTU_RTS_NONE   0
#define MODBUS_RTU_RTS_UP     1
#define MODBUS_RTU_RTS_DOWN   2

And here's the bindings generated from that:

pub const MODBUS_RTU_RS232: ::std::os::raw::c_uint = 0;
pub const MODBUS_RTU_RTS_NONE: ::std::os::raw::c_uint = 0;
pub const MODBUS_RTU_RTS_UP: ::std::os::raw::c_uint = 1;

Maybe this is some sort of weird bindgen bug?

zzeroo commented 7 years ago

Bindgen was my next bet. Please check the bindgen requirements https://servo.github.io/rust-bindgen/requirements.html

kinghajj commented 7 years ago

It seems like this is caused by a bug in bindgen; gonna close this issue and open one for that project.

emilio commented 7 years ago

Yeah, we debugged this this evening, and it's due to a libclang bug (which is the library that bindgen uses). In particular it's https://llvm.org/bugs/show_bug.cgi?id=9069, which I fixed in https://reviews.llvm.org/D26446, so it's libclang 4.0+ only unfortunately.

I couldn't find a good solution for this while chatting on IRC, but I think I got one that is good enough and simple.

kinghajj commented 7 years ago

I'm wondering, does this issue only manifest with Clang 3.9? Which version do you use, zzeroo?

emilio commented 7 years ago

It does manifest with any version previous to 4.0. I've opened https://github.com/servo/rust-bindgen/pull/750, if you verify that fixes the issue, it'd be rad :)

zzeroo commented 7 years ago

@kinghajj I use clang 4.0, the travis CI as well https://github.com/zzeroo/libmodbus-rs/blob/master/.travis.yml#L16. These builds are working

On docs.rs I've an issue as well. I think the cause is the same: https://docs.rs/crate/libmodbus-rs/0.4.3/builds/59047