Why does assert not work here?
很明显,Rcpp有自己定义NDEBUG
的习惯,即使不是我自己定义的。
m@m-X555LJ:~/wtfdir$ cat WTF.r
#!/usr/bin/Rscript
library(Rcpp)
sourceCpp("WTF.cpp")
m@m-X555LJ:~/wtfdir$ cat WTF.cpp
#ifdef NDEBUG
#error WTF I did not define this
#endif
m@m-X555LJ:~/wtfdir$ ./WTF.r
WTF.cpp:2:2: error: #error WTF I did not define this
#error WTF I did not define this
^~~~~
make: *** [WTF.o] Error 1
g++ -I"/usr/share/R/include" -DNDEBUG -I"/home/m/R/x86_64-pc-linux-gnu-library/3.5/Rcpp/include" -I"/home/m/wtfdir" -fpic -g -O2 -fdebug-prefix-map=/build/r-base-3U0YWo/r-base-3.5.3=. -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c WTF.cpp -o WTF.o
/usr/lib/R/etc/Makeconf:172: recipe for target 'WTF.o' failed
Error in sourceCpp("WTF.cpp") : Error 1 occurred building shared library.
Execution halted
m@m-X555LJ:~/wtfdir$
我链接的SO问题的答案解释了(a)禁止在上传到CRAN的包中调用assert
,因为(b) C++代码不应该停止R代码,(c)我应该抛出异常,这是由Rcpp捕获的。
但是:
NDEBUG
的情况下自行编译它以用于测试目的,然后在上传到CRAN之前定义NDEBUG
;NDEBUG
,我已经在代码中放置了相当多的assert
,并且通过std::cerr
打印诊断也包装在#ifndef NDEBUG
s中;这些显然无法工作;assert
计算繁重;< code >H221< code >H122到目前为止,我的C++代码无论如何都会损坏我的R代码,因为它崩溃了,我正在尝试调查这个问题,但我无法调查,因为我的诊断不起作用。< code >H223有没有办法让Rcpp停止定义NDEBUG
?或者我应该简单地删除所有asserts
和任何其他依赖于NDEBUG
的东西,并切换到抛出异常并停止抱怨?
发布于 2019-05-20 00:52:56
首先,如果您在R自己的Makeconf
中为NDEBUG
执行grep
(由于我如何设置.deb包,我们可以通过/etc/R
在Debian及其衍生产品上的一个方便的软链接访问它):
edd@rob:~$ grep NDEBUG /etc/R/Makeconf
R_XTRA_CPPFLAGS = -I"$(R_INCLUDE_DIR)" -DNDEBUG
ALL_CPPFLAGS = -I"$(R_INCLUDE_DIR)" -DNDEBUG $(PKG_CPPFLAGS) $(CLINK_CPPFLAGS) $(CPPFLAGS)
edd@rob:~$
根据您的问题,您可以看到它是由R而不是Rcpp
强加的。所以你的假设是错的。它在您的本地控制之下:只需在您的机器上编辑R的Makeconf
即可。您不能为您的代码的假设其他用户自动执行此操作。但根据你的问题,这似乎不是一个直接的问题。
其次,如果你想要一个特定的#define
,你可以直接定义它:
代码
#include <Rcpp.h>
// [[Rcpp::export]]
void foo() {
#ifdef DEBUG
Rcpp::Rcout << "foo: Debug mode" << std::endl;
#endif
Rcpp::Rcout << "foo: Hello, world" << std::endl;
}
#define DEBUG 1
// [[Rcpp::export]]
void bar() {
#ifdef DEBUG
Rcpp::Rcout << "bar: Debug mode" << std::endl;
#endif
Rcpp::Rcout << "bar: Hello, world" << std::endl;
}
/*** R
foo()
bar()
*/
输出
R> sourceCpp("~/git/stackoverflow/56209693/answer.cpp")
R> foo()
foo: Hello, world
R> bar()
bar: Debug mode
bar: Hello, world
R>
定义其他日志记录宏是很常见的,如DEBUG
或FATAL
或...其行为类似于assert,即使在默认情况下R(出于主要交互式工具的原因)关闭了assert
也是如此。
第三,也是最后一点,您可以遵循this question中的方法,暂时取消定义NDEBUG
以包含cassert
,然后重新定义它。这将给你assert()
并终止你的会话,正如我检查的那样。不是我使用R进行调试的方法,但是如果你真的必须这样做的话...
https://stackoverflow.com/questions/56209693
复制相似问题