未解决的外部符号错误 (LNK2019) ORTools

时间:2021-02-02 09:50:45

标签: c++ compiler-errors constraint-programming or-tools

我已尝试运行此代码:

#include "ortools/include/ortools/base/logging.h"
#include "ortools/include/ortools/constraint_solver/constraint_solver.h"

using namespace operations_research;
    void RunConstraintProgrammingExample() {
    // Instantiate the solver.
    Solver solver("ConstraintProgrammingExample");
    const int64 numVals = 3;

    // Define decision variables.
    IntVar* const x = solver.MakeIntVar(0, numVals - 1, "x");
    IntVar* const y = solver.MakeIntVar(0, numVals - 1, "y");
    IntVar* const z = solver.MakeIntVar(0, numVals - 1, "z");

    // Define constraints.
    std::vector<IntVar*> xyvars = { x, y };
    solver.AddConstraint(solver.MakeAllDifferent(xyvars));

    // Create decision builder to search for solutions.
    std::vector<IntVar*> allvars = { x, y, z };
    DecisionBuilder* const db = solver.MakePhase(
        allvars,
        Solver::CHOOSE_FIRST_UNBOUND,
        Solver::ASSIGN_MIN_VALUE);

    bool has_result = solver.Solve(db);
    // Check that the problem has a solution.
    if (has_result != true) {
        //LOG(FATAL) << "The problem does not have a solution!";
    }
    int count = 0;
    while (solver.NextSolution()) {
        count++;
        //LOG(INFO) << "Solution " << count << ":";
        //LOG(INFO) << "x = " << x->Value()
           // << " ; y = " << y->Value()
            //<< " ; z = " << z->Value();
    }
    //LOG(INFO) << "Number of solutions: " << count;
    //LOG(INFO) << "";
    //LOG(INFO) << "Advanced usage:";
    //LOG(INFO) << "Problem solved in " << solver.wall_time() << "ms";
}
// namespace operations_research 

int main(int argc, char** argv) {
    //google::InitGoogleLogging(argv[0]);
    //FLAGS_logtostderr = 1;
    RunConstraintProgrammingExample();
    return 0;
}

但我收到 9 个未解决的外部符号错误 (LNK2019)

错误 LNK2019 未解析的外部符号“public: __thiscall operations_research::Solver::Solver(class std::basic_string const &)” (??0Solver@operations_research @@QAE@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) 引用在函数“void __cdecl RunConstraintProgrammingExample(void)” (?RunConstraintProgrammingExample@ @YAXXZ)

我尝试将来自 ortools 的包含文件放入项目的其他包含目录中,并且我还尝试将 ortools 库放入属性中的链接器输入中。还是没用。我还尝试将其修改为 x64 put 然后我得到 200 个左右的错误。我还能尝试什么?

1 个答案:

答案 0 :(得分:2)

您需要使用 C++ 17 进行编译。

你可以看看这篇文章:

Adding OR-Tools Library to Visual Studio