I have a library in C with one function:
int double_input(int input) {
return input * 2;
}
If I use a build file, and call it from the program below, it works fine:
extern crate libc;
extern {
fn double_input(input: libc::c_int) -> libc::c_int;
}
fn main() {
let input = 2;
let output = unsafe { double_input(input) };
println!("{} * 2", output);
}
But when I compile that same C program as a library named "double" and try to link it with:
#[link(name = "double")]
above the extern { , it fails with:
error: linking with `cc` failed: exit code: 1
note: "cc" "-Wl,--as-needed" "-Wl,-z,noexecstack" "-m64" "-L" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib" "/root/rserve/r/target/debug/r.0.o" "-o" "/root/rserve/r/target/debug/r" "-Wl,--gc-sections" "-pie" "-nodefaultlibs" "-L" "/root/rserve/r/target/debug" "-L" "/root/rserve/r/target/debug/deps" "-L" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-Wl,-Bstatic" "-Wl,-Bdynamic" "-l" "double" "/root/rserve/r/target/debug/deps/liblibc-1bd8847afb79f283.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libstd-e8edd0fd.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcollections-e8edd0fd.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libpanic_unwind-e8edd0fd.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/librustc_unicode-e8edd0fd.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libunwind-e8edd0fd.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/librand-e8edd0fd.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc-e8edd0fd.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/liballoc_jemalloc-e8edd0fd.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/liblibc-e8edd0fd.rlib" "/usr/local/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-e8edd0fd.rlib" "-l" "util" "-l" "dl" "-l" "pthread" "-l" "gcc_s" "-l" "pthread" "-l" "c" "-l" "m" "-l" "rt" "-l" "util" "-l" "compiler-rt"
note: /usr/bin/ld: cannot find -ldouble
collect2: error: ld returned 1 exit status
I think the problem is in my Cargo.toml, but it's not clear where. The only thing I can see to add is this:
[dependencies]
libc = "0.2.0"
Thanks for your help!