【问题标题】:updating Rgdal in R.3.5.1 C++11 dependency... although C++11 is available更新 R.3.5.1 C++11 依赖项中的 Rgdal...虽然 C++11 可用
【发布时间】:2019-01-05 02:19:47
【问题描述】:

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

** 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 的输出,这对我了解不多。我已经从 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)

【问题讨论】:

    标签: r c++11 gdal rgdal


    【解决方案1】:

    就我而言,解决方案是更新我的 ~/.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 设置包含目录。描述中的 LinkingTo 字段中列出的任何包都将被自动包含 - 您无需显式添加它。

    我希望以上内容能帮助您指明解决方案的方向。

    【讨论】:

    • 感谢您非常彻底的回答。最后事实证明,就我而言,手动安装和自动安装的 gdal 库 (r-sig-geo.2731867.n2.nabble.com/…) 之间存在冲突。毕竟,按照那篇文章的建议,我能够在我的机器上安装 rgdal。
    • @FMKerckhof 太好了 - 很高兴您解决了问题。
    • @FMKerckhof 我想知道是否有一种方法可以检测多个 gDal 安装,可以内置到配置脚本中以提示问题?
    【解决方案2】:

    对我有用的是以下序列(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

    【讨论】:

    • 感谢@RolandASc,但我确实安装了 GDAL 2.3.1 和 proj.4。这也列在安装时 rgdal 的输出中:checking GDAL version &gt;= 1.11.4... yes。我确实已经添加了 ubuntugis ppa (`ppa.launchpad.net/ubuntugis/ubuntugis-unstable/ubuntu xenial InRelease`)。
    • 好的,通过我安装依赖项的方式,我得到了checking gdal: linking with --libs only... **yes**,这看起来更直接,但可能不是你的选择
    • 我在 Mac OS X 上遇到了同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-07
    • 2012-03-05
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 2021-04-21
    相关资源
    最近更新 更多