在R.3.5.1中更新Rgdal,需要C++11依赖...尽管C++11是可用的。

9

当我尝试从源码编译更新rgdal软件包时,升级了R版本从3.4.4到3.5.1,我遇到了一个奇怪问题,所有步骤都进行得很好,但由于“未定义的符号”错误,命名空间加载失败:

** 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

当我运行echo _ZNK10OGRFeature16GetFieldAsDoubleEi | c++filt时,输出结果为OGRFeature::GetFieldAsDouble(int) const,这并没有告诉我太多信息。我按照CRAN包网站上的建议从https://trac.osgeo.org/gdal/wiki/DownloadSource安装了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)
2个回答

5
在我的情况下,解决方案是更新我的~/.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

在此之后,编译问题和相关错误不再出现(至少在我的测试中如此)。
推断:
上述引发了一个问题 - 为什么?新增的R 3.5.1与rgdal安装在我的实验中并未出现编译错误,而升级自3.4.4则有。我认为,R和rgdal软件包配置之间存在相互作用,导致C++11编译标志未被激活。rgdal需要使用C++11特性。请参见Makevar - 概述。
引文: 编译示例:
没有-std=gnu++11标志的Makevars[错误]
> 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中列出的任何软件包都将自动包含-您无需显式添加。

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


3
谢谢你提供的详尽答案。最终结果是,在我的情况下,我安装的gdal库手动和自动冲突了(http://r-sig-geo.2731867.n2.nabble.com/Problems-with-installing-rgdal-on-Ubuntu-quot-checking-gdal-linking-with-libs-only-no-quot-td7590255.html)。按照该帖子上的建议,我成功地在我的计算机上安装了rgdal。 - FM Kerckhof
@FMKerckhof 很好 - 很高兴你解决了问题。 - Technophobe01
1
@FMKerckhof 我想知道是否有一种方法可以检测多个gDal安装,以便在配置脚本中进行提示? - Technophobe01

3
我为自己找到了以下步骤(适用于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")即可。
相关参考资料如下:
在Ubuntu 16.04上安装Python GDAL 2.1
错误:gdal-config未找到

感谢@RolandASc,但我已经安装了GDAL 2.3.1和proj.4。这也在安装rgdal时列出:checking GDAL version >= 1.11.4... yes。我已经添加了ubuntugis ppa (http://ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu xenial InRelease)。 - FM Kerckhof
好的,使用我的依赖项安装方法后,我得到了checking gdal: linking with --libs only... **yes**的结果,这似乎更直接,但可能不适合你。 - RolandASc
我在 Mac OS X 上遇到了同样的问题。 - Technophobe01

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接