OpenMP library linkage for Rust programs
This crate allows using OpenMP-dependent C code with Rust. It makes Cargo link to OpenMP, so that C static libraries linked with Rust programs can use OpenMP.
It can't be used with pure Rust programs (Rayon is a better choice for Rust).
NB: this crate can't automatically enable OpenMP for C code compiled from build scripts. You also need to pass -fopenmp
//openmp
flag to the C compiler (see usage below).
Requirements
- Rust 1.42 or later
libgomp.*
present in a directory printed bycc -print-search-dirs
, orvcomp.dll
on Windows- OpenMP-enabling flag set for any C code linked with the Rust program.
Usage
1. Adding Rust dependency
Add openmp-sys
as a runtime dependency (e.g. cargo install cargo-edit; cargo add openmp-sys
) and then add to your lib.rs
:
extern crate openmp_sys;
2. Configuring C compiler
The C code being linked must be compiled with an OpenMP-enabling flag. For projects that use the cc
crate it can be made easier. If you add openmp-sys
also as a dev-depenency, it will set DEP_OPENMP_FLAG
env var for your build.rs
script with an appropriate flag such as -fopenmp
or /openmp
, depending on the target compiler. Pass this to cc
build this way:
cc_build.flag;
Static linking
Optionally, you can enable static
feature or set OPENMP_STATIC=1
env var to link OpenMP statically, so that executables using it are usable on machines without a compiler installed. Only GCC supports this.
[]
= ["static"]
= "0.1"
macOS
In macOS Clang pretends to be the gcc
executable, but doesn't support OpenMP. Install brew install gcc
from Homebrew and compile with CC=gcc-9 cargo build
. You may need to run cargo clean
first if it doesn't take effect.
Static linking is recommended on macOS.