如何将 OR-Tools 链接到我的 CMake 项目?

How to linking OR-Tools to my CMake project?

下面是一个小的工作示例,说明如何 link OR-Tools 到 CMake 项目。

感谢mizux and kamilcuk的帮助。

此外,mizux最好更新文档以指定可能需要“USE_SCIP=OFF”来解决使用 FetchContent 构建时出现的错误。

解法:

CMakeLists.txt:

cmake_minimum_required(VERSION 3.14)
project(my_proj VERSION 1.0 LANGUAGES CXX)

# Build OR-tools dependencies.
set(BUILD_DEPS ON)

# Disable SCIP solver.
set(USE_SCIP OFF)

# Fetch OR-tools library and create the alias ortools::ortools.
include(FetchContent)
FetchContent_Declare(
        or-tools
        GIT_REPOSITORY https://github.com/google/or-tools.git
        GIT_TAG        master
)
FetchContent_MakeAvailable(or-tools)

# Create a main calling operations_research::BasicExample() and link the or-tools library.
add_executable(myapp main.cpp)
target_link_libraries(myapp ortools::ortools)

main.cpp:

#include "ortools/linear_solver/linear_solver.h"

namespace operations_research {

    void BasicExample() {
        // Create the linear solver with the GLOP backend.
        std::unique_ptr<MPSolver> solver(MPSolver::CreateSolver("GLOP"));

        // Create the variables x and y.
        MPVariable* const x = solver->MakeNumVar(0.0, 1, "x");
        MPVariable* const y = solver->MakeNumVar(0.0, 2, "y");

        LOG(INFO) << "Number of variables = " << solver->NumVariables();

        // Create a linear constraint, 0 <= x + y <= 2.
        MPConstraint* const ct = solver->MakeRowConstraint(0.0, 2.0, "ct");
        ct->SetCoefficient(x, 1);
        ct->SetCoefficient(y, 1);

        LOG(INFO) << "Number of constraints = " << solver->NumConstraints();

        // Create the objective function, 3 * x + y.
        MPObjective* const objective = solver->MutableObjective();
        objective->SetCoefficient(x, 3);
        objective->SetCoefficient(y, 1);
        objective->SetMaximization();

        solver->Solve();

        LOG(INFO) << "Solution:" << std::endl;
        LOG(INFO) << "Objective value = " << objective->Value();
        LOG(INFO) << "x = " << x->solution_value();
        LOG(INFO) << "y = " << y->solution_value();
    }

}

int main() {
    operations_research::BasicExample();
    return EXIT_SUCCESS;
}

输出:

$> myapp
WARNING: Logging before InitGoogleLogging() is written to STDERR
I0721 17:54:37.269460 1170927 main.cpp:17] Number of variables = 2
I0721 17:54:37.270126 1170927 main.cpp:24] Number of constraints = 1
W0721 17:54:37.273723 1170927 lp_solver.cc:163] 
******************************************************************
* WARNING: Glop will be very slow because it will use DCHECKs    *
* to verify the results and the precision of the solver.         *
* You can gain at least an order of magnitude speedup by         *
* compiling with optimizations enabled and by defining NDEBUG.   *
******************************************************************
I0721 17:54:37.277882 1170927 main.cpp:34] Solution:
I0721 17:54:37.277948 1170927 main.cpp:35] Objective value = 4
I0721 17:54:37.278002 1170927 main.cpp:36] x = 1
I0721 17:54:37.278023 1170927 main.cpp:37] y = 1

Process finished with exit code 0

几点(此处为 OR-Tools 开发):

基本上我们提供了一个别名库ortools::ortools,你应该依赖它。 来源:https://github.com/google/or-tools/blob/b37d9c786b69128f3505f15beca09e89bf078a89/cmake/cpp.cmake#L134

否则,请注意 or-tools 依赖于 abseil-cpp 并使用 C++17,因此您必须使用相同的语言方言,因为 abseil-cpp 依赖于语言版本。