I posted this on StackOverflow yesterday, but maybe of more interest to
people here:
How to set g++ compiler flags using Rcpp and inline?
http://stackoverflow.com/q/7063265/841830
In lieu of an answer I created ~/.R/Makevars with this single line:
CXXFLAGS = -std=c++0x
That works, but then I realized that ifelse is behaving differently today!
This simple example I posted yesterday:
using namespace Rcpp;
NumericMatrix x(x_);
return wrap(ifelse(x<5,x*x,x));
returned this before:
1 4 9 16 5 6 7 8 9 10 11 12
but in c++0x mode (g++ 3.4) it returns:
0 0 0 0 5 6 7 8 9 10 11 12
Then, Christopher's reply [1] works with normal g++, but crashes with
c++0x mode (see [2] for the error). As I played around with the code I
sometimes got garbage values in the returned matrix instead of a crash.
(-std=gnu++0x behaves the same.)
I took a look at the Rcpp source and the only thing that came to mind is
that exception specifications have been deprecated but the Rcpp code is
using them everywhere. I don't see how that could explain the observed
behaviour though, so I think it must be something more obscure?
Does everyone see the problem with different C++0x compilers, and/or
later versions of g++?
Darren
[1]:
using namespace Rcpp;
NumericMatrix x(x_);
NumericVector y=ifelse(x<5,x*x,x);
y.attr("dim")=Dimension(x.nrow(),x.ncol());
return y;
[2]:
*** caught segfault ***
address 0x300000000, cause 'memory not mapped'
Traceback:
1: .Primitive(".Call")(<pointer: 0x7f36acb327e6>, x_)
2: fun(x)
3: print(fun(x))
4: eval.with.vis(expr, envir, enclos)
5: eval.with.vis(ei, envir)
6: source("test4.R")
I just tried and could _not_ replicate it. I just uncommented my standard
line with the flag in ~/.R/Makevars:
CXXFLAGS= -std=c++0x -g -O3 -Wall -pipe -pedantic -Wno-variadic-macros
Thanks Dirk.
I tried with your line and it works for me too. So I tried shortening it
and this works:
CXXFLAGS= -std=c++0x -g -O3
but this does not:
CXXFLAGS= -std=c++0x -g
This also does not work:
CXXFLAGS= -std=c++0x -g -Wall -pipe -pedantic -Wno-variadic-macros
And neither does this:
CXXFLAGS= -g -Wall -pipe -pedantic -Wno-variadic-macros
I.e. c++0x was a red herring, and the lack of -O3 is the problem. It
works with -O3, -O2, -O1 and -Os, but fails to work with -O0.
Very curious. I considered trying each of the individual optimization
flags [1], to narrow it down further, until I realized how many of them
there were.
Darren
[1]: http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
| > I just tried and could _not_ replicate it. I just uncommented my standard
| > line with the flag in ~/.R/Makevars:
| >
| > CXXFLAGS= -std=c++0x -g -O3 -Wall -pipe -pedantic -Wno-variadic-macros
|
| Thanks Dirk.
| I tried with your line and it works for me too. So I tried shortening it
| and this works:
| CXXFLAGS= -std=c++0x -g -O3
|
| but this does not:
| CXXFLAGS= -std=c++0x -g
|
| This also does not work:
| CXXFLAGS= -std=c++0x -g -Wall -pipe -pedantic -Wno-variadic-macros
|
| And neither does this:
| CXXFLAGS= -g -Wall -pipe -pedantic -Wno-variadic-macros
|
| I.e. c++0x was a red herring, and the lack of -O3 is the problem. It
| works with -O3, -O2, -O1 and -Os, but fails to work with -O0.
Thanks for doing that. In the 15+ years of Debian maintainership I have seen
similar stuff on occassion (esp years when gfortran was newer), but the
gcc/g++ are so swamped that I never heard back fromn bug reports. They do
have their system for submitting stuff so maybe you could distill something
that wouldn't require R and Rcpp?
Dirk
| Very curious. I considered trying each of the individual optimization
| flags [1], to narrow it down further, until I realized how many of them
| there were.
|
| Darren
|
| [1]: http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
|
|
| --
| Darren Cook, Software Researcher/Developer
|
| http://dcook.org/work/ (About me and my work)
| http://dcook.org/blogs.html (My blogs and articles)
| _______________________________________________
| Rcpp-devel mailing list
| Rcpp-devel at lists.r-forge.r-project.org
| https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
On Tue, Aug 16, 2011 at 4:02 AM, Darren Cook <darren at dcook.org> wrote:
I just tried and could _not_ replicate it. I just uncommented my standard
line with the flag in ~/.R/Makevars:
CXXFLAGS= -std=c++0x -g -O3 -Wall -pipe -pedantic -Wno-variadic-macros
Thanks Dirk.
I tried with your line and it works for me too. So I tried shortening it
and this works:
?CXXFLAGS= -std=c++0x -g -O3
but this does not:
?CXXFLAGS= -std=c++0x -g
This also does not work:
?CXXFLAGS= -std=c++0x -g -Wall -pipe -pedantic -Wno-variadic-macros
And neither does this:
?CXXFLAGS= -g -Wall -pipe -pedantic -Wno-variadic-macros
I.e. c++0x was a red herring, and the lack of -O3 is the problem. It
works with -O3, -O2, -O1 and -Os, but fails to work with -O0.
Very curious. I considered trying each of the individual optimization
flags [1], to narrow it down further, until I realized how many of them
there were.
Darren
[1]: http://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
We had an earlier instance of something like this. The RcppArmadillo
package defines a struct containing the version information on
Armadillo. For some reason it is not accessible and the link step
fails with -O0. With optimization everything is fine. Most peculiar
indeed.
I've worked out, and added an answer to the above. In a nutshell, set
settings$env$PKG_CXXFLAGS, where settings is the return of
getPlugin("Rcpp").
(I set the answer as "community wiki", so others can enhance the answer
if they wish.)
Darren