[Rcpp-devel] Symbol not found when including an external library

Simon Zehnder szehnder at uni-bonn.de
Thu Sep 5 12:44:50 CEST 2013


Hi Dirk,

I was able to reconstruct my System this night and I immediately tried your suggestions: 

ii) Either using an absolute path nor repositioning the linking did work in my case.

i) Here is the whole building process: 

Simons-MacBook-Pro:finmixcopy simonzehnder$ R CMD INSTALL finmix/
* installing to library ‘/Library/Frameworks/R.framework/Versions/3.0/Resources/library’
* installing *source* package ‘finmix’ ...
** libs
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c FinmixData.cc -o FinmixData.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c FinmixMCMC.cc -o FinmixMCMC.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c FinmixModel.cc -o FinmixModel.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c FinmixPrior.cc -o FinmixPrior.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c LogCondPoissonFix.cc -o LogCondPoissonFix.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c LogCondPoissonInd.cc -o LogCondPoissonInd.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c LogPoissonFix.cc -o LogPoissonFix.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c LogPoissonInd.cc -o LogPoissonInd.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c ParCondPoissonFix.cc -o ParCondPoissonFix.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c ParCondPoissonInd.cc -o ParCondPoissonInd.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c ParOutPoisson.cc -o ParOutPoisson.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c ParPoissonFix.cc -o ParPoissonFix.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c ParPoissonInd.cc -o ParPoissonInd.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c PostOutCondPoissonFix.cc -o PostOutCondPoissonFix.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c PostOutCondPoissonInd.cc -o PostOutCondPoissonInd.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c PostOutPoissonFix.cc -o PostOutPoissonFix.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c PostOutPoissonInd.cc -o PostOutPoissonInd.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c PriorCondPoissonFix.cc -o PriorCondPoissonFix.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c PriorCondPoissonInd.cc -o PriorCondPoissonInd.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c PriorPoissonFix.cc -o PriorPoissonFix.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c PriorPoissonInd.cc -o PriorPoissonInd.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c RcppExports.cpp -o RcppExports.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c attributes.cc -o attributes.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c mcmc_condpoisson.cc -o mcmc_condpoisson.o
g++-4.8.1 -I/Library/Frameworks/R.framework/Resources/include -DNDEBUG -I./nlopt-2.3/include -I/usr/local/include -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/include" -I"/Library/Frameworks/R.framework/Versions/3.0/Resources/library/RcppArmadillo/include"  -g  -fPIC  -g -O2  -c mcmc_poisson.cc -o mcmc_poisson.o
g++-4.8.1 -dynamiclib -Wl,-headerpad_max_install_names -undefined dynamic_lookup -single_module -multiply_defined suppress -L/usr/local/lib -o finmix.so FinmixData.o FinmixMCMC.o FinmixModel.o FinmixPrior.o LogCondPoissonFix.o LogCondPoissonInd.o LogPoissonFix.o LogPoissonInd.o ParCondPoissonFix.o ParCondPoissonInd.o ParOutPoisson.o ParPoissonFix.o ParPoissonInd.o PostOutCondPoissonFix.o PostOutCondPoissonInd.o PostOutPoissonFix.o PostOutPoissonInd.o PriorCondPoissonFix.o PriorCondPoissonInd.o PriorPoissonFix.o PriorPoissonInd.o RcppExports.o attributes.o mcmc_condpoisson.o mcmc_poisson.o -lm /Users/simonzehnder/finmixcopy/finmix/src/nlopt-2.3/lib/libnlopt_cxx.a /Library/Frameworks/R.framework/Versions/3.0/Resources/library/Rcpp/lib/libRcpp.a -F/Library/Frameworks/R.framework/.. -framework R -Wl,-framework -Wl,CoreFoundation
installing to /Library/Frameworks/R.framework/Versions/3.0/Resources/library/finmix/libs
** R
** data
** inst
** preparing package for lazy loading
Creating a generic function for ‘plot’ from package ‘graphics’ in package ‘finmix’
** help
Warning: /Users/simonzehnder/finmixcopy/finmix/man/finmix-package.Rd:32: All text must be in a section
Warning: /Users/simonzehnder/finmixcopy/finmix/man/finmix-package.Rd:33: All text must be in a section
*** installing help indices
** building package indices
** testing if installed package can be loaded
Error in dyn.load(file, DLLpath = DLLpath, ...) :
  unable to load shared object '/Library/Frameworks/R.framework/Versions/3.0/Resources/library/finmix/libs/finmix.so':
  dlopen(/Library/Frameworks/R.framework/Versions/3.0/Resources/library/finmix/libs/finmix.so, 6): Symbol not found: __ZNSt8__detail15_List_node_base11_M_transferEPS0_S1_
  Referenced from: /Library/Frameworks/R.framework/Versions/3.0/Resources/library/finmix/libs/finmix.so
  Expected in: flat namespace
 in /Library/Frameworks/R.framework/Versions/3.0/Resources/library/finmix/libs/finmix.so
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/Library/Frameworks/R.framework/Versions/3.0/Resources/library/finmix’
* restoring previous ‘/Library/Frameworks/R.framework/Versions/3.0/Resources/library/finmix’

