Jean Vache
Jean Vache

Reputation: 93

Rcpp linking to external library (NLopt)

I'd need some help with building a Rcpp package linking to the NLopt library (http://ab-initio.mit.edu/wiki/index.php/NLopt).

I'm on Windows and using RStudio's 'Build & Reload'.

I've the following files in \src folder of the package:

where I've copied the .lib (built using MinGW) and dll files of NLOpt to the \src folder in the hope that R would compile them itself. That doesn't seem to be the case, however, and for that reason I've added a Makevars file, where I try to link to the library.

When running from RStudio, I get the following error message that, I suppose, tells me that there is a problem with linking (cf. "undefined reference") to the NLop library:

installing to library 'C:/Users/g49128/Documents/R/win-library/3.2'

installing source package 'NewPackage' ... ** libs

g++ -m64 -shared -s -static-libgcc -o NewPackage.dll tmp.def RcppExports.o Rcpp_project_test.o -Lc:/Temp/R/Rcpp/NewPackage/src/libnlopt.a -Lc:/applications/extsoft/lib/x64 -Lc:/applications/extsoft/lib -LC:/PROGRA~1/R/R-32~1.2/bin/x64 -lR Rcpp_project_test.o:Rcpp_project_test.cpp:(.text+0x73c): undefined reference to `__imp_nlopt_create'

So I suspect that I haven't got the Makevars right, not so well versed in those.

From consulting several CRAN packages, in particular nloptr, my current best guess on the content of both Makevars files is:

NLOPT_HOME = c:/Temp/R/Rcpp/NewPackage/src.

KG_CFLAGS = -I"$(NLOPT_HOME)"

PKG_LIBS = -L"$(NLOPT_HOME)/libnlopt.a"

where 'libnlopt.a' is the library and the environment variable 'NLOPT_HOME' holds the path to the library.

Could anyone tell me what I'm missing here? Any help would be much appreciated, thank you.

Upvotes: 1

Views: 1728

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368409

The nloptr package itself does that reliably. It uses the following Makevars.win:

# Copyright (C) 2010 Jelmer Ypma. All Rights Reserved.
# This code is published under the L-GPL.
#
# File:   Makevars.win
# Author: Jelmer Ypma
# Date:   18 August 2010
#
# 09 June 2011: Windows support added thanks to Stefan Theussl and Uwe Ligges.
#               NLOPT_HOME is the directory where a working installation of 
#               NLopt is located (within subdirectories NLOPT_VERSION/R_ARCH)
# 18 November 2011: Removed NLOPT_VERSION macro and adopted some other changes
#                   proposed by Brian Ripley to make nloptr work with his new toolchain.
#                   NLopt should now be located in NLOPT_HOME/R_ARCH (i.e. without
#                   version number)
# 19 February 2013: NLopt is compiled with --with-cxx option, in order to include
#                   the StoGo algorithm. This means that we now need to link to 
#                   with -lnlopt_cxx and also link to the c++ library, -lstdc++.
# 7 November 2013: Changed PKG_CPPFLAGS to PKG_CFLAGS.
# 2 May 2014: Added quotes around include paths to allow for spaces in NLOPT_HOME.

# C Compiler options
PKG_CFLAGS = -I"$(NLOPT_HOME)$(R_ARCH)/include"
PKG_LIBS = -L"$(NLOPT_HOME)$(R_ARCH)/lib" -lnlopt_cxx

The requires that you have the correct NLOPT_HOME variable set.

You can sometimes download a corresponding pre-built library from a CRAN support site, but I do not see this here.

Upvotes: 4

Related Questions