Linking Library C with Rust Programm


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


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

Yes, you can use cmake crate for this.


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


Thanks ,
I try to use the example in :
I have some probleme with the call of cmake.

unlike the example, my source file contains:

├── 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
│ └──

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


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


You can add 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.