You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows下CMake集成OR-Tools库时出现链接错误求助

Windows下OR-Tools二进制包链接Abseil库未解析外部符号问题

按照官方指南在Windows上安装C++版OR-Tools二进制包,构建CP求解器示例程序时编译正常,但链接器报以下3个未解析外部符号错误:

  • absl::lts_20240722::ParseCommandLine(int,char * * const)
  • absl::lts_20240722::SetProgramUsageMessage(class std::basic_string_view<char,struct std::char_traits<char>>)
  • enum absl::lts_20240722::container_internal::ctrl_t const * const absl::lts_20240722::container_internal::kEmptyGroup

推测是OR-Tools或其底层依赖(如Protocol Buffers、Abseil)构建异常,因为这些缺失的函数应存在于Abseil库中,但执行命令 dumpbin /exports .\abseil_dll.dll | findstr ParseCommandLine 未找到对应导出项。

编译代码

#include <stdlib.h>

#include "absl/base/log_severity.h"
#include "absl/log/globals.h"
#include "ortools/base/init_google.h"
#include "ortools/base/logging.h"
#include "ortools/sat/cp_model.h"
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/cp_model_solver.h"
#include "ortools/sat/model.h"
#include "ortools/sat/sat_parameters.pb.h"
#include "ortools/util/sorted_interval_list.h"
#include "ortools/constraint_solver/routing_search.h"


namespace operations_research {
    namespace sat {

        void SearchAllSolutionsSampleSat() {
            CpModelBuilder cp_model;

            const Domain domain(0, 2);
            const IntVar x = cp_model.NewIntVar(domain).WithName("x");
            const IntVar y = cp_model.NewIntVar(domain).WithName("y");
            const IntVar z = cp_model.NewIntVar(domain).WithName("z");

            cp_model.AddNotEqual(x, y);

            Model model;

            int num_solutions = 0;
            model.Add(NewFeasibleSolutionObserver([&](const CpSolverResponse& r) {
                LOG(INFO) << "Solution " << num_solutions;
                LOG(INFO) << "  x = " << SolutionIntegerValue(r, x);
                LOG(INFO) << "  y = " << SolutionIntegerValue(r, y);
                LOG(INFO) << "  z = " << SolutionIntegerValue(r, z);
                num_solutions++;
                }));

            // Tell the solver to enumerate all solutions.
            SatParameters parameters;
            parameters.set_enumerate_all_solutions(true);
            model.Add(NewSatParameters(parameters));
            const CpSolverResponse response = SolveCpModel(cp_model.Build(), &model);

            LOG(INFO) << "Number of solutions found: " << num_solutions;
        }

    }  // namespace sat
}  // namespace operations_research

int main(int argc, char* argv[]) {
    InitGoogle(argv[0], &argc, &argv, true);
    absl::SetStderrThreshold(absl::LogSeverityAtLeast::kInfo);
    operations_research::sat::SearchAllSolutionsSampleSat();
    return EXIT_SUCCESS;
}

CMake配置文件

cmake_minimum_required(VERSION 3.16)
project(HelloORTools)

# Set C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_definitions(-DOR_PROTO_DLL=)

# Path to OR-Tools installation
set(ORTOOLS_DIR "C:/program_files/or-tools_x64_VisualStudio2022_cpp_v9.12.4544")

# Add executable
add_executable(hello_ortools main.cpp)

# Include and link OR-Tools
target_include_directories(hello_ortools PUBLIC "${ORTOOLS_DIR}/include")
target_link_directories(hello_ortools PUBLIC ${ORTOOLS_DIR}/lib)

set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreadedDLL")

# Link OR-Tools library
target_link_libraries(hello_ortools PRIVATE
    ortools
    abseil_dll
)

环境

  • Windows 11
  • Microsoft Visual Studio Community 2022 17.14.13

更新于2025/8/30

尝试添加以下CMake代码:

list(APPEND CMAKE_MODULE_PATH "C:/program_files/or-tools_x64_VisualStudio2022_cpp_v9.12.4544/lib/cmake/ortools/")
find_package(ortools REQUIRED)

但CMake无法找到OR-Tools的底层依赖,无法进入编译或链接阶段。下载OR-Tools源码重新尝试,仍卡在无法找到ZLIB的环节,但单独构建OR-Tools可以成功,不清楚两个CMake配置的交互逻辑。

2025/8/30进一步更新

搜索发现有其他人遇到类似错误(涉及gRPC、Protocol Buffers等库),曾怀疑路径中存在其他版本的libprotobuf.dll,但未找到相关文件,现寻求解决该链接错误的方法。


内容的提问来源于stack exchange,提问作者Dr Phil

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 10:44:54