gpt4 book ai didi

Rcpp 链接到外部库 (NLopt)

转载 作者:可可西里 更新时间:2023-11-01 11:16:50 26 4
gpt4 key购买 nike

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

我在 Windows 上使用 RStudio 的“Build & Reload”。

我在包的\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 运行时,我收到以下错误消息,我想,它告诉我链接(参见“ undefined reference ”)到 NLop 库时出现问题:

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'

所以我怀疑我没有正确掌握 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”保存库的路径。

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

最佳答案

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

关于Rcpp 链接到外部库 (NLopt),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36909850/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com