在 R.3.5.1 C++11 依赖项中更新 Rgdal... 尽管 C++11 可用

updating Rgdal in R.3.5.1 C++11 dependency... although C++11 is available

当我在将 R 从 3.4.4 更新到 3.5.1 后通过从源代码编译来更新(或至少尝试更新)rgdal 包时,我 运行 遇到了一个奇怪的问题,一切顺利,但由于 "undefined symbol" 错误,命名空间加载失败:

** installing vignettes
** testing if installed package can be loaded
Error: package or namespace load failed for ‘rgdal’ in dyn.load(file, DLLpath = DLLpath, ...):
 unable to load shared object '/usr/local/lib/R/site-library/rgdal/libs/rgdal.so':
  /usr/local/lib/R/site-library/rgdal/libs/rgdal.so: undefined symbol: _ZNK10OGRFeature16GetFieldAsDoubleEi
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/usr/local/lib/R/site-library/rgdal’
* restoring previous ‘/usr/local/lib/R/site-library/rgdal’

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

当 运行ning echo _ZNK10OGRFeature16GetFieldAsDoubleEi | c++filt 时,我得到的输出是 OGRFeature::GetFieldAsDouble(int) const,这并没有让我学到太多东西。我已经从 https://trac.osgeo.org/gdal/wiki/DownloadSource 安装了 GDAL,正如 CRAN 软件包网站上所建议的那样,它表明我已经安装了 GDAL 2.3.1。

奇怪的是更新失败了,因为包已经安装了,所以依赖关系应该已经满足。热烈欢迎任何有关如何解决此问题的指示。

由于过去几周没有收到任何建议,我决定卸载 rgdal,然后重新安装。有趣的是,现在有人声称我的系统 (Ubuntu 16.04.5 LTS) 不支持 C++11,尽管支票上说它在那里 (C++11 support available):

* installing *source* package ‘rgdal’ ...
** package ‘rgdal’ successfully unpacked and MD5 sums checked
configure: R_HOME: /usr/lib/R
configure: CC: gcc -std=gnu99
configure: CXX: g++
configure: C++11 support available
configure: rgdal: 1.3-4
checking for /usr/bin/svnversion... yes
configure: svn revision: 766
checking for gdal-config... /usr/local/bin/gdal-config
checking gdal-config usability... yes
configure: GDAL: 2.3.1
checking C++11 support for GDAL >= 2.3.0... yes
checking GDAL version >= 1.11.4... yes
checking gdal: linking with --libs only... no
checking gdal: linking with --libs and --dep-libs... no
In file included from /usr/local/include/gdal.h:45:0,
                 from gdal_test.cc:1:
/usr/local/include/cpl_port.h:187:6: error: #error Must have C++11 or newer.
 #    error Must have C++11 or newer.
      ^
In file included from /usr/local/include/gdal.h:49:0,
                 from gdal_test.cc:1:
/usr/local/include/cpl_minixml.h:202:47: error: expected template-name before '<' token
 class CPLXMLTreeCloser: public std::unique_ptr<CPLXMLNode, CPLXMLTreeCloserDeleter>
                                               ^
/usr/local/include/cpl_minixml.h:202:47: error: expected '{' before '<' token
/usr/local/include/cpl_minixml.h:202:47: error: expected unqualified-id before '<' token
In file included from /usr/local/include/ogr_api.h:45:0,
                 from /usr/local/include/gdal.h:50,
                 from gdal_test.cc:1:
/usr/local/include/ogr_core.h:79:28: error: expected '}' before end of line
/usr/local/include/ogr_core.h:79:28: error: expected declaration before end of line
In file included from /usr/local/include/gdal.h:45:0,
                 from gdal_test.cc:1:
/usr/local/include/cpl_port.h:187:6: error: #error Must have C++11 or newer.
 #    error Must have C++11 or newer.
      ^
In file included from /usr/local/include/gdal.h:49:0,
                 from gdal_test.cc:1:
/usr/local/include/cpl_minixml.h:202:47: error: expected template-name before '<' token
 class CPLXMLTreeCloser: public std::unique_ptr<CPLXMLNode, CPLXMLTreeCloserDeleter>
                                               ^
/usr/local/include/cpl_minixml.h:202:47: error: expected '{' before '<' token
/usr/local/include/cpl_minixml.h:202:47: error: expected unqualified-id before '<' token
In file included from /usr/local/include/ogr_api.h:45:0,
                 from /usr/local/include/gdal.h:50,
                 from gdal_test.cc:1:
