Rcpp链接到外部库(NLopt)

时间:2016-04-28 08:57:12

标签: r windows rcpp static-linking .lib

我需要一些帮助来构建链接到NLopt库(http://ab-initio.mit.edu/wiki/index.php/NLopt)的Rcpp包。

我在Windows上使用RStudio的'Build&重新加载”。

我在包的\ src文件夹中有以下文件:

  • \ SRC
    • Makevars.in
    • Makevars.win
    • RcppExports.cpp
    • nlopt.h
    • nlopt.hpp
    • libnlopt.lib
    • libnlopt.def
    • libnlopt.a
    • libnlopt.dll

我已经将.lib(使用MinGW构建)和NLOpt的dll文件复制到\ src文件夹中,希望R能够自己编译它们。但是,情况似乎并非如此,因此我添加了一个Makevars文件,我尝试链接到库。

从RStudio运行时,我收到以下错误消息,我想,它告诉我链接(参见“未定义的引用”)与NLop库存在问题:

  

安装到库'C:/Users/g49128/Documents/R/win-library/3.2'

     

安装包'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):对__imp_nlopt_create 的未定义引用

所以我怀疑我没有让Makevars合适,不太熟悉那些。

通过咨询几个CRAN软件包,特别是nloptr,我目前对Makevars文件内容的最佳猜测是:

  

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

     

KG_CFLAGS = -I“$(NLOPT_HOME)”

     

PKG_LIBS = -L“$(NLOPT_HOME)/llnlopt.a”

其中'libnlopt.a'是库,环境变量'NLOPT_HOME'包含库的路径。

谁能告诉我这里缺少什么?非常感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:4)

nloptr包本身可以做到这一点。它使用以下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

要求您拥有正确的NLOPT_HOME变量集。

您有时可以从CRAN支持网站下载相应的预建库,但我在此处没有看到。