Skip to content

[Rcpp-devel] Compilation errors when building a package with RcppArmadillo code and using attributes

8 messages · Yan Zhou, Howard Zail, Dirk Eddelbuettel +1 more

#
I am trying to build a package with RcppArmadillo code and using the new
attributes-type methodology.  When I compile using sourceCpp() the
functions compile and work.  However when I use
"Rcpp.package.skeleton("NewPackage", example_code = FALSE, cpp_files =
c("test_arma3.cpp"))", and then "R CMD INSTALL...." I get compilation
errors.  Here is some sample Armadillo code (the fastLM example) which is
then followed by the compilation errors.  Any help would be much
appreciated.
Thanks,

Sample Code:

// [[Rcpp::depends(RcppArmadillo)]]


#include <Rcpp.h>
#include <RcppArmadillo.h>

using namespace Rcpp;


// [[Rcpp::depends(RcppArmadillo)]]
#include <RcppArmadillo.h>
// [[Rcpp::export]]
List fastLm(NumericVector yr, NumericMatrix Xr) {
      int n = Xr.nrow(), k = Xr.ncol();
  arma::mat X(Xr.begin(), n, k, false);
  arma::colvec y(yr.begin(), yr.size(), false);
  arma::colvec coef = arma::solve(X, y);
  arma::colvec resid = y - X*coef;
  double sig2 = arma::as_scalar(arma::trans(resid)*resid/(n-k));
  arma::colvec stderrest = arma::sqrt(
  sig2 * arma::diagvec( arma::inv(arma::trans(X)*X)) );
  return List::create(Named("coefficients") = coef,
  Named("stderr") = stderrest);
}


Which produces the following errors when building the package:
* Updated src/RcppExports.cpp

==> Rcmd.exe INSTALL --no-multiarch NewPackage

