使用TMB软件包在R中发布C ++代码的问题

时间:2019-06-27 01:20:56

标签: c++ r tmb

我正在尝试应用几年前由同事开发的用C ++编写的分层补偿模型。我正在尝试使用TMB软件包来编译代码,然后将该模型应用于从BLS和内部数据库中提取的一些教育数据。

但是,当我在TMB中使用编译功能时,在R版本3.6.0中运行时会得到以下内容

Error in compile("hierarchical_earnings_model.cpp") : Compilation failed

我尝试将debug函数与compile一起使用,以查看导致此错误的原因-但我的R知识太低,无法理解调试结果。

debug(compile('hierarchical_earnings_model.cpp'))
debugging in: compile("hierarchical_earnings_model.cpp")
debug: {
    if (.Platform$OS.type == "windows") {
        system.file <- function(...) {
            ans <- base::system.file(...)
            chartr("\\", "/", shortPathName(ans))
        }
    }
    if (tracesweep) 
        libtmb <- FALSE
    debug <- length(grep("-O0", flags)) && length(grep("-g", 
        flags))
    fpath <- system.file(paste0("libs", Sys.getenv("R_ARCH")), 
        package = "TMB")
    f <- paste0(fpath, "/libTMB", if (openmp) 
        "omp"
    else if (debug) 
        "dbg", ".cpp")
    libtmb <- libtmb && file.exists(f)
    if (libtmb) 
        file <- c(file, f)
    oldmvuser <- mvuser <- Sys.getenv("R_MAKEVARS_USER", 
        NA)
    if (is.na(oldmvuser)) {
        on.exit(Sys.unsetenv("R_MAKEVARS_USER"))
    }
    else {
        on.exit(Sys.setenv(R_MAKEVARS_USER = oldmvuser))
    }
    if (is.na(mvuser) && file.exists(f <- path.expand("~/.R/Makevars"))) 
        mvuser <- f
    if (!is.na(mvuser)) {
        cat("Note: Using Makevars in", mvuser, "\n")
    }
    makevars <- function(...) {
        file <- tempfile()
        args <- unlist(list(...))
        txt <- paste(names(args), args, sep = "=")
        if (!is.na(mvuser)) {
            if (file.exists(mvuser)) {
                txt <- c(readLines(mvuser), txt)
            }
        }
        writeLines(txt, file)
        Sys.setenv(R_MAKEVARS_USER = file)
        file
    }
    libname <- sub("\\.[^\\.]*$", "", basename(file[1]))
    if (safeunload) {
        valid <- c(letters[1:26], LETTERS[1:26], 0:9, "_")
        invalid <- setdiff(unique(strsplit(libname, "")[[1]]), 
            valid)
        if (length(invalid) > 0) {
            cat("Your library name has invalid characters:\n")
            print(invalid)
            cat("It is recommended to replace invalid characters by underscore.\n")
            cat("Alternatively compile with safeunload=FALSE (not recommended).\n")
            stop()
        }
    }
    if (.Platform$OS.type == "windows") {
        tr <- try(dyn.unload(dynlib(libname)), silent = TRUE)
        if (!is(tr, "try-error")) 
            cat("Note: Library", paste0("'", dynlib(libname), 
                "'"), "was unloaded.\n")
    }
    useRcppEigen <- !file.exists(system.file("include/Eigen", 
        package = "TMB"))
    useContrib <- file.exists(system.file("include/contrib", 
        package = "TMB"))
    ppflags <- paste(paste0("-I", system.file("include", 
        package = "TMB")), paste0("-I", system.file("include", 
        package = "RcppEigen"))[useRcppEigen], paste0("-I", 
        system.file("include/contrib", package = "TMB"))[useContrib], 
        "-DTMB_SAFEBOUNDS"[safebounds], paste0("-DLIB_UNLOAD=R_unload_", 
            libname)[safeunload], "-DWITH_LIBTMB"[libtmb], 
        paste0("-DTMB_LIB_INIT=R_init_", libname)[libinit], 
        "-DCPPAD_FORWARD0SWEEP_TRACE"[tracesweep])
    mvfile <- makevars(PKG_CPPFLAGS = ppflags, PKG_LIBS = paste("$(SHLIB_OPENMP_CXXFLAGS)"[openmp]), 
        PKG_CXXFLAGS = "$(SHLIB_OPENMP_CXXFLAGS)"[openmp], 
        CXXFLAGS = flags[flags != ""], ...)
    on.exit(file.remove(mvfile), add = TRUE)
    status <- .shlib_internal(file)
    if (status != 0) 
        stop("Compilation failed")
    status
}
Browse[2]> c
Error in compile("hierarchical_earnings_model.cpp") : Compilation failed

理想情况下,应该编译代码,然后在开始使用BLS数据开始对学校的随机样本进行测试之前,可以使用dyn.load加载模型。

我应该在调试代码中寻找什么,以便解决此编译错误?

预先感谢您的帮助。

0 个答案:

没有答案