了解 R 中 Makevars 文件的内容(宏、变量、~/.R/Makevars 和 pkg/src/Makevars) [英] Understanding the contents of the Makevars file in R (macros, variables, ~/.R/Makevars and pkg/src/Makevars)

查看:207
本文介绍了了解 R 中 Makevars 文件的内容(宏、变量、~/.R/Makevars 和 pkg/src/Makevars)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解在安装/构建自己的~/.R/Makevarspackage_directory/src/Makevars 中设置的宏/变量的作用和关系R 包.假设这些文件看起来像

I am trying to understand the role and relationship of the macros/variables set in ~/.R/Makevars and package_directory/src/Makevars when installing/building own R packages. Suppose these files look like

~/.R/Makevars

~/.R/Makevars

CXX = g++
CXXSTD = -std=c++11
CXXFLAGS = -fsanitize=undefined,address -fno-omit-frame-pointer

CXX98 = g++
CXX98STD = -std=c++98

CXX11 = g++
CXX11STD = -std=c++11

CXX14 = g++
CXX14STD = -std=c++14

package_directory/src/Makevars

package_directory/src/Makevars

PKG_CPPFLAGS = -I../inst/include
CXX_STD = CXX11

据我所知,使用 CXX 我们可以在构建 R 包时选择 C++ 的编译器,使用 CXXSTD 我们选择标准,使用 CXXFLAGS 我们添加编译器标志.使用 PKG_CPPFLAGS 我们为 C++ 预处理器添加标志,使用 CXX_STD 我们告诉我们我们的包使用 C++11.

As I understand it, with CXX we can select the compiler for C++ when building R packages, with CXXSTD we chose the standard and with CXXFLAGS we add compiler flags. With PKG_CPPFLAGS we add flags for the C++ preprocessor and with CXX_STD we tell that our packages uses C++11.

我有以下问题:

  • CXXCXX98CXX11CXX14 是什么关系?
  • 如果已经隐含了 C++11,例如 CXX11STD = -std=c++11 是什么意思?是否在选择 -std=c++11-std=gnu++11 之间?出于可移植性的原因,通常应该避免 -std=gnu++11 吗?
  • 是否可以将 CXXSTDCXXFLAGS 的标志不只是添加到 CXX 中,这样前三行减少为 CXX= g++ -std=c++11 -fsanitize=undefined,address -fno-omit-frame-pointer.显式指定 CXXSTDCXXFLAGS 有什么好处?
  • CXX_STD = CXX11 如何工作?CXX11~/.R/Makevars 中的 CXX11 有什么关系?
  • CXXFLAGSPKG_CXXFLAGS(不包括在我的示例中)之间的关系是什么?
  • What is the relationship between CXX and CXX98, CXX11 and CXX14?
  • What is the meaning of, e.g., CXX11STD = -std=c++11 if C++11 is already implied? Is it between choosing -std=c++11 and -std=gnu++11? Should -std=gnu++11 generally be avoided for portability reasons?
  • Could the flags for CXXSTD and CXXFLAGS not just be added to CXX, such that the first three lines reduce to CXX = g++ -std=c++11 -fsanitize=undefined,address -fno-omit-frame-pointer. What is the advantage in explicity specifying CXXSTD and CXXFLAGS?
  • How does CXX_STD = CXX11 work? How is CXX11 here related to CXX11 in ~/.R/Makevars?
  • What is the relationship between CXXFLAGS and PKG_CXXFLAGS(not included in my example)?

我知道 Writing R 中包含的信息扩展R安装和管理,但我无法提取超出我目前理解水平的更多信息来回答上述问题.

I am aware of the information contained in Writing R Extensions and R Installation and Administration, but I am not able to extract more information beyond my current level of understanding to answer the above questions.

我添加一个 Rcpp 标签是因为我认为这些问题的答案与 Rcpp 的用户最相关,但我知道这可能不是直接的与 Rcpp 相关,因此如果认为合适,该标签可能会被删除.

