【问题标题】:"C compiler cannot create executables" when installing stringi in R在 R 中安装 stringi 时“C 编译器无法创建可执行文件”
【发布时间】:2021-12-06 17:46:42
【问题描述】:

我经常从源代码安装 R 包,并且需要正确配置的 ~/.R/Makevars 来执行此操作。我希望能够使用OpenMP,所以我复制了一个我在网上找到的Makevars。我的Makevars 最终变成了这样:

OPT_LOC = $(HOME)/homebrew/opt
LLVM_LOC = $(OPT_LOC)/llvm
CC=$(LLVM_LOC)/bin/clang
CXX=$(LLVM_LOC)/bin/clang++

# CFLAGS and CXXFLAGS *with* -fopenmp
CFLAGS=-fopenmp -g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
CXXFLAGS=-fopenmp -g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe

# CFLAGS and CXXFLAGS *without* -fopenmp
# (sometimes the package install process fails if -fopenmp is present)
# CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
# CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe

LDFLAGS=-L$(OPT_LOC)/gettext/lib -L$(LLVM_LOC)/lib -Wl,-rpath,$(LLVM_LOC)/lib
CPPFLAGS=-I$(OPT_LOC)/gettext/include -I$(LLVM_LOC)/include

我找不到我的版本的原始来源,但它类似于this one

我的 Makevars 工作得很好,除了一些包,比如 stringi。这些包抱怨"C compiler cannot create executables"。我发现如果我从CFLAGSCXXFLAGS 中删除-fopenmp,这个错误通常会消失。如您所见,我已经设置了Makevars,因此我可以轻松地在使用-fopenmp 和不使用-fopenmp 的配置之间来回切换。

我的解决方法可以正常工作,就像上面链接的 Stack Overflow 帖子中“完全删除 Makevars”的解决方法一样。但是没有更好的方法吗?令人讨厌的是,由于这些依赖项讨厌 -fopenmp 标志,我无法期望软件包安装成功,而删除该标志意味着我可能会在某些软件包安装中错过 OpenMP。

【问题讨论】:

    标签: r clang openmp llvm stringi


    【解决方案1】:

    如果您使用 GCC 而不是 Clang,您应该能够在启用 OpenMP 的情况下安装 stringi。我测试了许多不同的解决方案,最终想出了these steps 以使用-fopenmp 标志(英特尔macOS v11(Big Sur))从源代码成功安装stringi:

    1. 重新安装 Xcode 命令行工具(不要相信软件更新,如果它说“最新” - 它是谎言 - brew doctor 说我的版本实际上是旧的)

      sudo rm -rf /Library/Developer/CommandLineTools
      sudo xcode-select --install
      
    2. 通过 Homebrew (instructions for installing Homebrew) 安装 GCC 和 LLVM,或者,如果您已经安装了 GCC/LLVM,请跳到下一步。

      # WARNING: This can take several hours
      brew install gcc
      brew install llvm
      
    3. 如果您已经通过 Homebrew 安装了 GCC 和 LLVM:

      brew cleanup
      brew update
      brew upgrade
      brew reinstall gcc
      brew reinstall llvm
      
    4. 将一些标题链接到 /usr/local/include

      sudo ln -s /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/* /usr/local/include/
      
      # I believe you can safely ignore warnings like this:
      #ln: /usr/local/include//tcl.h: File exists
      #ln: /usr/local/include//tclDecls.h: File exists
      #ln: /usr/local/include//tclPlatDecls.h: File exists
      #ln: /usr/local/include//tclTomMath.h: File exists
      #ln: /usr/local/include//tclTomMathDecls.h: File exists
      #ln: /usr/local/include//tk.h: File exists
      #ln: /usr/local/include//tkDecls.h: File exists
      #ln: /usr/local/include//tkPlatDecls.h: File exists
      
    5. 编辑您的~/.R/Makevars 文件(如果您的~/.R/ 目录中没有名为Makevars 的文件,请创建它)并仅包含以下几行:

      LOC = /usr/local/gfortran
      CC=$(LOC)/bin/gcc -fopenmp
      CXX=$(LOC)/bin/g++ -fopenmp
      CXX11 = $(LOC)/bin/g++ -fopenmp
      
      CFLAGS=-g -O3 -Wall -pedantic -std=gnu99 -mtune=native -pipe
      CXXFLAGS=-g -O3 -Wall -pedantic -std=c++11 -mtune=native -pipe
      LDFLAGS=-L$(LOC)/lib -Wl,-rpath,$(LOC)/lib
      CPPFLAGS=-I$(LOC)/include -I/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include
      
      FLIBS=-L/usr/local/gfortran/lib/gcc/x86_64-apple-darwin19/10.2.0 -L/usr/local/gfortran/lib -lgfortran -lquadmath -lm
      CXX1X=/usr/local/gfortran/bin/g++
      CXX98=/usr/local/gfortran/bin/g++
      CXX11=/usr/local/gfortran/bin/g++
      CXX14=/usr/local/gfortran/bin/g++
      CXX17=/usr/local/gfortran/bin/g++
      
    6. 在 R/RStudio 中从源代码编译包

      # Test if OpenMP is actually enabled
      install.packages("data.table", type = "source")
      # (loading data.table will tell you if it was successful)
      
      # Compile the stringi package from source
      install.packages("stringi", type = "source")
      

    注意:一些用户评论说他们必须安装一个“新的”GFortran(例如,GFortran 10.2 for Big Sur (macOS 11), for Intel processors)才能成功编译包,而其他用户则评论说他们不需要LLVM,但这些步骤似乎在大多数情况下都有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-16
      • 2013-09-23
      • 1970-01-01
      相关资源
      最近更新 更多