0

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

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

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

我尝试使用带有编译的调试功能来查看导致此错误的原因 - 但我担心我的 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

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

我应该在调试代码中寻找什么以便修复这个编译错误?

提前感谢您的所有帮助。

4

0 回答 0