0

I copied the C++ code from the TMB tutorial page

#include <TMB.hpp>                                // Links in the TMB libraries

template<class Type>
Type objective_function<Type>::operator() ()
{
    DATA_VECTOR(x);                                 // Data vector transmitted from R
    PARAMETER(mu);                                  // Parameter value transmitted from R
    PARAMETER(sigma);                               //                 

    Type f;                                         // Declare the "objective function" (neg. log. likelihood)
    f = -sum(dnorm(x,mu,sigma,true));               // Use R-style call to normal density

    return f;
}

and saved it as tutorial.cpp. I then tried to compile it per the instructions and got:

> compile("tutorial.cpp")
Error in compile("tutorial.cpp") : Compilation failed
In addition: Warning messages:
1: In readLines(file) : incomplete final line found on 'tutorial.cpp'
2: running command 'make -f ".../R/R-33~1.2/etc/x64/Makeconf" -f "C:/PROGRA~1/R/R-33~1.2/share/make/winshlib.mk" -f "...\AppData\Local\Temp\RtmpqgV7xA\file2d987ade32f3" SHLIB_LDFLAGS='$(SHLIB_CXXLDFLAGS)' SHLIB_LD='$(SHLIB_CXXLD)' SHLIB="tutorial.dll" WIN=64 TCLBIN=64 OBJECTS="tutorial.o"' had status 127 

In addition, in the RStudio editor window I can see a cross next to the line Type objective_function<..., with hover over error:

Variable templates are a C++1y extension
expected ';' at end of declaration
expected unqualified-id

What should I do to fix it so that the tutorial function compiles?

Session info:

R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] TMB_1.7.8

loaded via a namespace (and not attached):
[1] Matrix_1.2-8     tools_3.3.2      grid_3.3.2    lattice_0.20-34 
Wave
  • 1,216
  • 1
  • 9
  • 22
Alex
  • 15,186
  • 15
  • 73
  • 127
  • Do you mean TMB rather than AMB?? Results of `sessionInfo()` would be helpful. Works for me on x86_64-apple-darwin13.4.0 (64-bit). Try `devtools::dr_devtools()` too? – Ben Bolker Mar 13 '17 at 04:40
  • I did mean TMB thanks. I may have found the solution in the TMB faq, if it works I will post here, I think it is to do with my (lack of) RTools installation. (taking a while to download the 100 mb installation) – Alex Mar 13 '17 at 04:44
  • I am getting the error: `c:/Rtools/mingw_64/bin/g++: not found`. Yet, Rtools was installed to `c:/RBuildTools`, and `> system('g++ -v') Using built-in specs. COLLECT_GCC=C:\RBUILD~1\3.4\mingw_32\bin\G__~1.EXE` works. – Alex Mar 13 '17 at 05:04
  • Having a look at: http://stackoverflow.com/questions/28376337/error-installing-and-running-rcpp now. – Alex Mar 13 '17 at 05:06

1 Answers1

2

I have solved my problem now, which came in two parts. The first was not having Rtools installed in Windows. This is mentioned here:

Why can I not build and run models in Windows, after installing TMB from CRAN? TMB on Windows requires Rtools. The PATH environment variable should point to the Rtools 'make' and 'gcc', and no other instances of 'make' or 'gcc'.

The second problem was that after installing Rtools I still could not find the compile, despite verifying that I could use it from the command line following these instructions:

In some cases this PATH might be C:\RBuildTools... Further, we can check if g++ can be really called from R. For example, we can see the version of gcc in R as follows.

> system('g++ -v')
Using built-in specs.
COLLECT_GCC=c:\Rtools\GCC-46~1.3\bin\G__~1.EXE
COLLECT_LTO_WRAPPER=c:/rtools/gcc-46~1.3/bin/../libexec/gcc/i686-w64-mingw32/4.6.3/lto-wrapper.exe
Target: i686-w64-mingw32
Configured with: /data/gannet/ripley/Sources/mingw-test3/src/gcc/configure --host=i686-w64-mingw32 --build=x86_64-linux-gnu --target=i686-w64-mingw32 --with-sysroot=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/mingw32 --prefix=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/mingw32 --with-gmp=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/prereq_install --with-mpfr=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/prereq_install --with-mpc=/data/gannet/ripley/Sources/mingw-test3/mingw32mingw32/prereq_install --disable-shared --enable-static --enable-targets=all --enable-languages=c,c++,fortran --enable-libgomp --enable-sjlj-exceptions --enable-fully-dynamic-string --disable-nls --disable-werror --enable-checking=release --disable-win32-registry --disable-rpath --disable-werror CFLAGS='-O2 -mtune=core2 -fomit-frame-pointer' LDFLAGS=
Thread model: win32
gcc version 4.6.3 20111208 (prerelease) (GCC)

> system('where make')
c:\Rtools\bin\make.exe

The error I received was:

> compile('tutorial.cpp')
c:/Rtools/mingw_64/bin/g++  
...
c:/Rtools/mingw_64/bin/g++: not found

This suggested that the path was being hard coded somewhere. Following the instructions for using alternative gcc compilers, I discovered that the path was hard coded in my Makeconf file located at: "C:\Program Files\R\R-3.3.2\etc\x64\Makeconf"

I then commented out the old line and replaced it with the right path:

# BINPREF ?= c:/Rtools/mingw_64/bin/
BINPREF ?= C:/RBuildTools/3.4/mingw_64/bin/

After this the compilation works without a restart.

Alex
  • 15,186
  • 15
  • 73
  • 127