* installing to library 'C:/Users/Howard/Documents/R/win-library/2.15'
* installing *source* package 'NewPackage' ...
** libs
g++ -m64 -I"C:/R/R-2-15-2/include" -DNDEBUG
-I"C:/Users/Howard/Documents/R/win-library/2.15/Rcpp/include"
-I"C:/Users/Howard/Documents/R/win-library/2.15/RcppArmadillo/include"
-I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall
-mtune=core2 -c RcppExports.cpp -o RcppExports.o
g++ -m64 -I"C:/R/R-2-15-2/include" -DNDEBUG
-I"C:/Users/Howard/Documents/R/win-library/2.15/Rcpp/include"
-I"C:/Users/Howard/Documents/R/win-library/2.15/RcppArmadillo/include"
-I"d:/RCompile/CRANpkg/extralibs64/local/include"     -O2 -Wall
-mtune=core2 -c test_arma3.cpp -o test_arma3.o
g++ -m64 -shared -s -static-libgcc -o NewPackage.dll tmp.def
RcppExports.o test_arma3.o
C:/Users/Howard/Documents/R/win-library/2.15/Rcpp/lib/x64/libRcpp.a
-Ld:/RCompile/CRANpkg/extralibs64/local/lib/x64
-Ld:/RCompile/CRANpkg/extralibs64/local/lib -LC:/R/R-2-15-2/bin/x64
-lR
test_arma3.o:test_arma3.cpp:(.text+0xae4): undefined reference to `dgemm_'
test_arma3.o:test_arma3.cpp:(.text+0x19db): undefined reference to `dgemm_'
test_arma3.o:test_arma3.cpp:(.text+0x1b0c): undefined reference to `dgemv_'
test_arma3.o:test_arma3.cpp:(.text$_ZN4arma6auxlib8solve_odIdNS_3MatIdEEEEbRNS2_IT_EES6_RKNS_4BaseIS4_T0_EE[_ZN4arma6auxlib8solve_odIdNS_3MatIdEEEEbRNS2_IT_EES6_RKNS_4BaseIS4_T0_EE]+0x702):
undefined reference to `dgels_'
test_arma3.o:test_arma3.cpp:(.text$_ZN4arma6auxlib8solve_udIdNS_3MatIdEEEEbRNS2_IT_EES6_RKNS_4BaseIS4_T0_EE[_ZN4arma6auxlib8solve_udIdNS_3MatIdEEEEbRNS2_IT_EES6_RKNS_4BaseIS4_T0_EE]+0x51c):
undefined reference to `dgels_'
test_arma3.o:test_arma3.cpp:(.text$_ZN4arma6auxlib10det_lapackIdEET_RKNS_3MatIS2_EEb[_ZN4arma6auxlib10det_lapackIdEET_RKNS_3MatIS2_EEb]+0x14b):
undefined reference to `dgetrf_'
test_arma3.o:test_arma3.cpp:(.text$_ZN4arma6auxlib5solveIdNS_3MatIdEEEEbRNS2_IT_EES6_RKNS_4BaseIS4_T0_EEb[_ZN4arma6auxlib5solveIdNS_3MatIdEEEEbRNS2_IT_EES6_RKNS_4BaseIS4_T0_EEb]+0x375):
undefined reference to `dgesv_'
test_arma3.o:test_arma3.cpp:(.text$_ZN4arma4gemvILb1ELb0ELb0EE15apply_blas_typeIdEEvPT_RKNS_3MatIS3_EEPKS3_S3_S3_[_ZN4arma4gemvILb1ELb0ELb0EE15apply_blas_typeIdEEvPT_RKNS_3MatIS3_EEPKS3_S3_S3_]+0x17d):
undefined reference to `dgemv_'
test_arma3.o:test_arma3.cpp:(.text$_ZN4arma27glue_times_redirect2_helperILb1EE5applyINS_2OpINS_3MatIdEENS_9op_htransEEES5_EEvRNS4_INT_9elem_typeEEERKNS_4GlueIS8_T0_NS_10glue_timesEEE[_ZN4arma27glue_times_redirect2_helperILb1EE5applyINS_2OpINS_3MatIdEENS_9op_htransEEES5_EEvRNS4_INT_9elem_typeEEERKNS_4GlueIS8_T0_NS_10glue_timesEEE]+0x37a):
undefined reference to `dgemm_'
test_arma3.o:test_arma3.cpp:(.text$_ZN4arma10op_diagvec5applyINS_2OpINS_4GlueINS2_INS_3MatIdEENS_9op_htransEEES5_NS_10glue_timesEEENS_6op_invEEEEEvRNS4_INT_9elem_typeEEERKNS2_ISC_S0_EE[_ZN4arma10op_diagvec5applyINS_2OpINS_4GlueINS2_INS_3MatIdEENS_9op_htransEEES5_NS_10glue_timesEEENS_6op_invEEEEEvRNS4_INT_9elem_typeEEERKNS2_ISC_S0_EE]+0x2c1):
undefined reference to `dgetrf_'
test_arma3.o:test_arma3.cpp:(.text$_ZN4arma10op_diagvec5applyINS_2OpINS_4GlueINS2_INS_3MatIdEENS_9op_htransEEES5_NS_10glue_timesEEENS_6op_invEEEEEvRNS4_INT_9elem_typeEEERKNS2_ISC_S0_EE[_ZN4arma10op_diagvec5applyINS_2OpINS_4GlueINS2_INS_3MatIdEENS_9op_htransEEES5_NS_10glue_timesEEENS_6op_invEEEEEvRNS4_INT_9elem_typeEEERKNS2_ISC_S0_EE]+0x322):
undefined reference to `dgetri_'
test_arma3.o:test_arma3.cpp:(.text$_ZN4arma10op_diagvec5applyINS_2OpINS_4GlueINS2_INS_3MatIdEENS_9op_htransEEES5_NS_10glue_timesEEENS_6op_invEEEEEvRNS4_INT_9elem_typeEEERKNS2_ISC_S0_EE[_ZN4arma10op_diagvec5applyINS_2OpINS_4GlueINS2_INS_3MatIdEENS_9op_htransEEES5_NS_10glue_timesEEENS_6op_invEEEEEvRNS4_INT_9elem_typeEEERKNS2_ISC_S0_EE]+0x398):
undefined reference to `dgetri_'
test_arma3.o:test_arma3.cpp:(.text$_ZN4arma10op_diagvec5applyINS_2OpINS_4GlueINS2_INS_3MatIdEENS_9op_htransEEES5_NS_10glue_timesEEENS_6op_invEEEEEvRNS4_INT_9elem_typeEEERKNS2_ISC_S0_EE[_ZN4arma10op_diagvec5applyINS_2OpINS_4GlueINS2_INS_3MatIdEENS_9op_htransEEES5_NS_10glue_timesEEENS_6op_invEEEEEvRNS4_INT_9elem_typeEEERKNS2_ISC_S0_EE]+0x775):
undefined reference to `dgetrf_'
test_arma3.o:test_arma3.cpp:(.text$_ZN4arma10op_diagvec5applyINS_2OpINS_4GlueINS2_INS_3MatIdEENS_9op_htransEEES5_NS_10glue_timesEEENS_6op_invEEEEEvRNS4_INT_9elem_typeEEERKNS2_ISC_S0_EE[_ZN4arma10op_diagvec5applyINS_2OpINS_4GlueINS2_INS_3MatIdEENS_9op_htransEEES5_NS_10glue_timesEEENS_6op_invEEEEEvRNS4_INT_9elem_typeEEERKNS2_ISC_S0_EE]+0x7d6):
undefined reference to `dgetri_'
test_arma3.o:test_arma3.cpp:(.text$_ZN4arma10op_diagvec5applyINS_2OpINS_4GlueINS2_INS_3MatIdEENS_9op_htransEEES5_NS_10glue_timesEEENS_6op_invEEEEEvRNS4_INT_9elem_typeEEERKNS2_ISC_S0_EE[_ZN4arma10op_diagvec5applyINS_2OpINS_4GlueINS2_INS_3MatIdEENS_9op_htransEEES5_NS_10glue_timesEEENS_6op_invEEEEEvRNS4_INT_9elem_typeEEERKNS2_ISC_S0_EE]+0x892):
undefined reference to `dgetri_'
collect2: ld returned 1 exit status
ERROR: compilation failed for package 'NewPackage'
* removing 'C:/Users/Howard/Documents/R/win-library/2.15/NewPackage'
* restoring previous 'C:/Users/Howard/Documents/R/win-library/2.15/NewPackage'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121202/315c103d/attachment.html>
#
You haven't link to BLAS, and thus there are undefined reference to dgemm etc?
On Dec 02, 2012, at 01:35 PM, Howard Zail <howard at zail.org> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121202/c7e3dcc6/attachment-0001.html>
#
On Sun, Dec 2, 2012 at 8:35 AM, Howard Zail <howard at zail.org> wrote:
For building packages with attributes dependencies are specified using
Depends and LinkingTo rather than the Rcpp::depends attribute (this is
covered in the attributes vignette but it certainly could be missed).

Note also that we have an outstanding work item to have
Rcpp.package.skeleton look for Rcpp::depends attribute in cpp_file and
automatically add depended on packages to the DESCRIPTION. For now
though this has to be done manually.

J.J.
#
Thanks Yan.  This solved the issue:  I did the following change:

Link against the Rcpp, blas and lapack libraries, by adding this line in
the Makevars

PKG_LIBS = $(shell $(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()" )
$(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
and this line to the Makevars.win:
PKG_LIBS = $(shell $(R_HOME)/bin${R_ARCH_BIN}/Rscript.exe -e
"Rcpp:::LdFlags()") $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)
On Sun, Dec 2, 2012 at 8:46 AM, Yan Zhou <zhouyan at me.com> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121202/5b10f900/attachment.html>
#
On 2 December 2012 at 10:29, Howard Zail wrote:
| Thanks Yan. ?This solved the issue: ?I did the following change:
| 
| Link against the Rcpp, blas and lapack libraries, by adding this line in the
| Makevars
| 
| PKG_LIBS = $(shell $(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()" ) $(LAPACK_LIBS)
| $(BLAS_LIBS) $(FLIBS)?
| and this line to the Makevars.win:
| PKG_LIBS = $(shell $(R_HOME)/bin${R_ARCH_BIN}/Rscript.exe -e "Rcpp:::LdFlags()
| ") $(LAPACK_LIBS) $(BLAS_LIBS) $(FLIBS)

Right. This is both the default for RcppArmadillo --- see the skeleton/
directory in the installed package --- and its inline plugin.  As JJ said,
not everything is fully automated yet.

But you wrote 'when I use "Rcpp.package.skeleton()..."' and that may have
been the error.  Can you check if RcppArmadillo.package.skeleton() does better?

Dirk
#
The RcppArmadillo.package.skeleton does not work as I am providing a
reference to a cpp file with attributes.  I get this error:

Error in RcppArmadillo.package.skeleton("NewPackage", example_code = FALSE,
 :
unused argument(s) (cpp_files = c("test_arma3.cpp"))


Howard
On Sun, Dec 2, 2012 at 11:22 AM, Dirk Eddelbuettel <edd at debian.org> wrote:

            
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121202/3f3b3b9a/attachment.html>
#
On 2 December 2012 at 18:59, Howard Zail wrote:
| The RcppArmadillo.package.skeleton does not work as I am providing a reference
| to a cpp file with attributes. ?I get this error:
| 
| Error in RcppArmadillo.package.skeleton("NewPackage", example_code = FALSE,
| ?:?
| unused argument(s) (cpp_files = c("test_arma3.cpp"))

Thanks for checking. We will try to look into this.

Dirk
#
Some additional insight: we plan to update Rcpp.package.skeleton to be able
to interrogate packages like RcppArmadillo for their packaging
dependencies. This will be based on the inline plugin system. For example,
if you call this function:

RcppArmadillo:::inlineCxxPlugin()

You'll see that RcppArmadillo can report everything that's required for
it's Makevars, etc. The inline plugin system is very general so all
packages will be able to participate in this (and won't necessarily have to
create their own package.skeleton function unless they want to include
additional example code).

For now, if you just call RcppArmadillo.package.skeleton with it's default
arguments and then drop your cpp file into the package src directory things
should work fine (note you'll need to remember to instrument a call to
compileAttributes at the appropriate time).

J.J.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20121202/99094056/attachment.html>