Cannot call external C function in Rust
⚓ Rust 📅 2025-12-12 👤 surdeus 👁️ 1When I run cargo run, I receive the following error. I am not able to figure why rust is not detecting the badmath function. I have provided all code setup here.
➜ unssafe git:(master) ✗ cargo run --verbose
Fresh find-msvc-tools v0.1.5
Fresh shlex v1.3.0
Fresh cfg-if v1.0.4
Fresh cc v1.2.49
Fresh libloading v0.9.0
Fresh cmake v0.1.55
Dirty unssafe v0.1.0 (/Users/{username}/Documents/developer/rustlearning/unssafe): the precalculated components changed
Compiling unssafe v0.1.0 (/Users/username/Documents/developer/rustlearning/unssafe)
Running `/Users/username/Documents/developer/rustlearning/unssafe/target/debug/build/unssafe-2a5c1942373426cc/build-script-build`
Running `/Users/username/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rustc --crate-name unssafe --edition=2024 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=95 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=aaab07e41384c66e -C extra-filename=-6af635420abb50d0 --out-dir /Users/username/Documents/developer/rustlearning/unssafe/target/debug/deps -C incremental=/Users/username/Documents/developer/rustlearning/unssafe/target/debug/incremental -L dependency=/Users/username/Documents/developer/rustlearning/unssafe/target/debug/deps --extern libloading=/Users/username/Documents/developer/rustlearning/unssafe/target/debug/deps/liblibloading-363423c133c5393d.rlib -l static=badmath`
error: linking with `cc` failed: exit status: 1
|
= note: "cc" "/var/folders/s7/042_vps90354b58lxyr0wkvh0000gn/T/rustc8iF8U3/symbols.o" "<8 object files omitted>" "-lbadmath" "<sysroot>/lib/rustlib/aarch64-apple-darwin/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,libcfg_if-*,liblibc-*,librustc_std_workspace_core-*,liballoc-*,libcore-*,libcompiler_builtins-*}.rlib" "-lSystem" "-lc" "-lm" "-arch" "arm64" "-mmacosx-version-min=11.0.0" "-o" "/Users/username/Documents/developer/rustlearning/unssafe/target/debug/deps/unssafe-6af635420abb50d0" "-Wl,-dead_strip" "-nodefaultlibs"
= note: some arguments are omitted. use `--verbose` to show all linker arguments
= note: ld: library 'badmath' not found
clang: error: linker command failed with exit code 1 (use -v to see invocation)
error: could not compile `unssafe` (bin "unssafe") due to 1 previous error
Caused by:
process didn't exit successfully: `/Users/username/.rustup/toolchains/stable-aarch64-apple-darwin/bin/rustc --crate-name unssafe --edition=2024 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=95 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C split-debuginfo=unpacked --check-cfg 'cfg(docsrs,test)' --check-cfg 'cfg(feature, values())' -C metadata=aaab07e41384c66e -C extra-filename=-6af635420abb50d0 --out-dir /Users/shoebilyas/Documents/developer/rustlearning/unssafe/target/debug/deps -C incremental=/Users/shoebilyas/Documents/developer/rustlearning/unssafe/target/debug/incremental -L dependency=/Users/shoebilyas/Documents/developer/rustlearning/unssafe/target/debug/deps --extern libloading=/Users/shoebilyas/Documents/developer/rustlearning/unssafe/target/debug/deps/liblibloading-363423c133c5393d.rlib -l static=badmath` (exit status: 1)
// Filepath: libbadmath/CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(LibBadMath C)
add_library(badmath STATIC badmath.c)
install(TARGETS badmath DESTINATION .)
// Filepath: libbadmath/badmath.c
#include <stdio.h>
float bad_add(float a, float b) {
printf("Adding %f and %f", a,b);
return a + b + 10.0;
}
// Filepath: src/main.rc
#[link(name = "badmath", kind = "static")]
unsafe extern "C" {
fn bad_add(a: f32, b: f32) -> f32;
}
fn main() {
println!("Calling bad_math");
let result = unsafe { bad_add(5.2, 10.0) };
println!("Received = {}", result);
}
// Filepath: ./build.rs
extern crate cmake;
use cmake::Config;
fn main() {
let dst = Config::new("libbadmath").build();
println!("cargo:rustc-link-search-native={}", dst.display());
println!("cargo:rustc-link-lib=static=badmath");
}
1 post - 1 participant
🏷️ Rust_feed