Paul
Paul

Reputation: 3361

ld: unknown option: -platform_version when building R packages from source

Certain R packages, such as mgcv, fail to compile from source with clang 10+ (under macOS 10.14 and R version 3.6+). The error reported during compilation is

ld: unknown option: -platform_version

How do I resolve this error and compile these packages?

Upvotes: 3

Views: 2514

Answers (1)

Paul
Paul

Reputation: 3361

The problem with ld is the same as in Clang 10 fails to link C++ application with CMake on macOS 10.12. However, the suggestion to add the flag -DCMAKE_CXX_FLAGS="-mlinker-version=305" is not applicable to the R package compilation process. For R, you need to add -mlinker-version=305 to LDFLAGS to your Makevars file, typically located in $HOME/.R/.

My Makevars is based on this GitHub gist. I changed LDFLAGS from this:

LDFLAGS+=-L$(HO)/llvm/lib -Wl,-rpath,$(HO)/llvm/lib

to this:

LDFLAGS+=-L$(HO)/llvm/lib -Wl,-rpath,$(HO)/llvm/lib -mlinker-version=305

That resolved the ld error when compiling mgcv from source.

For the igraph package, adding the mlinker flag to LDFLAGS was not enough; it had to be added to the C++ flags as well. In the gist Makevars above, this is done by adding -mlinker-version=305 to STD_FLAGS, which then adds the flag to CXX**FLAGS for all C++ versions.

UPDATE, June 24 2020: unfortunately, some packages (rJava in my case) fail to use the STD_FLAGS. My workaround was to put the mlinker flag in the C compiler invocation:

CC=$(CCACHE) $(HO)/llvm/bin/clang -mlinker-version=305

Upvotes: 2

Related Questions