LightGBM [R Package] OpenMP test ignores LDFLAGS (fix provided). - Cplusplus


The LDFLAGS R variable "R CMD config LDFLAGS" is not included as part of the OpenMP test in the R package. This causes the compilation to not find OpenMP libraries due to ignoring the -L directories in .R/Makevars. This is a larger issue on the Mac where -L is usually needed to find the OpenMP libraries. Furthermore, since LDFLAGS are used for the package compilation, it would be a more reasonable test if LDFLAGS was used.

I wrote a fixed, but I"m not sure how you would want to merge it due to the need to re-run autoconf.

Reproducible example

From the Mac

Current version does not look at the LDFLAGS when compiling the OpenMP test, therefore the test fails

/opt/homebrew/Cellar/llvm/11.1.0/bin/clang -o conftest conftest.c -lomp -Xclang -fopenmp

After adding the changes to and running autoconf I was able to fix the issue and pass the OpenMP test by adding ${LDFLAGS} to the OpenMP test.

/opt/homebrew/Cellar/llvm/11.1.0/bin/clang -o conftest conftest.c -L/usr/local/lib -L/opt/homebrew/lib -lomp -L/opt/homebrew/opt/llvm/lib -Wl,-rpath,/opt/homebrew/opt/llvm/lib -lomp -Xclang -fopenmp

Environment info

LightGBM version or commit hash:

M1 Mac running big-sur (R 4.0.3.). home-brew llvm 11.1.0 + libomp

cd lightgbm R CMD INSTALL .

Additional Comments


CC=/opt/homebrew/Cellar/llvm/11.1.0/bin/clang CXX=/opt/homebrew/Cellar/llvm/11.1.0/bin/clang++ CXX11=/opt/homebrew/Cellar/llvm/11.1.0/bin/clang++ CXX14=/opt/homebrew/Cellar/llvm/11.1.0/bin/clang++ CXX17=/opt/homebrew/Cellar/llvm/11.1.0/bin/clang++ CXX20=/opt/homebrew/Cellar/llvm/11.1.0/bin/clang++

CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe -fopenmp -I/opt/homebrew/include -I/opt/homebrew/opt/llvm/include CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe -fopenmp -I/opt/homebrew/include -I/opt/homebrew/opt/llvm/include LDFLAGS=-L/usr/local/lib -L/opt/homebrew/lib -lomp -L/opt/homebrew/opt/llvm/lib -Wl,-rpath,/opt/homebrew/opt/llvm/lib

Asked Oct 16 '21 01:10
avatar jlisic

7 Answer:

Thanks very much for this excellent report @jlisic ! We'd be grateful for a pull request that adds this, it looks like a good change to me.

I wrote a fixed, but I"m not sure how you would want to merge it due to the need to re-run autoconf.

If you have docker on your mac, you can regenerate R-package/configure by following the steps at

docker run \
    -v $(pwd):/opt/LightGBM \
    -t ubuntu:20.04 \
    /bin/bash -c "cd /opt/LightGBM && ./R-package/"

Otherwise, if you open a pull request that just updates, I can regenerate configure and push to the branch on your fork.

Answered Mar 28 '21 at 19:51
avatar  of jameslamb

By the way, if you're feeling very generous with your time, I think our friends at {xgboost} might benefit from a similar patch:

Answered Mar 28 '21 at 19:56
avatar  of jameslamb

To provide a bit more context here, this is actually an M1 Mac issue, or more specifically, a non-standard dyld search path issue due to the suggested install location moving to /opt/homebrew for native homebrew on M1 Macs. This is not an issue for intel Macs, as homebrew is installed in /usr/local/Hombrew and symlinks shared libraries to /usr/local/lib, as I discovered when I tested this issue on my Mac Pro running Catalina and my MBP running Big Sur.

The bottom line here is that /opt/homebrew/lib (as suggested on the homebrew website) is not searched for when linking against dyld, but /usr/local/lib is (the old default on intel Macs). This would also be the case for anyone using non-standard locations for libomp.

I'll make the suggestion over at xgboost to consider adding the change as this will be a bigger issue as more people use native R on m1 and later designs.


search directories (also man dyld will provide info)

Homebrew 3.0.0 suggested Directories

Answered Mar 28 '21 at 23:31
avatar  of jlisic

A little bit offtopic: we have never tried to build LightGBM on macOS with LLVM's Clang, only with default AppleClang. Good to know that it compiles fine!

Answered Mar 29 '21 at 01:07
avatar  of StrikerRUS

@jlisic are you interested in creating a pull request? If not I'll pick this up.

Answered Apr 18 '21 at 20:02
avatar  of jameslamb

Assigning this to myself and I will pick it up soon, to get this fix into the next release (#4310).

I'd also like to include here two possibly-relevant pieces of information that were recently posted on the R-pkg-devel mailing like.

  1. There is very detailed documentation available at on CRAN's recommendations for using OpenMP in R packages.
  2. @dakep shared a link to this that does even more detailed OpenMP detection (checking for specific features that are only available in newer versions):
Answered Jun 25 '21 at 05:54
avatar  of jameslamb

I am going to pick this up tonight, to get this fix in for the next release (#4310).

Answered Aug 09 '21 at 02:49
avatar  of jameslamb