I see, that the headers of nlopt are included in the build process and then the linker recognizes also the static library. 

I took a look into the Makevars of the nloptr package: There, the Flags from R CMD config are used to compile the nlopt (see below the attached code from nloptr). Maybe this is the shortcoming in my installation of nlopt? 


Best

Simon

CODE FROM Makevars nloptr:

# define NLopt version
NLOPT_VERSION = 2.3

# C Compiler options
NLOPTR_CFLAGS = 

# additional C Compiler options for linking
NLOPTR_CLINKFLAGS = 

# Libraries necessary to link with NLopt
NLOPTR_LIBS = -lm `pwd`/nlopt-${NLOPT_VERSION}/lib/libnlopt_cxx.a -lstdc++

# Necessary Include dirs 
NLOPTR_INCL = -I`pwd`/nlopt-${NLOPT_VERSION}/include

# Get R compilers and flags
NLOPT_CC=`"${R_HOME}/bin/R" CMD config CC`
NLOPT_CFLAGS=`"${R_HOME}/bin/R" CMD config CFLAGS`

NLOPT_CPP=`"${R_HOME}/bin/R" CMD config CPP`
NLOPT_CPPFLAGS=`"${R_HOME}/bin/R" CMD config CPPFLAGS`

NLOPT_CXX=`"${R_HOME}/bin/R" CMD config CXX`
NLOPT_CXXFLAGS=`"${R_HOME}/bin/R" CMD config CXXFLAGS`

NLOPT_CXXCPP=`"${R_HOME}/bin/R" CMD config CXXCPP`


# Define objects for R to build
OBJECTS = nloptr.o

# Convert to R macros
PKG_LIBS=${NLOPTR_CLINKFLAGS} ${NLOPTR_LIBS}
PKG_CFLAGS=${NLOPTR_CFLAGS} ${NLOPTR_INCL}

.PHONY: all
     
all: $(SHLIB)

$(SHLIB): nlopt-${NLOPT_VERSION}/lib/libnlopt_cxx.a ${OBJECTS}

nloptr.o: nlopt-${NLOPT_VERSION}/include/nlopt.h
  
# Compile NLopt source code and clean up
# --prefix="`pwd`", which is the directory we want to
# install in, after we changed our current directory
nlopt-${NLOPT_VERSION}/include/nlopt.h nlopt-${NLOPT_VERSION}/lib/libnlopt_cxx.a: nlopt-${NLOPT_VERSION}/configure
	echo "Installing library to: `pwd`/nlopt-${NLOPT_VERSION}"
	cd nlopt-${NLOPT_VERSION}; \
	./configure --prefix="`pwd`" --enable-shared --enable-static --without-octave --without-matlab --without-guile --without-python --with-cxx CC="${NLOPT_CC}" CFLAGS="${NLOPT_CFLAGS}" CPP="${NLOPT_CPP}" CPPFLAGS="${NLOPT_CPPFLAGS}" CXX="${NLOPT_CXX}" CXXFLAGS="${NLOPT_CXXFLAGS}" CXXCPP="${NLOPT_CXXCPP}"; \
	make; \
	make install; \
	ls | grep -v ^include$$ | grep -v ^lib$$ | xargs rm -rf; \
    rm -rf .libs;

