Linking Library C with Rust Programm


#1

Dear Rust users,

As part of a project, I have to use functions of a lib C in my program.
I would like to link a dynamic library compiled with CMake with a program Rust.
I need to compile my lib at the same time as my Rust program:
Is there a simple way to build a program in Rust with cmake?

I’d be very happy if someone could tell me how to resolve the problem


#2

Is there a simple way to build a program in Rust with cmake?

Yes, you can use cmake crate for this.


#3

There was also an article posted recently, Using Rust Code in a C/C++ Project with CMake that might be useful.


#4

Thanks ,
I try to use the example in : https://docs.rs/cmake/0.1.31/cmake/
I have some probleme with the call of cmake.

unlike the example, my source file contains:

.src
├── fastscan.c
├── gw_cdm
│ └── gw_cdm.c
├── gw_conf
│ └── gw_conf.c
├── gw_filter
│ └── gw_filter.c
├── gw_frames_info_parser
│ └── gw_frames_info_parser.c
├── gw_modcod
│ ├── gw_modcod.c
│ └── gw_modcod_private.h
├── gw_net
│ ├── gw_net.c
│ ├── main
│ └── main.rs

My lib libgw is compuled with cmake and I want just to link gw_net net with my Rust program


#5

I’m looking to Use C Code in a Rust project by using Cmake


#6

You can add build.rs and use either cmake or cc crate to compile and link it with the Rust program.

If it’s a library that’s generally available and you’d like to make it easily usable by anyone, you can wrap that in reusable sys crate.