I am adding a Rcpp tag because I suppose that answers to these questions will be most relevant to users of Rcpp, but I am aware that this is probably not directly related to Rcpp, so the tag might be removed if deemed appropriate.

推荐答案

Makevars 文件,如 编写 R 扩展:1.2.1 使用 Makevars,是 Make 对 R 来说独特.许多变量您列出的被称为隐式变量.其含义如下:

The Makevars file, as specified in Writing R Extensions: 1.2.1 Using Makevars, is a variant of Make that is unique to R. Many of the variables you have listed are called implicit variables. The meaning is given as:

隐式规则告诉 make 如何使用惯用的技术,这样您就不必在要使用它们时详细指定它们.

Implicit rules tell make how to use customary techniques so that you do not have to specify them in detail when you want to use them.

这些隐式变量规定应该使用什么编译器以及什么可用选项.

These implicit variables dictate what compiler should be used and what options are available.

R 中,我们关心以下默认编译器选项:

Within R, we care about the following default compiler options:

CC 用于编译 C 程序的程序;默认抄送".

CC Program for compiling C programs; default ‘cc’.

CXX 用于编译 C++ 程序的程序;默认'g++'.

CXX Program for compiling C++ programs; default ‘g++’.

CPP 用于运行 C 预处理器的程序,结果为标准输出;默认'$(CC) -E'.

CPP Program for running the C preprocessor, with results to standard output; default ‘$(CC) -E’.

FC 用于编译或预处理 Fortran 和 Ratfor 程序的程序;默认‘f77’.

FC Program for compiling or preprocessing Fortran and Ratfor programs; default ‘f77’.

下一组值详细说明编译器应该使用什么选项.通常,所有这些选项的默认值都是空字符串.

The next set of values details what options should be used by the compiler. In general, the default values for all of these options is an empty string.

CFLAGS 提供给 C 编译器的额外标志.

CFLAGS Extra flags to give to the C compiler.

CXXFLAGS 提供给 C++ 编译器的额外标志.

CXXFLAGS Extra flags to give to the C++ compiler.

CPPFLAGS 提供给 C 预处理器和使用它的程序(C 和 Fortran 编译器)的额外标志.

CPPFLAGS Extra flags to give to the C preprocessor and programs that use it (the C and Fortran compilers).

FFLAGS 提供给 Fortran 编译器的额外标志.

FFLAGS Extra flags to give to the Fortran compiler.

LDFLAGS 在编译器应该调用链接器时提供给编译器的额外标志,ld",例如 -L.应添加库 (-lfoo)改为 LDLIBS 变量.

LDFLAGS Extra flags to give to compilers when they are supposed to invoke the linker, ‘ld’, such as -L. Libraries (-lfoo) should be added to the LDLIBS variable instead.

LDLIBS 当编译器应该调用链接器ld"时,给它们提供的库标志或名称.LOADLIBES 已被弃用(但仍然支持)替代 LDLIBS.非库链接器标志,例如作为 -L,应该进入 LDFLAGS 变量.

LDLIBS Library flags or names given to compilers when they are supposed to invoke the linker, ‘ld’. LOADLIBES is a deprecated (but still supported) alternative to LDLIBS. Non-library linker flags, such as -L, should go in the LDFLAGS variable.

现在,R 根据不同的 C++ ISO 标准定义了额外"变体.这些变体在 R Administration 中给出:第 2.7.2 节 C++ 支持R 管理:B.7 节编译和加载标志

Now, R defines "extra" variants in terms of different C++ ISO standards. These variants are given in the R Administration: Section 2.7.2 C++ Support and R Administration: Section B.7 Compile and load flags

CXX98CXX98STDCXX98FLAGSCXX98PICFLAGS

CXX98 CXX98STD CXX98FLAGS CXX98PICFLAGS

CXX11CXX11STDCXX11FLAGSCXX11PICFLAGS

CXX11 CXX11STD CXX11FLAGS CXX11PICFLAGS

CXX14CXX14STDCXX14FLAGSCXX14PICFLAGS

CXX14 CXX14STD CXX14FLAGS CXX14PICFLAGS

