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.

1 Like