我正在使用 Qt Creator 制作一个静态库,该库将用于更大的基于 GUI 的应用程序。我以前使用过 Rcpp,但以 R 为起点 - 因此将 R 数据移动到 C++ 函数以执行操作并获得结果,我习惯于使用 R studio 进行操作。这次我开始一个 Qt 项目,我需要一个表来存储一些数据,并且 Rcpp::Dataframe 和一些向量类一样适合这项工作。在我的 .cpp 源文件中,我有 include 语句#include <Rcpp.h>
,我在网上查看并找到了 Qt 中使用的 Rcpp / RInside 库的示例——尽管它更多的是关于 RInside 而不是 Rcpp:RInside 和 Qt
似乎需要在 .pro 文件中添加一些东西才能让 qmake 做正确的事情。我在下面附加的 .pro 文件中包含了我认为 Rcpp 所需的内容。我遇到的问题是,在 .pro 文件中包含 Rcpp 内容和 .cpp 文件中的 include 语句之前,包含您也可以在下面的 .pro 文件中看到的 bpp 库不会导致编译中的任何错误或警告。在 .pro 文件中包含包含 R 和 Rcpp 的语句后,我收到许多警告和一个与 bpp 组件有关的错误,特别是:/local/yrq12edu/local/bpp/dev/include/Bpp/Numeric/NumConstants.h:96: error: expected unqualified-id before numeric constant
. 还有很多Wunused-parameter
消息,其中大部分似乎来自 bpp 内的文件。这是我第一次不得不处理这样的事情,虽然我已经阅读了很多 Qt 文档,但我仍然是一个非常新鲜的 Qt 新手。为什么在使用 bpp 之前我不会收到任何错误,但现在我已经尝试将 Rcpp 包含到我的 Qt 项目中?我应该怎么做才能采取措施解决这个问题?
#-------------------------------------------------
#
# Project created by QtCreator 2014-09-05T23:27:21
#
#-------------------------------------------------
QT -= core gui
TARGET = libHybRIDS
TEMPLATE = lib
CONFIG += staticlib
SOURCES += hybridsengine.cpp
HEADERS += hybridsengine.h
INCLUDEPATH += /local/yrq12edu/local/bpp/dev/include
LIBS += -L/local/yrq12edu/local/bpp/dev/lib -lbpp-core
LIBS += -L/local/yrq12edu/local/bpp/dev/lib -lbpp-seq
# Set R home directory
R_HOME = $$system(R RHOME)
# Set Rcpp include and lib flags.
RCPPINCL = $$system($$R_HOME/bin/Rscript -e \'Rcpp:::CxxFlags\(\)\')
RCPPLIBS = $$system($$R_HOME/bin/Rscript -e \'Rcpp:::LdFlags\(\)\')
# Set R cpp and ld flags. Also for BLAS and LAPACK it uses.
RCPPFLAGS = $$system($$R_HOME/bin/R CMD config --cppflags)
RLDFLAGS = $$system($$R_HOME/bin/R CMD config --ldflags)
RBLAS = $$system($$R_HOME/bin/R CMD config BLAS_LIBS)
RLAPACK = $$system($$R_HOME/bin/R CMD config LAPACK_LIBS)
RCPPWARNING = -Wno-unused-parameter
QMAKE_CXXFLAGS += $$RCPPWARNING $$RCPPFLAGS $$RCPPINCL
QMAKE_LFLAGS += $$RLDFLAGS $$RBLAS $$RLAPACK $$RCPPLIBS
unix {
target.path = /usr/lib
INSTALLS += target
}
编辑: 按照建议,我使用 RInside/examples/qt 中的示例 pro 文件作为起点 - 保持语句的顺序:
#-------------------------------------------------
#
# Project created by QtCreator 2014-09-05T23:27:21
#
#-------------------------------------------------
QT -= core gui
TARGET = libHybRIDS
TEMPLATE = lib
CONFIG += staticlib
SOURCES += hybridsengine.cpp
HEADERS += hybridsengine.h
## comment this out if you need a different version of R,
## and set set R_HOME accordingly as an environment variable
R_HOME = $$system(R RHOME)
#message("R_HOME is" $$R_HOME)
## include headers and libraries for R
RCPPFLAGS = $$system($$R_HOME/bin/R CMD config --cppflags)
RLDFLAGS = $$system($$R_HOME/bin/R CMD config --ldflags)
RBLAS = $$system($$R_HOME/bin/R CMD config BLAS_LIBS)
RLAPACK = $$system($$R_HOME/bin/R CMD config LAPACK_LIBS)
## if you need to set an rpath to R itself, also uncomment
RRPATH = -Wl,-rpath,$$R_HOME/lib
## include headers and libraries for Rcpp interface classes
## note that RCPPLIBS will be empty with Rcpp (>= 0.11.0) and can be omitted
RCPPINCL = $$system($$R_HOME/bin/Rscript -e \"Rcpp:::CxxFlags\(\)\")
RCPPLIBS = $$system($$R_HOME/bin/Rscript -e \"Rcpp:::LdFlags\(\)\")
## for some reason when building with Qt we get this each time
## /usr/local/lib/R/site-library/Rcpp/include/Rcpp/module/Module_generated_ctor_signature.h:25: warning: unused parameter ‘classname
## so we turn unused parameter warnings off
## no longer needed with Rcpp 0.9.3 or later
#RCPPWARNING = -Wno-unused-parameter
## include headers and libraries for RInside embedding classes
RINSIDEINCL = $$system($$R_HOME/bin/Rscript -e \"RInside:::CxxFlags\(\)\")
RINSIDELIBS = $$system($$R_HOME/bin/Rscript -e \"RInside:::LdFlags\(\)\")
## compiler etc settings used in default make rules
QMAKE_CXXFLAGS += $$RCPPWARNING $$RCPPFLAGS $$RCPPINCL $$RINSIDEINCL
QMAKE_LIBS += $$RLDFLAGS $$RBLAS $$RLAPACK $$RINSIDELIBS $$RCPPLIBS
## addition clean targets
QMAKE_CLEAN += qtdensity Makefile
unix {
target.path = /usr/lib
INSTALLS += target
}
在我的语句中包含与 RInside 和 Rcpp 相关的语句之前或之后的 bpp 库会导致与以前相同的错误。
来自 bpp 的 .h 文件的内容是:
#ifndef _NUMCONSTANTS_H_
#define _NUMCONSTANTS_H_
#include <cmath>
#include <limits>
namespace bpp {
/**
* @brief this static class contains several useful constant values.
*
* This classe uses function in order to avoid the infamous "static initialization order fiasco".
* C++0x solves this...
*/
class NumConstants
{
public:
/**
* @name Golden ratio.
*
* The golden ratio, @f$\phi@f$ is equal to @f$\frac{1+\sqrt{5}}{2} = 1.6180339887498948482\ldots@f$.
* We also define @f$R=\phi-1@f$ and @f$C = 1 - R@f$.
* @{
*/
static double GOLDEN_RATIO_PHI() { return (1. + sqrt(5.)) / 2.; }
static double GOLDEN_RATIO_R() { return GOLDEN_RATIO_PHI() - 1.; }
static double GOLDEN_RATIO_C() { return 1. - GOLDEN_RATIO_R(); }
/** @} */
static double MEGA() { return 1e6; }
static double KILO() { return 1e3; }
static double DECI() { return 1e-1; }
static double CENTI() { return 1e-2; }
static double MILLI() { return 1e-3; }
static double MICRO() { return 1e-6; }
static double NANO() { return 1e-9; }
static double PICO() { return 1e-12; }
static double SMALL() { return 1e-6; }
static double TINY() { return 1e-12; }
static double VERY_TINY() { return 1e-20; }
static double VERY_BIG() { return static_cast<double>(1.7E+23); }
/**
* @name Define those constants in case they would not be available in stl/limits.
*
* @{
*/
static double INF() { return std::numeric_limits<double>::has_infinity ? -log(0) : std::numeric_limits<double>::max(); }
static double PINF() { return std::numeric_limits<double>::has_infinity ? -log(0) : std::numeric_limits<double>::max(); }
static double MINF() { return std::numeric_limits<double>::has_infinity ? log(0) : std::numeric_limits<double>::min(); }
static double NaN() { return NAN; }
/** @} */
static double PI() { return 3.141593; }
};
}//end of namespace bpp.
#endif //_NUMCONSTANTS_H_