Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

c++11

gdal

rgdal

When I am updating (or at least trying to) the rgdal package by compiling from source after updating R from 3.4.4 to 3.5.1, I run into the odd issue that all goes well, but the namespace load fails due to an "undefined symbol" error:

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

When running echo _ZNK10OGRFeature16GetFieldAsDoubleEi | c++filt I get as an output OGRFeature::GetFieldAsDouble(int) const which doesn't learn me much. I have installed GDAL from https://trac.osgeo.org/gdal/wiki/DownloadSource , as suggested on the CRAN package site which shows that I have GDAL 2.3.1 installed.

It is odd that updates fail, as the package was already installed, so dependencies should have been met. Any pointers as to how to resolve this issue are warmly welcome.

As I got no suggestions in the past few weeks I decided to uninstall rgdal, and reinstall it. Interestingly, now I get a claim that my system (Ubuntu 16.04.5 LTS) does not support C++11, although the check says it is there (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’

My devtools::session_info() is the following:

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)
like image 995
FM Kerckhof Avatar asked Jul 27 '18 23:07

FM Kerckhof


2 Answers

In my case, the solution was to update my ~/.R/Makevars config.

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

I added: -std=gnu++11 to my CXX definition

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

after this, the compile problems and related error no longer occur (at least in my testing so far)

Inference

The above leads to the question - Why? A new install of R 3.5.1 with rgdal does not (in my experience) exhibit the compile error, whilst the upgrade from 3.4.4 does. My sense is that an interaction exists between R and rgdal package config that causes the C++11 compile flag to be not activated. rgdal requires and uses C++11 features. See Makevar - an overview below.

Citations:

  • R Installation and Administration
  • R Using Makevars
  • How do I enable C++11 in gcc?

Compile Example:

No -std=gnu++11 flag in Makevars [Errors]

> 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

Added -std=gnu++11 to 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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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/[email protected]/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 - An Overview:

Makevars is a makefile that overrides the default makefile generated by R (which is located at file.path(R.home("etc"), "Makeconf")). This allows you to take advantage of R’s default behaviour while being able to set the flags you need. These are the most commonly used flags:

  • PKG_LIBS Linker flags. A common use is PKG_LIBS = $(BLAS_LIBS). This allows you to use the same BLAS library as R.

  • PKG_CFLAGS and PKG_CXXFLAGS C and C++ flags. Most commonly used to set define directives with -D.

PKG_CPPFLAGS Preprocessor flags (not C++ flags!). Most commonly used to set include directories with -I. Any package listed in the LinkingTo field in the DESCRIPTION will be automatically included—you do not need to explicitly add it.

I hope the above helps point you in the direction of a solution.

like image 148
Technophobe01 Avatar answered Sep 21 '22 15:09

Technophobe01


What worked for me is the following sequence (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

Afterwards, I could simply run install.packages("rgdal") under R 3.5.1.

Related references here:
python GDAL 2.1 installation on Ubuntu 16.04
Error: gdal-config not found

like image 35
RolandASc Avatar answered Sep 24 '22 15:09

RolandASc