Build.sh extern C lib with cc?

example: I have install on my machine "some" library, how to link it properly in rust project?
/etc/include/some/version.h
/etc/lib/libsome.so

exec c file

#include <some/version.h>
#include <stdio.h>

int test_version() {
  return some_version();
}

int main() {
  printf("some ex. lib ver = %d\n", test_version());
}

clang -o test test.c -lsome && ./test # works

Now i want to have same in cargo

Cargo.toml
build = build.rs
[dependencies]
libc = "0.1"

[build-dependencies]
cc = "1.0"

src/main.rs
extern crate libc;

use libc::int32_t;

extern "C" {
    fn test_version() -> int32_t;
}

fn ffi_version() -> i32 {
    unsafe { test_version() as i32 }
}
fn main() {
    println!("{}", ffi_version());
}

src/test.c

#include <stdio.h>
#include <some/version.h>
int test_version() {
  return some_version();
}

build.rs
extern crate cc;

fn main() {
  cc::Build::new()
     // add -lsome flag ???
     .file("src/test.c")
     .compile("libtest.a")

Wrapping a C library like this is usually handled with a pattern called a "-sys crate." Here's some instructions on how to do that..

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.