# Extract NLopt source code and remove .tar.gz
# tar -xzvf nlopt-${NLOPT_VERSION}.tar.gz
nlopt-${NLOPT_VERSION}/configure: nlopt-${NLOPT_VERSION}.tar.gz
	`"${R_HOME}/bin/Rscript" --vanilla -e "untar(tarfile='nlopt-${NLOPT_VERSION}.tar.gz')"`
	rm -rf nlopt-${NLOPT_VERSION}.tar.gz

# Download NLopt source code
# curl -O http://ab-initio.mit.edu/nlopt/nlopt-${NLOPT_VERSION}.tar.gz
nlopt-${NLOPT_VERSION}.tar.gz:
	`"${R_HOME}/bin/Rscript" --vanilla -e "download.file(url='http://ab-initio.mit.edu/nlopt/nlopt-${NLOPT_VERSION}.tar.gz', destfile='nlopt-${NLOPT_VERSION}.tar.gz')"`

clean:
	rm -rf *.so
	rm -rf *.o
	rm -rf nlopt-${NLOPT_VERSION}

 
On Sep 4, 2013, at 7:30 PM, Dirk Eddelbuettel <edd at debian.org> wrote:

> 
> On 4 September 2013 at 18:51, Simon Zehnder wrote:
> | Dear Rcpp::Users, Rcpp::Devels,
> | 
> | I am following the ongoing discussion 'How to use external C++ libraries in R packages withRcpp' (Makevars are still one of my weaknesses) and I have a related question. 
> | 
> | I am trying to use the C++ optimization library nlopt (http://ab-initio.mit.edu/wiki/index.php/NLopt) in my package that is based on RcppArmadillo. In a first step I installed nlopt in a subfolder of the 'src' folder of my package structure (so I have '/src/nlopt-2.3'). Second, I included the header file into my source file right below '#include <RcppArmadillo.h>'. Then I set my Makevars file to:
> | 
> | ## Use the R_HOME indirection to support installations of multiple R version
> | NLOPT_VERSION = 2.3
> | 
> | NLOPT_LIBS = -lm nlopt-${NLOPT_VERSION}/lib/libnlopt_cxx.a
> | 
> | NLOPT_INCL = -I./nlopt-${NLOPT_VERSION}/include
> | 
> | PKG_CPPFLAGS = ${NLOPT_INCL}
> | PKG_CFLAGS = -pipe ${NLOPT_INCL}
> | PKG_LIBS = `$(R_HOME)/bin/Rscript -e "Rcpp:::LdFlags()"` ${NLOPT_LIBS}
> | 
> | Calling 'R CMD INSTALL packageName' compiles without errors but produces the well-known error during loading the shared library:
> | 
> | Error in dyn.load(file, DLLpath = DLLpath, ...) :
> |   unable to load shared object '/Library/Frameworks/R.framework/Versions/3.0/Resources/library/finmix/libs/finmix.so':
> |   dlopen(/Library/Frameworks/R.framework/Versions/3.0/Resources/library/finmix/libs/finmix.so, 6): Symbol not found: __ZNSt8__detail15_List_node_base11_M_transferEPS0_S1_
> 
> Briefly:
> 
> i)   Please show the actual linking step.
> 
> ii)  Mixing shared and static is a bit messy, but I have done it too I
>      think. You may want to try _an absolute path_ to the .a library,
>      and a different link position, eg place ${NLOPT_LIBS} first.
> 
> This is definitely doable.
> 
> Dirk
> 
> 
> | I find the missing symbol (using 'nm -A libnlopt_cxx.a | grep "transfer"' on the shell) in the static library of nlopt (libnlopt_cxx.a) to which I link in the Makevars, but I do not know yet, how I have to modify the Makevars to include also the static library of nlopt into the shared library of my package. Does anyone have a suggestion how to do it? 
> | 
> | 
> | Best
> | 
> | Simon
> | 
> | 
> | _______________________________________________
> | Rcpp-devel mailing list
> | Rcpp-devel at lists.r-forge.r-project.org
> | https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
> 
> -- 
> Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com



More information about the Rcpp-devel mailing list