rust-or / good_lp

Linear Programming for Rust, with a user-friendly API. This crate allows modeling LP problems, and lets you solve them with various solvers.
https://crates.io/crates/good_lp
MIT License
238 stars 34 forks source link

Compilation failing #45

Closed sevberg closed 6 months ago

sevberg commented 6 months ago

I've tried using this crate today, and for the most part it has gone very well. Thank you very much for the effort!

I have a simple problem that I've built from one of the examples, which runs fine with cargo run and gives the correct answer for x=0, y=6, and z=0.5. So that tells me that linkers and libraries are set-up properly

use good_lp::{
    coin_cbc, variable, Expression, ProblemVariables, ResolutionError, Solution, SolverModel,
};

fn main() -> Result<(), ResolutionError> {
    let mut problem = ProblemVariables::new();

    let x = problem.add(variable().bounds(0..).name("x"));
    let y = problem.add(variable().bounds(0..).name("y"));
    let z = problem.add(variable().bounds(0..).name("z"));

    let objective = x + (2 * y) + z;

    let c1 = ((3 * x) + y).leq(6);
    let c2 = (y + (2 * z)).leq(7);

    let model = problem
        .maximise(objective)
        .using(coin_cbc)
        .with(c1)
        .with(c2);

    let result = model.solve()?;

    let r_x = result.value(x);
    let r_y = result.value(y);
    let r_z = result.value(z);

    println!("x={} y={} z={}", r_x, r_y, r_z);

    Ok(())
}

I then wanted to do a portability test, i.e. compile a binary on one machine and run it on another (assuming the CBC libraries are present, of course). However when trying to build this with cargo build --release I encountered the following error, which I'm unsure of what to do with:

error: linking with `cc` failed: exit status: 1
  |
  = note: LC_ALL="C" PATH=... VSLANG="1033" "cc" "-m64" "/tmp/rustcg8Thj6/symbols.o" "[PROJECT]/target/release/deps/tut_goodlp-92e8b26fa0c440e6.tut_goodlp.5c9cd6f5f2b2df3d-cgu.0.rcgu.o" "[PROJECT]/target/release/deps/tut_goodlp-92e8b26fa0c440e6.tut_goodlp.5c9cd6f5f2b2df3d-cgu.1.rcgu.o" "[PROJECT]/target/release/deps/tut_goodlp-92e8b26fa0c440e6.tut_goodlp.5c9cd6f5f2b2df3d-cgu.2.rcgu.o" "[PROJECT]/target/release/deps/tut_goodlp-92e8b26fa0c440e6.tut_goodlp.5c9cd6f5f2b2df3d-cgu.3.rcgu.o" "[PROJECT]/target/release/deps/tut_goodlp-92e8b26fa0c440e6.tut_goodlp.5c9cd6f5f2b2df3d-cgu.4.rcgu.o" "[PROJECT]/target/release/deps/tut_goodlp-92e8b26fa0c440e6.1tlwplqw1rp6pnvd.rcgu.o" "-Wl,--as-needed" "-L" "[PROJECT]/target/release/deps" "-L" "/usr/lib/x86_64-linux-gnu" "-L" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "[PROJECT]/target/release/deps/libgood_lp-05f2cd015599861a.rlib" "[PROJECT]/target/release/deps/libcoin_cbc-31d39607b8b834b9.rlib" "[PROJECT]/target/release/deps/liblazy_static-df89fd9b4b197d62.rlib" "[PROJECT]/target/release/deps/libcoin_cbc_sys-6b6210d15e0954f9.rlib" "[PROJECT]/target/release/deps/libfnv-4e5ea88ba9d01e53.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-66d8041607d2929b.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-a57e2388c0aea9b1.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libobject-dcd9be90ae2cb505.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libmemchr-516789932d161b4e.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libaddr2line-1ff34b0cf871cb60.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libgimli-0c110dd0650d6cb7.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_demangle-a6e97aae2681ad8f.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd_detect-b93dac2525ec4d1e.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libhashbrown-ce1d65fb391ae98b.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_alloc-8933a2fb54d88492.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libminiz_oxide-306712ebb1ee1a3f.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libadler-349c574f342b0d30.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-65c422a3ad95273d.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcfg_if-7e6330a6c0cb9441.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-39c59240bfdfab27.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-e9d126c51bb8b2bb.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_std_workspace_core-5af394d9b1f07bdc.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-693a8f23970c5917.rlib" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcompiler_builtins-13fc9d1ed9c7a2bc.rlib" "-Wl,-Bdynamic" "-lCbcSolver" "-lCbc" "-lpthread" "-lrt" "-lCgl" "-lOsiClp" "-lClpSolver" "-lClp" "-lOsi" "-lCoinUtils" "-lbz2" "-lz" "-llapack" "-lblas" "-lm" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "[HOME]/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "[PROJECT]/target/release/deps/tut_goodlp-92e8b26fa0c440e6" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-Wl,-O1" "-nodefaultlibs"
  = note: /usr/bin/ld: cannot find -lz: No such file or directory
          collect2: error: ld returned 1 exit status

I had actually originally intended to do my portability test with Highs (which also worked with cargo run, since AFAIK that should get packaged into the release, however I encountered the same error above. This is what leads me to believe that something is not configured right (I'm sure on my end) at the good_lp-level, rather than related to one particular solver. Nevertheless, I'm not sure how to proceed..

If you could please help me understand what this error means, and how (or if?) I can get a good_lp model compiled, then I would be very grateful

In case it's helpful, I'm running this in a WSL2-Unbuntu environment, and I had to manually install the CBC libraries, build-essential, and clang (which seemed necessary to use Highs, rather than CBC)

lovasoa commented 6 months ago

The error message indicates that the linker (ld) cannot find the library -lz, which is typically the zlib library. Make sure zlib is installed on your system and that the linker can locate it. You may need to install zlib development package (sudo apt-get install zlib1g-dev) or adjust your linker settings to include the correct path to the zlib library.

sevberg commented 6 months ago

hi @lovasoa, you're absolutely right 🤦. Installing zlib fixed it. I feel like a dummy... Anyway, thank you!