CXX17CXX17STDCXX17FLAGSCXX17PICFLAGS

CXX17 CXX17STD CXX17FLAGS CXX17PICFLAGS

<小时>

说了这么多,让我们来解决第一个问题:


Having said this, let's tackle the first question:

CXXCXX98CXX11CXX14是什么关系?

CXX 是要使用的通用编译器选项.同时,R 定义了额外的 CXX 选项,以根据检测到的编译标准使用.也就是说,如果 -std=c++98(CXX98 语言规范)由 CXX_STD 设置,则与 CXX98 关联的编译器 使用.类似地,对于CXX11CXX14,遵循相同的逻辑.参见 Rcpp Gallery:使用 RcppC++11、C++14 和 C++17 了解更多详情.

CXX is the general compiler option to use. Meanwhile, R defines additional CXX options to use depending on the detected compilation standard. That is, if -std=c++98 (CXX98 language spec) set by CXX_STD, then the compiler associated with CXX98 is used. Similarly, for CXX11 and CXX14, the same logic follows. See Rcpp Gallery: Using Rcpp with C++11, C++14 and C++17 for more details.

如果已经隐含了 C++11,例如 CXX11STD = -std=c++11 是什么意思?是否在选择 -std=c++11-std=gnu++11 之间?出于可移植性的原因,通常应该避免 -std=gnu++11 吗?

What is the meaning of, e.g., CXX11STD = -std=c++11 if C++11 is already implied? Is it between choosing -std=c++11 and -std=gnu++11? Should -std=gnu++11 generally be avoided for portability reasons?

CXX11STD 的意思是为 C++11 编译确定合适的语言标准.之所以存在这个选项,仅仅是因为如果 R 选择合适的 C++11 编译选项的版本对编译器不正确,您可以更改它.之所以存在这种情况,是因为每个编译器可能定义的 C++11 支持与下一个略有不同,如 R 安装和管理:2.7.2 C++ 支持:

The meaning of CXX11STD is to determine the appropriate language standard for C++11 compile. This option exists simply because if R's version of selecting the appropriate C++11 compile option is incorrect for the compiler, you can change it. The reason this exists is because each compiler may define C++11 support slightly differently than the next as indicate in R Installation and Administration: 2.7.2 C++ Support:

可能是 [脚注 13] 没有适合 C++11 支持的标志,在这种情况下,可以为 CXX11 及其相应标志选择不同的编译器.

It may be [Footnote 13] that there is no suitable flag for C++11 support, in which case a different compiler could be selected for CXX11 and its corresponding flags.

脚注 13:

这适用于较早版本的 g++,例如 4.2.1,也适用于 Solaris 编译器 CC 的常用版本.

This is true for earlier versions of g++ such as 4.2.1, and also for commonly-used versions of the Solaris compiler CC.

有关 gcc 批准的语言标准的详细信息,请参阅 GCC 手册:3.4控制 C 方言的选项.此外,有关在包中将 C++11 与 R 结合使用的详细信息,请参阅 编写 R 扩展:第 1.2.4 节使用 C++11 代码.

For details on the language standards approved by gcc see GCC Manual: 3.4 Options Controlling C Dialect. Also, for details on using C++11 with R in a package see Writing R Extensions: Section 1.2.4 Using C++11 Code.

通常,我会避免显式设置此变量.如果您必须显式设置此变量,我建议使用 -std=c++11,因为大多数编译器都支持此声明.

Generally, I would avoid explicitly setting this variable. If you must explicitly set this variable, I would recommend going with -std=c++11 as a majority of compilers support this declaration.

CXXSTDCXXFLAGS 的标志是否可以不只是添加到 CXX 中,这样前三行减少为 CXX= g++ -std=c++11 -fsanitize=undefined,address -fno-omit-frame-pointer.显式指定 CXXSTDCXXFLAGS 有什么好处?

Could the flags for CXXSTD and CXXFLAGS not just be added to CXX, such that the first three lines reduce to CXX = g++ -std=c++11 -fsanitize=undefined,address -fno-omit-frame-pointer. What is the advantage in explicitly specifying CXXSTD and CXXFLAGS?