/usr/local/include/ogr_core.h:79:28: error: expected '}' before end of line
/usr/local/include/ogr_core.h:79:28: error: expected declaration before end of line
configure: Install failure: compilation and/or linkage problems.
configure: error: GDALAllRegister not found in libgdal.
ERROR: configuration failed for package ‘rgdal’
* removing ‘/usr/local/lib/R/site-library/rgdal’

我的devtools::session_info()如下:

Session info ------------------------------------------------------------------
 setting  value
 version  R version 3.5.1 (2018-07-02)
 system   x86_64, linux-gnu
 ui       X11
 language en_US:en
 collate  en_US.UTF-8
 tz       Europe/Brussels
 date     2018-07-28

Packages ----------------------------------------------------------------------
 package   * version date       source
 base      * 3.5.1   2018-07-03 local
 compiler    3.5.1   2018-07-03 local
 datasets  * 3.5.1   2018-07-03 local
 devtools  * 1.13.6  2018-06-27 CRAN (R 3.5.1)
 digest      0.6.15  2018-01-28 CRAN (R 3.5.1)
 graphics  * 3.5.1   2018-07-03 local
 grDevices * 3.5.1   2018-07-03 local
 memoise     1.1.0   2017-04-21 CRAN (R 3.5.1)
 methods   * 3.5.1   2018-07-03 local
 stats     * 3.5.1   2018-07-03 local
 tools       3.5.1   2018-07-03 local
 utils     * 3.5.1   2018-07-03 local
 withr       2.1.2   2018-03-15 CRAN (R 3.5.1)

对我有用的是以下序列(Ubuntu 16.04.5 LTS):

sudo add-apt-repository -y ppa:ubuntugis/ppa
sudo apt update
# I didn't have gdal previously, so I cannot just upgrade below.
# Also note that I'm using the python library as well, which you may not want.
sudo apt install gdal-bin python-gdal python3-gdal

# the above also covered **PROJ.4**, therefore below was not needed
# sudo apt install proj-bin

sudo apt install libgdal1-dev
sudo apt install libgdal1i

# gdalinfo --version
> GDAL 2.1.3, released 2017/20/01

之后,我可以在 R 3.5.1.

下简单地 运行 install.packages("rgdal")

相关参考:
python GDAL 2.1 installation on Ubuntu 16.04
Error: gdal-config not found

就我而言,解决方案是更新我的 ~/.R/Makevars 配置。

  CC=clang
  CXX=clang++
  PKG_CFLAGS= -g -O2

我在我的 CXX 定义中添加了:-std=gnu++11

  CC=clang
  CXX=clang++ -std=gnu++11
  PKG_CFLAGS= -g -O2

此后,编译问题和相关错误不再出现(至少在我目前的测试中)

推理

以上引出了问题 - 为什么?新安装的带有 rgdal 的 R 3.5.1(根据我的经验)不会出现编译错误,而从 3.4.4 升级会出现。我的感觉是 R 和 rgdal 包配置之间存在交互,导致 C++11 编译标志未激活。 rgdal 需要并使用 C++11 特性。请参阅 Makevar - 下面的概述。

引用次数:

编译示例:

Makevars 中没有 -std=gnu++11 标志 [错误]

> install.packages("rgdal")
--2018-08-10 21:31:58--  https://cran.rstudio.com/src/contrib/rgdal_1.3-4.tar.gz
Resolving cran.rstudio.com (cran.rstudio.com)... 52.84.17.191
Connecting to cran.rstudio.com (cran.rstudio.com)|52.84.17.191|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1664774 (1.6M) [application/x-gzip]
Saving to: ‘/var/folders/md/03gdc4c14z18kbqwpfh4jdfc0000gr/T//RtmpidZNr4/downloaded_packages/rgdal_1.3-4.tar.gz’

/var/folders/md/03gdc4c14z18kbqwpfh4jd 100%[============================================================================>]   1.59M  --.-KB/s    in 0.04s

2018-08-10 21:31:58 (35.6 MB/s) - ‘/var/folders/md/03gdc4c14z18kbqwpfh4jdfc0000gr/T//RtmpidZNr4/downloaded_packages/rgdal_1.3-4.tar.gz’ saved [1664774/1664774]

