【问题标题】:Rcpp linking to external library (NLopt)Rcpp 链接到外部库 (NLopt)
【发布时间】:2016-04-28 08:57:12
【问题描述】:

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

我在 Windows 上并使用 RStudio 的“构建和重新加载”。

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

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

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

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

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

安装源代码包'NewPackage' ... ** 库

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)/libnlopt.a"

其中“libnlopt.a”是库,环境变量“NLOPT_HOME”保存库的路径。

谁能告诉我我在这里缺少什么?任何帮助将不胜感激,谢谢。

【问题讨论】:

  • 我认为你在 Makevars 中反转了 -l-L-L 用于目录 -l 用于 lib 文件)
  • 感谢您的输入,但反转 -l 和 -L(以及在 lib 路径前既没有 -l 也没有 -L)似乎并没有解决问题;在这种情况下,编译器抱怨它找不到 -lc:/Temp/R/Rcpp/NewPackage/src/libnlopt.a.

标签: r windows rcpp static-linking .lib


【解决方案1】:

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 支持站点下载相应的预构建库,但我在这里没有看到。

【讨论】:

  • 谢谢德克。我现在可以使用我之前尝试过的以下 Makevars.win 文件。不知道为什么它突然起作用,但它很棒。 PKG_CFLAGS = -l"$(NLOPT_HOME)" PKG_LIBS = -L"$(NLOPT_HOME)" -lnlopt
猜你喜欢
  • 2021-09-17
相关资源
最近更新 更多