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).
libgomp.*
present in a directory printed by cc -print-search-dirs
, or vcomp.dll
on WindowsAdd openmp-sys
as a runtime dependency (e.g. cargo install cargo-edit; cargo add openmp-sys
) and then add to your lib.rs
:
rust
extern crate openmp_sys;
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:
rust
cc_build.flag(&std::env::var("DEP_OPENMP_FLAG").unwrap());
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.
toml
[dependencies.openmp]
features = ["static"]
version = "0.1"
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.