* installing *source* package ‘rgdal’ ...
** package ‘rgdal’ successfully unpacked and MD5 sums checked
configure: R_HOME: /Users/***/.Renv/versions/3.5.1/lib/R
configure: CC: clang
configure: CXX: clang++
configure: C++11 support available
configure: rgdal: 1.3-4
checking for /usr/bin/svnversion... yes
configure: svn revision: 766
checking for gdal-config... /usr/local/opt/gdal2/bin/gdal-config
checking gdal-config usability... yes
configure: GDAL: 2.3.1
checking C++11 support for GDAL >= 2.3.0... yes
checking GDAL version >= 1.11.4... yes
checking gdal: linking with --libs only... no
checking gdal: linking with --libs and --dep-libs... no
In file included from gdal_test.cc:1:
In file included from /usr/local/Cellar/gdal2/2.3.1/include/gdal.h:45:
/usr/local/Cellar/gdal2/2.3.1/include/cpl_port.h:187:6: error: Must have C++11 or newer.
#    error Must have C++11 or newer.
     ^
1 error generated.
In file included from gdal_test.cc:1:
In file included from /usr/local/Cellar/gdal2/2.3.1/include/gdal.h:45:
/usr/local/Cellar/gdal2/2.3.1/include/cpl_port.h:187:6: error: Must have C++11 or newer.
#    error Must have C++11 or newer.
     ^
1 error generated.
configure: Install failure: compilation and/or linkage problems.
configure: error: GDALAllRegister not found in libgdal.
ERROR: configuration failed for package ‘rgdal’
* removing ‘/Users/***/.Renv/versions/3.5.1/lib/R/library/rgdal’

The downloaded source packages are in
    ‘/private/var/folders/md/03gdc4c14z18kbqwpfh4jdfc0000gr/T/RtmpidZNr4/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done
Warning message:
In install.packages("rgdal") :
  installation of package ‘rgdal’ had non-zero exit status

已将 -std=gnu++11 添加到 Makevars

> install.packages("rgdal")
--2018-08-10 21:34:20--  https://cran.rstudio.com/src/contrib/rgdal_1.3-4.tar.gz
Resolving cran.rstudio.com (cran.rstudio.com)... 52.84.17.191
Connecting to cran.rstudio.com (cran.rstudio.com)|52.84.17.191|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 1664774 (1.6M) [application/x-gzip]
Saving to: ‘/var/folders/md/03gdc4c14z18kbqwpfh4jdfc0000gr/T//RtmpidZNr4/downloaded_packages/rgdal_1.3-4.tar.gz’

/var/folders/md/03gdc4c14z18kbqwpfh4jd 100%[============================================================================>]   1.59M  --.-KB/s    in 0.04s

2018-08-10 21:34:20 (43.5 MB/s) - ‘/var/folders/md/03gdc4c14z18kbqwpfh4jdfc0000gr/T//RtmpidZNr4/downloaded_packages/rgdal_1.3-4.tar.gz’ saved [1664774/1664774]

