title: R Makevars file to overwrite R CMD's default g++ options?
body: <p>I have this standalone C++ code
  that I'm trying to wrap in an R
  package.</p>
  <p>My problem is that I absolutely
  want it to be compiled with the
  <code>-O3</code> flag on.</p>
  <p>So in the <code>src/Makevars</code> file
  I put:</p>
  <pre><code>PKG_CPPFLAGS = -I../inst/include
  PKG_CXXFLAGS = -O3  
  CXX_STD = CXX11
  </code></pre>
  <p>and still when I install my package on my
  machine, I see:</p>
  <pre><code>g++ -std=c++0x -I/usr/share/R/include -DNDEBUG -I../inst/include -O3  -fpic  -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -D_FORTIFY_SOURCE=2 -g -c mycppfunctions.cpp -o mycppfunctions.o
  g++ -std=c++0x -shared -Wl,-Bsymbolic-functions -Wl,-z,relro -o mycppfunctions.so mycppfunctions.o -L/usr/lib/R/lib -lR
  </code></pre>
  <p>(the dreaded <code>-O2</code> flag appears to the <strong>right</strong>)</p>
  <p>so my question is: how can I overwrite the
  cpp flags used when <code>g++</code> is invoked by <code>R CMD</code>?</p>
  <h1>Edit:</h1>
  <p>Recently, in another package,  I found a way to do
  something similar for a F77 code (also in an R package).
  Basically, by adding this to the <code>Makevars</code>:</p>
  <pre><code>PKG_FFLAGS = $(FPICFLAGS) $(SHLIB_FFLAGS) 
  all: $(SHLIB)
  otherf77foo.o: otherf77foo.f
      $(F77) $(PGK_FFLAGS) -O3 -pipe -g -c -o otherf77foo.o otherf77foo.f
  </code></pre>
  <p>but I don't know how to do the same for a cpp code...</p>
  <h1>Edit2:</h1>
  <p>So, doing this is totally possible. Dirk Eddelbuettel question 'b)'  from his answer below
  guided me to the solution. So, all I had to do was to
  place this in the <code>src/Makevars</code> file:</p>
  <pre><code>mycppfoo.o: mycppfoo.cpp
      g++ -std=c++0x -I/usr/share/R/include -DNDEBUG -I../inst/include -fpic  -g -O3 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Wformat-security -Werror=format-security -D_FORTIFY_SOURCE=2 -g -c mycppfoo.cpp -o mycppfoo.o
      g++ -std=c++0x -shared -Wl,-Bsymbolic-functions -Wl,-z,relro -o mycppfoo.so mycppfoo.o -L/usr/lib/R/lib -lR
  </code></pre>
  <p>and my problem was solved!</p>