R安装xml遇到的问题 - installation of package 'xml2' had non-zero exit status

系统版本

macOS Mojave Version 10.14.2

R版本

R version 3.5.2 (2018-12-20) -- "Eggshell Igloo"
Copyright (C) 2018 The R Foundation for Statistical Computing
Platform: x86_64-apple-darwin18.2.0 (64-bit)

xml安装版本:

2_1.2.0

安装xml2包报错如下:

During startup - Warning messages:
1: Setting LC_TIME failed, using "C"
2: Setting LC_MESSAGES failed, using "C"
3: Setting LC_MONETARY failed, using "C"
* installing *source* package ‘xml2’ ...
** package ‘xml2’ successfully unpacked and MD5 sums checked
Found pkg-config cflags and libs!
Using PKG_CFLAGS=-I/usr/include/libxml2
Using PKG_LIBS=-L/usr/lib -lxml2 -lz -lpthread -licucore -lm
------------------------- ANTICONF ERROR ---------------------------
Configuration failed because libxml-2.0 was not found. Try installing:
 * deb: libxml2-dev (Debian, Ubuntu, etc)
 * rpm: libxml2-devel (Fedora, CentOS, RHEL)
 * csw: libxml2_dev (Solaris)
If libxml-2.0 is already installed, check that 'pkg-config' is in your
PATH and PKG_CONFIG_PATH contains a libxml-2.0.pc file. If pkg-config
is unavailable you can set INCLUDE_DIR and LIB_DIR manually via:
R CMD INSTALL --configure-vars='INCLUDE_DIR=... LIB_DIR=...'
--------------------------------------------------------------------
ERROR: configuration failed for package ‘xml2’
* removing ‘/usr/local/lib/R/3.5/site-library/xml2’

The downloaded source packages are in
    ‘/private/tmp/Rtmp9tYdXH/downloaded_packages’
Warning message:
In install.packages("xml2") :
  installation of package ‘xml2’ had non-zero exit status

在网上调研了半天得到的导致结论是pkg-config不能准确定位到libxml2的位置, 这样说不是很准确, 但不纠结, 下边我们来看如何解决.

解决办法

brew安装如下包
brew install pkg-config
brew install libxml2

注意, libxml2 需配置一下PATH

brew info libxml2
# ==============================================================
==> Caveats
libxml2 is keg-only, which means it was not symlinked into /usr/local,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.

If you need to have libxml2 first in your PATH run:
  echo 'export PATH="/usr/local/opt/libxml2/bin:$PATH"' >> ~/.zshrc

For compilers to find libxml2 you may need to set:
  export LDFLAGS="-L/usr/local/opt/libxml2/lib"
  export CPPFLAGS="-I/usr/local/opt/libxml2/include"

For pkg-config to find libxml2 you may need to set:
  export PKG_CONFIG_PATH="/usr/local/opt/libxml2/lib/pkgconfig"
下载xml2包, 解压

地址: https://mirrors.tongji.edu.cn/CRAN/src/contrib/xml2_1.2.0.tar.gz, 如果地址失效, 在R安装这个包 - install.packages('xml2'), 就可以看到可用地址了
解压之后在iterm2(终端)打开:

➜  ~ cd Downloads/xml2
➜  xml2 pwd
/Users/*/Downloads/xml2
➜  xml2 ll
total 80
-rw-r--r--@  1 *  staff   1.5K Jan 24  2018 DESCRIPTION
-rw-r--r--@  1 *  staff   6.5K Jan 24  2018 MD5
-rw-r--r--@  1 *  staff   5.9K Jan  5  2018 NAMESPACE
-rw-r--r--@  1 *  staff   5.4K Jan  6  2018 NEWS.md
drwxr-xr-x@ 25 *  staff   800B Jan 23  2018 R
-rw-r--r--@  1 *  staff   1.9K Nov 23  2017 README.md
drwxr-xr-x@  4 *  staff   128B Jan 23  2018 build
-rwxr-xr-x@  1 *  staff    29B Jan 23  2018 cleanup
-rwxr-xr-x@  1 *  staff   3.1K Jan 22 17:35 configure
-rw-r--r--@  1 *  staff     0B Nov  5  2015 configure.win
drwxr-xr-x@  5 *  staff   160B Jan 23  2018 inst
drwxr-xr-x@ 31 *  staff   992B Jan  5  2018 man
drwxr-xr-x@ 27 *  staff   864B Jan 22 17:35 src
drwxr-xr-x@  4 *  staff   128B Jan  5  2018 tests
drwxr-xr-x@  3 *  staff    96B Jun 13  2016 tools
drwxr-xr-x@  4 *  staff   128B Jan 23  2018 vignettes

注意, 其中configure文件需要我们修改

修改configure文件

xml2-config --version >/dev/null 2>&1
if [ $? -eq 0 ]; then
  PKGCONFIG_CFLAGS=`xml2-config --cflags`
  PKGCONFIG_LIBS=`xml2-config --libs`

  # MacOS versions El Capitan and later ship a xml2-config which appends `xcrun
  # --show-sdk-path` to the xml2-config. So we remove it if it is present.
  # (https://stat.ethz.ch/pipermail/r-sig-mac/2016-September/012046.html)
  if echo $OSTYPE | grep -q '^darwin'; then
    PKGCONFIG_CFLAGS=`echo $PKGCONFIG_CFLAGS | perl -pe "s{\Q\`xcrun -show-sdk-path\`\E}{}"`
    PKGCONFIG_LIBS=`echo $PKGCONFIG_LIBS | perl -pe "s{\Q\`xcrun -show-sdk-path\`\E}{}"`
  fi
else
  pkg-config --version >/dev/null 2>&1
  if [ $? -eq 0 ]; then
    PKGCONFIG_CFLAGS=`pkg-config --cflags $PKG_CONFIG_NAME`
    PKGCONFIG_LIBS=`pkg-config --libs $PKG_CONFIG_NAME`
  fi
fi

修改为

if [ $(command -v pkg-config) ]; then
  PKGCONFIG_CFLAGS=$(pkg-config --cflags $PKG_CONFIG_NAME)
  PKGCONFIG_LIBS=$(pkg-config --libs $PKG_CONFIG_NAME)
fi

或者

# 添加制定路径 - libxml2 文件夹的路径
PKG_CFLAGS='-I/usr/local/Cellar/libxml2/2.9.9_2/include/libxml2'
PKG_LIBS='-L/usr/local/Cellar/libxml2/2.9.9_2/lib -lxml2'
# #####
# Find compiler
CC=`${R_HOME}/bin/R CMD config CC`
CFLAGS=`${R_HOME}/bin/R CMD config CFLAGS`
CPPFLAGS=`${R_HOME}/bin/R CMD config CPPFLAGS`
iterm2安装xml2
# 必须在xml2文件夹下
R CMD INSTALL .

执行信息

* installing to library ‘/usr/local/lib/R/3.5/site-library’
* installing *source* package ‘xml2’ ...
file ‘configure’ has the wrong MD5 checksum
Found pkg-config cflags and libs!
Using PKG_CFLAGS=-I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2
Using PKG_LIBS=-L/usr/local/Cellar/libxml2/2.9.8_1/lib -lxml2
** libs
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c RcppExports.cpp -o RcppExports.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c connection.cpp -o connection.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_doc.cpp -o xml2_doc.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_init.cpp -o xml2_init.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_namespace.cpp -o xml2_namespace.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_node.cpp -o xml2_node.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_output.cpp -o xml2_output.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_schema.cpp -o xml2_schema.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_url.cpp -o xml2_url.o
clang++  -I"/usr/local/Cellar/r/3.5.2_2/lib/R/include" -DNDEBUG -I../inst/include -I/usr/local/Cellar/libxml2/2.9.8_1/include/libxml2 -I"/usr/local/lib/R/3.5/site-library/Rcpp/include" -I/usr/local/opt/gettext/include -I/usr/local/opt/readline/include -I/usr/local/include   -fPIC  -g -O2  -c xml2_xpath.cpp -o xml2_xpath.o
clang++ -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/opt/gettext/lib -L/usr/local/opt/readline/lib -L/usr/local/lib -L/usr/local/Cellar/r/3.5.2_2/lib/R/lib -L/usr/local/opt/gettext/lib -L/usr/local/opt/readline/lib -L/usr/local/lib -o xml2.so RcppExports.o connection.o xml2_doc.o xml2_init.o xml2_namespace.o xml2_node.o xml2_output.o xml2_schema.o xml2_url.o xml2_xpath.o -L/usr/local/Cellar/libxml2/2.9.8_1/lib -lxml2 -L/usr/local/Cellar/r/3.5.2_2/lib/R/lib -lR -lintl -Wl,-framework -Wl,CoreFoundation
installing to /usr/local/lib/R/3.5/site-library/xml2/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (xml2)
在R运行
R安装xml遇到的问题 - installation of package 'xml2' had non-zero exit status_第1张图片
R运行结果

大家若有疑问可以联系我.

参考文章: https://www.perfectlyrandom.org/2015/12/13/install-xml2-r-package-on-macos/

你可能感兴趣的:(R安装xml遇到的问题 - installation of package 'xml2' had non-zero exit status)