有可能吗?是的.这样对吗?号

Is it possible? Yes. Is it right? No.

当我们可以只拥有一个变量时,为什么要三个每个变量都有自己的目标?

Why have three variables each with their own goal when we could simply just have one?

可变工作流的优势为不同的生产线提供了不同的角色.这允许快速理解编译选项的能力.因此,与在一行上塞进一个变量(终端宽度为 80)相比,它更容易理解.

The advantages of a three variable workflow provide different lines each with a distinct role. This allows for the ability to quickly understand the compilation option. Thus, it is much more straight forward to grok when compared it being crammed into one variable on one line (with terminal width of 80).

例如

CXX = g++ -std=c++11 -fsanitize=undefined,address -fno-omit-frame-pointer

对比

CXX = g++ 
CXX11STD = -std=c++11
CXXFLAGS = -fsanitize=undefined,address -fno-omit-frame-pointer

此外,如 编写 R 扩展:第 1.2.4 节使用 C++11 代码.这只是为了确保 R 将包注册为需要 C++xy.另一种方法是在DESCRIPTION 文件中写入属性SystemRequirements: C++xy,其中xy 表示年份.

Furthermore, you should opt for CXX_STD over CXXSTD when packaging as shown in Writing R Extensions: Section 1.2.4 Using C++11 Code. This is simply to ensure that R registered the package as requiring C++xy. The alternative is to write in the DESCRIPTION file the attribute SystemRequirements: C++xy, where xy denotes the year.

CXX_STD = CXX11 如何工作?CXX11 与 ~/.R/Makevars 中的 CXX11 有什么关系?

How does CXX_STD = CXX11 work? How is CXX11 here related to CXX11 in ~/.R/Makevars?

这将使用由 CXX11 设置的 C++11 编译器设置语言的编译和链接.通过指定 CXX11,您指定了一个 Make 中的变量将用于编译配方下的文件:

This sets the compilation and linking for the language to be done with the C++11 compiler set by CXX11. By specifying CXX11, you are specifying a variable in Make that will be used to compile the file under the recipe:

$(OBJCXX) $(ALL_CPPFLAGS) $(ALL_OBJCXXFLAGS) -c $< -o $@

其中 $(OBJCXX)CXX$(ALL_CPPFLAGS)$(R_XTRA_CPPFLAGS) $(PKG_CPPFLAGS) 给出$(CLINK_CPPFLAGS) $(CPPFLAGS)$(ALL_OBJCXXFLAGS)$(PKG_OBJCXXFLAGS) $(CXXPICFLAGS) $(SHLIB_CXXFLAGS) $(OBJCXXFLAGS).

where $(OBJCXX) is CXX, $(ALL_CPPFLAGS) is given by $(R_XTRA_CPPFLAGS) $(PKG_CPPFLAGS) $(CLINK_CPPFLAGS) $(CPPFLAGS), and $(ALL_OBJCXXFLAGS) has $(PKG_OBJCXXFLAGS) $(CXXPICFLAGS) $(SHLIB_CXXFLAGS) $(OBJCXXFLAGS) .

以上如下/R/Makeconf.in.但是,例程可能是 /m4/R.

CXXFLAGSPKG_CXXFLAGS 之间的关系是什么(我的例子中没有包含)?

What is the relationship between CXXFLAGS and PKG_CXXFLAGS (not included in my example)?

这两个都指定了编译器的编译标志.它们在 Makevars 中的编写顺序是不同的.特别是,我们有CXXFLAGS放在之后 PKG_CXXFLAGS.最正确的选项总是被使用.因此,CXXFLAGS优先于PKG_CXXFLAGS.

Both of these specify the compiler's compilation flags. The order in which they are written in the Makevars is different. In particular, we have CXXFLAGS placed after PKG_CXXFLAGS. The right most option is always used. So, CXXFLAGS takes precedence over PKG_CXXFLAGS.

