Building R package and error "ld: cannot find -lgfortran"

roxygen2
r package documentation
r cmd check
r packages
r package structure
r package description
build r package from source
r install package

I'm trying to install the package lars. Ubuntu 11.04 Natty 64-bit. From building I get:

* installing *source* package âlarsâ ...
** libs
gfortran   -fpic  -O3 -pipe  -g -c delcol.f -o delcol.o
gcc -shared -o lars.so delcol.o -lgfortran -lm -L/usr/lib64/R/lib -lR
/usr/bin/ld: cannot find -lgfortran
collect2: ld returned 1 exit status
make: *** [lars.so] Error 1
ERROR: compilation failed for package âlarsâ

gfortran is installed and when I run gfortran --version I get

gfortran --version GNU Fortran (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2

sudo ldconfig -v gives the error

/sbin/ldconfig.real: Cannot stat /usr/lib/libgfortran.so: No such file or directory

I have already removed and reinstalled gfortran. What do I need to fix this?

For the Debian / Ubuntu family, we usually recommend

 $ sudo apt-get install r-base-dev

as it pulls in all packages commonly needed for compiling. And this approach gets tested all the time as the automated package builders rely on this (as well as additional per-package Build-Depends). The gfortran package is listed here too; maybe you have a broken link from a prior installation so I'd also try dpkg --purge gfortran; apt-get install gfortran. That said, dozens of R packages (and R itself) use Fortran so there should not be any magic here.

Checking a package · R packages, These are usually removed automatically by R CMD build so you shouldn't see this error. If you do, just delete the directory. The case of files and directories is  Replace /opt/R/R-3.4.1 with the path to your R installation. Use the path to the R installation directory, not the path to the binary (do not include /bin/R ). While multiple versions of R may be installed on the server, only one version of R may be specified for use by RStudio Package Manager.

I had the same problem when trying to install the CRAN package VGAM on Ubuntu 12.10 64bit. I already had r-base-dev installed, but Andrew Redd's second comment to Dirk Eddelbuettel's answer worked for me.

Specifically, I was getting two errors:

/usr/bin/ld: cannot find -lgfortran
/usr/bin/ld: cannot find -lquadmath

Which were fixed by the lines:

sudo ln -s /usr/lib/x86_64-linux-gnu/libgfortran.so.3 /usr/lib/libgfortran.so
sudo ln -s /usr/lib/x86_64-linux-gnu/libquadmath.so.0 /usr/lib/libquadmath.so

Note that only the first line would be necessary to take care of the problem from the original post. The second line fixed of my additional error with lquadmath.

Package structure · R packages, In the rare case that you do need a bundle, call devtools::build() to make it. While library() throws an error, require() prints a warning message and returns  Building R Package Error: Objects listed as exports, but not present in namespace

It looks like other suggestions already fixed your problem, but your question also applied to me but the solution was different in my case. My problem was that my gcc and g++ versions differed from my gfortran version. I used the following to switch them so that they were all the same.

  1. Check what version of gcc, g++, and gfortran you have:

    g++ --version
    gcc --version
    gfortran --version
    
  2. Match them so that they are all the same:

    sudo update-alternatives --config g++
    sudo update-alternatives --config gcc
    sudo update-alternatives --config gfortran
    

In my case, I only had one version of gfortran so I simply changed the g++ and gcc versions to match that of gfortran.

Developing Packages with RStudio – RStudio Support, How do you check if a package is loaded in R? When I execute the install.packages command described in those instructions, R reports "installation of package ‘data.table’ had non-zero exit status". In the interest of time, I'm going to skip this for now, and just use data.frame instead of data.table.

Same problem installing R package minqa on ubuntu 12.04, R3.1.0., an x86 32bits (actually it was part of the caret package installation).

Solved by

sudo ln -s /usr/lib/i386-linux-gnu/libgfortran.so.3 /usr/lib/libgfortran.so

r-base-dev reinstall didn't work and I didn't try to re-install gfortran because of all the dependencies.

Depending on the system/version,

ls -l /usr/lib/libgfortran.so

checks that the link exists/is right.

Checking an R package, RStudio includes a variety of tools that make developing R packages easier and more for Rcpp including syntax highlighting for C/C++ and gcc error navigation There are two main prerequisites for building R packages:. Instructions for Creating Your Own R Package In Song Kimy Phil Martinz Nina McMurryx Andy Halterman{March 18, 2018 1 Introduction The following is a step-by-step guide to creating your own R package. Even beyond this course, you may nd this useful for storing functions you create for your own research or for editing existing R packages to suit

I use Centos and I can't get r-base-dev. I have also installed gfortran and its version matches that of gcc and g++; it still didn't work. However, I solved this problem by creating ~/.R/Makevars, using

cd ~
mkdir .R
touch Makevars

I found the directory where I installed gfortran (apparently the problem is that R can't find it) by

which gfortran

It said I installed gfortran in usr/bin/gfortran. Then I added flags to .R/Makevars to tell R to use:

F77 = /usr/bin/gfortran
FC = $F77
FLIBS = -L/usr/bin/gfortran

You can edit the Makevars file this way:

vi .R/Makevars

Now you have entered the vi program that can edit text files. Type i to edit; you will see INSERT by the bottom of the terminal window. Then you can input what I put above. To save the changes and quit vi, press the esc key, and type :wq.

I'm not totally sure if I put the FLIBS line correctly, since it's very different for MacOS. In MacOS, there's a directory under gfortran that has the libraries to link to, but apparently gfortran is not a directory in linux. At least this worked for me, and also solved the problem of /usr/bin/ld: cannot find -lquadmath, so I installed R packages requiring gfortran smoothly.

Building and installing an R package, R CMD check runs all sorts of checks on the contents of an R package, and gives warnings and error messages when it finds things that aren't right. It also will run the examples in the . Rd files for each of your functions, as well as other automated tests that you've included. This will create a file like brocolors_0.1.tar.gz that you can then install (so that you can load it and use it within R) or give to a friend to install. I generally do so from the command line (on my Mac). If you’re using Windows, you’ll want to install Rtools.

Making Your First R Package, You now have the raw materials for a simple R package. Next you need to build it​. This will create a file like brocolors_0.1.tar.gz that you can then install (so that  This is a one-day intensive course on building a package in R. This course will be a mixture of lectures and computer practicals. The main focus will be getting a working R package ready for distribution.

[PDF] Instructions for Creating Your Own R Package, Hadley Wickham has provided the R community with devtools which helps with building R packages. We will be using this package to make  I don’t know much about setting paths, profiles, and environments, but this indicated that there is no path to latex and so the build function can’t find pdflatex needed to convert the .Rd files (LaTeX code of manuals in R packages) to PDF files for the R package.

Writing R Extensions, Section 4 illustrates how to create a R package with functions written in C++ via Rcpp 3 Building R Package with Command Line Tools If you get an error similar to the following, install development tools available here. Bar charts are a pretty common way to represent data visually, but constructing them isn’t always the most intuitive thing in the world. One way that we can construct these graphs is using R’s default packages. Barplots using base R Let’s start by viewing our dataframe: here we will be finding the mean miles per […]

Comments
  • Couple of questions: 1) Is there really a file /usr/lib/libgfortran.so? (and if it's a symlink, is the file it points to really there?) 2) Is the location of libgfortran.so in your LD_LIBRARY_PATH?
  • I checked the sources. This is a standard R build nothing special of odd about it. Single fortran file so I'm assuming that it is the standard R build trying to link against libgfortran.so
  • Dirk, usually your advice is spot on, but something funny is going on with the config here. In the question I show the build messages. gfortran is found and used to compile delcol.f successfully, but then links again gfortran, which it cannot find.
  • Did you by chance mess around with the symbolic links between the different gcc-*, g++-* and gfortran-* versions and/or their library equivalents? The 'something funny' is sometimes simple operator error. On my box, libgfortran.so.3 lives in /usr/lib/x86_64-linux-gnu/ and comes from the libgfortran3 package.
  • Yes that is the same for mine. There was a problem with the link /usr/lib/libgfortran.so once that was correct to point to /usr/lib/x86_64-linux-gnu/libfortran.so.3.0.0 things work again.
  • Careful. My Ubuntu 11.04 has no /usr/lib/libgfortran.so link. Try ldconfig -p | grep libgfortran and it should show that libgfortran.so.3 is found from the /usr/lib/x86_64-linux-gnus directory. Or else my (pretty new) box at work is off :)
  • Install r-base-dev did the trick for me, on ubuntu, with the same error as the OP
  • Worked for me. Thanks.
  • It seems to me that libgfortran3-dev is missing on Ubuntu 13.10; there are such packages for newer versions, but for R (from the official Ubuntu packages), one seems to need to link against libgfortran.so.3. In other words, I only got it to work with this manual symlinking solution, because installing r-base-dev (and thus, the gfortran packages) did not suffice.
  • Same on Ubuntu 14.04, again I tried Dirk's suggestion first (probably wise to be upgrading R regularly anyway!)