* installing *source* package ‘rgdal’ ...
** package ‘rgdal’ successfully unpacked and MD5 sums checked
configure: R_HOME: /Users/***/.Renv/versions/3.5.1/lib/R
configure: CC: clang
configure: CXX: clang++ -std=gnu++11
configure: C++11 support available
configure: rgdal: 1.3-4
checking for /usr/bin/svnversion... yes
configure: svn revision: 766
checking for gdal-config... /usr/local/opt/gdal2/bin/gdal-config
checking gdal-config usability... yes
configure: GDAL: 2.3.1
checking C++11 support for GDAL >= 2.3.0... yes
checking GDAL version >= 1.11.4... yes
checking gdal: linking with --libs only... yes
checking GDAL: /usr/local/Cellar/gdal2/2.3.1/share/gdal/pcs.csv readable... yes
configure: pkg-config proj exists, will use it
configure: PROJ version: 5.1.0
checking proj_api.h presence and usability... yes
checking PROJ version >= 4.8.0... yes
checking projects.h presence and usability... yes
checking PROJ.4: epsg found and readable... yes
checking PROJ.4: conus found and readable... yes
configure: Package CPP flags:  -I/usr/local/Cellar/gdal2/2.3.1/include -I/usr/local/Cellar/proj/5.1.0/include
configure: Package LIBS:  -L/usr/local/Cellar/gdal2/2.3.1/lib -lgdal -lproj
configure: creating ./config.status
config.status: creating src/Makevars
** libs
g++ -std=gnu++11 -I"/Users/***/.Renv/versions/3.5.1/lib/R/include" -DNDEBUG -I/usr/local/Cellar/gdal2/2.3.1/include -I/usr/local/Cellar/proj/5.1.0/include -I"/Users/***/.Renv/versions/3.5.1/lib/R/library/sp/include" -I/usr/local/opt/gdal2/include -I/usr/local/opt/liblas-gdal2/include -I/usr/local/opt/laszip@2.2/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/zlib/include  -g -O2 -stdlib=libc++ -std=c++11 -fPIC  -g -O2 -c OGR_write.cpp -o OGR_write.o
g++ -std=gnu++11 -I"/Users/***/.Renv/versions/3.5.1/lib/R/include" -DNDEBUG -I/usr/local/Cellar/gdal2/2.3.1/include -I/usr/local/Cellar/proj/5.1.0/include -I"/Users/***/.Renv/versions/3.5.1/lib/R/library/sp/include" -I/usr/local/opt/gdal2/include -I/usr/local/opt/liblas-gdal2/include -I/usr/local/opt/laszip@2.2/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/zlib/include  -g -O2 -stdlib=libc++ -std=c++11 -fPIC  -g -O2 -c gdal-bindings.cpp -o gdal-bindings.o
clang -I"/Users/***/.Renv/versions/3.5.1/lib/R/include" -DNDEBUG -I/usr/local/Cellar/gdal2/2.3.1/include -I/usr/local/Cellar/proj/5.1.0/include -I"/Users/***/.Renv/versions/3.5.1/lib/R/library/sp/include" -I/usr/local/opt/gdal2/include -I/usr/local/opt/liblas-gdal2/include -I/usr/local/opt/laszip@2.2/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/zlib/include  -g -O2 -fPIC  -g -O2  -c init.c -o init.o
clang -I"/Users/***/.Renv/versions/3.5.1/lib/R/include" -DNDEBUG -I/usr/local/Cellar/gdal2/2.3.1/include -I/usr/local/Cellar/proj/5.1.0/include -I"/Users/***/.Renv/versions/3.5.1/lib/R/library/sp/include" -I/usr/local/opt/gdal2/include -I/usr/local/opt/liblas-gdal2/include -I/usr/local/opt/laszip@2.2/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/zlib/include  -g -O2 -fPIC  -g -O2  -c inverser.c -o inverser.o
clang -I"/Users/***/.Renv/versions/3.5.1/lib/R/include" -DNDEBUG -I/usr/local/Cellar/gdal2/2.3.1/include -I/usr/local/Cellar/proj/5.1.0/include -I"/Users/***/.Renv/versions/3.5.1/lib/R/library/sp/include" -I/usr/local/opt/gdal2/include -I/usr/local/opt/liblas-gdal2/include -I/usr/local/opt/laszip@2.2/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/zlib/include  -g -O2 -fPIC  -g -O2  -c local_stubs.c -o local_stubs.o
g++ -std=gnu++11 -I"/Users/***/.Renv/versions/3.5.1/lib/R/include" -DNDEBUG -I/usr/local/Cellar/gdal2/2.3.1/include -I/usr/local/Cellar/proj/5.1.0/include -I"/Users/***/.Renv/versions/3.5.1/lib/R/library/sp/include" -I/usr/local/opt/gdal2/include -I/usr/local/opt/liblas-gdal2/include -I/usr/local/opt/laszip@2.2/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/zlib/include  -g -O2 -stdlib=libc++ -std=c++11 -fPIC  -g -O2 -c ogr_geom.cpp -o ogr_geom.o
clang -I"/Users/***/.Renv/versions/3.5.1/lib/R/include" -DNDEBUG -I/usr/local/Cellar/gdal2/2.3.1/include -I/usr/local/Cellar/proj/5.1.0/include -I"/Users/***/.Renv/versions/3.5.1/lib/R/library/sp/include" -I/usr/local/opt/gdal2/include -I/usr/local/opt/liblas-gdal2/include -I/usr/local/opt/laszip@2.2/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/zlib/include  -g -O2 -fPIC  -g -O2  -c ogr_polygons.c -o ogr_polygons.o
g++ -std=gnu++11 -I"/Users/***/.Renv/versions/3.5.1/lib/R/include" -DNDEBUG -I/usr/local/Cellar/gdal2/2.3.1/include -I/usr/local/Cellar/proj/5.1.0/include -I"/Users/***/.Renv/versions/3.5.1/lib/R/library/sp/include" -I/usr/local/opt/gdal2/include -I/usr/local/opt/liblas-gdal2/include -I/usr/local/opt/laszip@2.2/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/zlib/include  -g -O2 -stdlib=libc++ -std=c++11 -fPIC  -g -O2 -c ogr_proj.cpp -o ogr_proj.o
g++ -std=gnu++11 -I"/Users/***/.Renv/versions/3.5.1/lib/R/include" -DNDEBUG -I/usr/local/Cellar/gdal2/2.3.1/include -I/usr/local/Cellar/proj/5.1.0/include -I"/Users/***/.Renv/versions/3.5.1/lib/R/library/sp/include" -I/usr/local/opt/gdal2/include -I/usr/local/opt/liblas-gdal2/include -I/usr/local/opt/laszip@2.2/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/zlib/include  -g -O2 -stdlib=libc++ -std=c++11 -fPIC  -g -O2 -c ogrdrivers.cpp -o ogrdrivers.o
g++ -std=gnu++11 -I"/Users/***/.Renv/versions/3.5.1/lib/R/include" -DNDEBUG -I/usr/local/Cellar/gdal2/2.3.1/include -I/usr/local/Cellar/proj/5.1.0/include -I"/Users/***/.Renv/versions/3.5.1/lib/R/library/sp/include" -I/usr/local/opt/gdal2/include -I/usr/local/opt/liblas-gdal2/include -I/usr/local/opt/laszip@2.2/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/zlib/include  -g -O2 -stdlib=libc++ -std=c++11 -fPIC  -g -O2 -c ogrsource.cpp -o ogrsource.o
g++ -std=gnu++11 -I"/Users/***/.Renv/versions/3.5.1/lib/R/include" -DNDEBUG -I/usr/local/Cellar/gdal2/2.3.1/include -I/usr/local/Cellar/proj/5.1.0/include -I"/Users/***/.Renv/versions/3.5.1/lib/R/library/sp/include" -I/usr/local/opt/gdal2/include -I/usr/local/opt/liblas-gdal2/include -I/usr/local/opt/laszip@2.2/include -I/usr/local/opt/bzip2/include -I/usr/local/opt/zlib/include  -g -O2 -stdlib=libc++ -std=c++11 -fPIC  -g -O2 -c projectit.cpp -o projectit.o
g++ -std=gnu++11 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/Users/***/.Renv/versions/3.5.1/lib/R/lib -L/usr/local/opt/gdal2/lib -L/usr/local/opt/liblas-gdal2/lib -L/usr/local/opt/laszip@2.2/lib -L/usr/local/opt/bzip2/lib -L/usr/local/opt/zlib/lib -o rgdal.so OGR_write.o gdal-bindings.o init.o inverser.o local_stubs.o ogr_geom.o ogr_polygons.o ogr_proj.o ogrdrivers.o ogrsource.o projectit.o -L/usr/local/Cellar/gdal2/2.3.1/lib -lgdal -lproj -L/Users/***/.Renv/versions/3.5.1/lib/R/lib -lR -Wl,-framework -Wl,CoreFoundation
installing to /Users/***/.Renv/versions/3.5.1/lib/R/library/rgdal/libs
** R
** data
** 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 (rgdal)

The downloaded source packages are in
    ‘/private/var/folders/md/03gdc4c14z18kbqwpfh4jdfc0000gr/T/RtmpidZNr4/downloaded_packages’
Updating HTML index of packages in '.Library'
Making 'packages.html' ... done

MakeVars - 概述:

Makevars 是一个 makefile,它覆盖了 R 生成的默认 makefile(位于 file.path(R.home("etc"), "Makeconf"))。这允许您利用 R 的默认行为,同时能够设置您需要的标志。这些是最常用的标志:

  • PKG_LIBS 链接器标志。一个常见的用法是 PKG_LIBS = $(BLAS_LIBS)。这允许您使用与 R.

  • 相同的 BLAS 库
  • PKG_CFLAGSPKG_CXXFLAGS C 和 C++ 标志。最常用于使用 -D.

  • 设置定义指令

PKG_CPPFLAGS 预处理器标志(不是 C++ 标志!)。最常用于使用 -I 设置包含目录。 DESCRIPTION 的 LinkingTo 字段中列出的任何包都将自动包含 - 您无需显式添加它。

希望以上内容能帮助您找到解决方案。