编写 R 扩展:第 5.5 节创建共享对象.

There is a brief note on PKG_* options in Writing R Extensions: Section 5.5 Creating shared objects.

以下是@Dominik 在此回复的评论部分提出的问题.

The following are questions that were asked by @Dominik in the comment section of this response.

~/.R/Makevars 中定义的变量是否全局适用于所有包的安装,而 /src/Makevars 中的变量仅适用于礼物包?

Is it correct that variables defined in ~/.R/Makevars apply globally to the installation of all packages, while variables in /src/Makevars only apply to the present package?

是的.这是准确的.~/.R/Makevars 中的变量将应用于所有包,而每个包附带的 /src/Makevars 只会影响该包的设置./src/Makevars 中的值将优先于 ~/.R/Makevars.

Yes. This is accurate. Variables within ~/.R/Makevars will apply to all packages while the /src/Makevars that ships with each package will only influence settings for that package. The values in /src/Makevars will take precedence over ~/.R/Makevars.

某些软件包可能附带 /src/Makevars.win,它提供了专门用于 Windows 环境的 Makevars 文件.

Some packages may ship with /src/Makevars.win, which provides a Makevars file specifically for the Windows environment.

现在用于软件包的编译标准是否仅通过 CXX_STD 设置,而不再由 PKG_CXXFLAGS 设置,如 gallery.rcpp.org/articles/simple-lambda 中所示-func-c++11?

Is the compilation standard used for a packages nowadays only set via CXX_STD and not any more by PKG_CXXFLAGS as shown in gallery.rcpp.org/articles/simple-lambda-func-c++11?

应该使用这两个标志的时间略有不同.特别是,CXX_STD 仅在包环境中有效.同时,与其名称相反,PKG_CXXFLAGS 会影响所有编译选项.因此,当您引用上述 Rcpp 画廊帖子时,您正在观察正在运行的独立脚本.要快速进入正确的模式,需要设置 PKG_CXXFLAGS不是CXX_STD 定义.

There is a slight difference between when these two flags should be used. In particular, CXX_STD is only operational in a package environment. Meanwhile, contrary to its name, PKG_CXXFLAGS, affects all compile options. Thus, when you quote the above Rcpp gallery post, you are observing a standalone script being run. To quickly engage the correct mode, that requires PKG_CXXFLAGS to be set and not the CXX_STD definition.

现在,请原谅我对独立使用编译选项的历史进行了简短的介绍......PKG_CXXFLAGS 的使用有点老派.事实上,R 3.4 中的首选方法是设置环境变量 USE_CXX11 = "yes".在 R 3.1 和 R 3.3 之间,标准是设置环境变量 USE_CXX1X = "yes".在这些实例之前,首选使用 PKG_CXXFLAGS ="-std=c++11".(在 Windows 上除外,它需要 PKG_CXXFLAGS ="-std=c++0x".)

Now, forgive me for going on a brief tangent on the history of standalone use compilation options.... The use of PKG_CXXFLAGS is a bit old school. In fact, the preferred approach in R 3.4 is to set the environment variable USE_CXX11 = "yes". Between R 3.1 and R 3.3, the standard was to set the environment variable USE_CXX1X = "yes". Prior to those instances, the use of PKG_CXXFLAGS ="-std=c++11" was preferred. (Except on Windows, which needed PKG_CXXFLAGS ="-std=c++0x".)

使用 CXX_STD=CXX11 那么意味着使用 CXXCXXSTDCXXFLAGS 给出的所有设置和 CXX11PICFLAGS?

Does using CXX_STD=CXX11 then mean to use all the settings given by CXX, CXXSTD, CXXFLAGS and CXX11PICFLAGS?

没有.这意味着使用以下设置的选项:

No. This means to use options set by:

CXX11CXX11STDCXX11FLAGSCXX11PICFLAGS

CXX11 CXX11STD CXX11FLAGS CXX11PICFLAGS

这篇关于了解 R 中 Makevars 文件的内容(宏、变量、~/.R/Makevars 和 pkg/src/Makevars)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