windows环境下cmake配置or-tools环境

本文详细介绍了在Windows环境下,使用CMake编译器配置Google的Or-Tools库与第三方线性优化求解器的过程。从操作系统、开发环境的选择,到具体软件的安装与设置,直至环境测试与问题解决,提供了全面的指导。
部署运行你感兴趣的模型镜像

    最近需要用第三方的开源代码,在python里面调用线性优化求解器。google公司的开源代码or-tools可以支持第三方求解器。在配置环境的过程中,遇到了各种各样的bug,虽然官方教程很规范,但缺乏对于这些报错的处理方式,所以在这里写一篇简单的教程,记录一下如何配置环境。

  • SCIP Optimization Suite

  • CPLEX

  • GLPK

  • Gurobi

====================

    第一步,需要x64位的操作系统,虽然不确定win7能不能使用,但是win10的安装是没有问题的。

    第二步,需要安装VS2019,家庭版或者企业版都可以。这里有个很重要的点,就是很多报错时需要的插件其实并不需要。在VS2019中,有一个专门用于cmake的C、C++组件,安装这个即可。另外windows10 SDK也需要安装,不然在使用cmake GUI的时候,很容易报错找不到需要的文件。(注意,使用cmake编译的过程中,对于VS版本以及安装位置有严格的验证过程,如果版本不一致,会报错。卸载VS可以使用installCleanup.exe。)

    第三步,常规的软件安装,需要安装git、python、cmake,三个文件都不大,几十MB。

    这个过程中要注意一下pip的版本号,最好更新为最新的版本。python和cmake安装完成后会自动在windows的环境变量中添加path,所以可以直接在命令行模式下运行python和make指令。

python --version;显示python版本号
python -m pip --version;显示pip版本号
python -m pip install --upgrade pip;升级pip版本

    第四步,安装https://github.com/google/or-tools,下载代码的时候会遇到一个问题,速度非常慢。整个代码包一共1.6G

    解决方法是使用第三方的网站http://gg.widyun.com/进行代理,将代码链接输入后,点击后可以生成压缩文件,下载速度快很多。

    第五步,运行 x64 Native Tools Command Prompt进入命令行模式(注意不是CMD模式,这点非常重要),该命令行模式快捷方式在VS2019的MSVC组件里,进入到or-tools目录夹下(注意,不能有中文路径名,建议放在根目录下)。运行指令激活第三方插件功能。

tools\make third_party
  如果,VS2019版本一致,安装目录规范;代码下载完整,无损坏文件。这个阶段应该会顺利编译完成。速度非常慢,因为从github上下载代码,耐心等待就可以了。到这一步,环境配置的第一道难关就算结束了

    第六步,安装第三方优化求解器,并在cmake配置文件中加入求解器路径。在or-tools的主目录下面有一个Makefile.local的文件,使用文本编辑器打开(推荐notepad++),在文件尾部添加:

WINDOWS_SCIP_DIR=c:/Program Files/SCIPOptSuite
 这里是以SCIP为例,等号后面的部分为你安装求解器的实际路径名。

    第七步,使用cmake编译or-tools代码

tools\make python
 第八步,测试环境配置是否正确
tools\make test_python
  之前在这一步出现报错,后来发现是某个源文件的编码模式不一致,仍然使用的ASCII编码,而不是Unicode。在ASCII中的“?”变成了Unicode的“□”。后来单独把文件复制出来,转换编码后修复该bug。到test_python完成,环境配置的第二道难关就算结束了

    如果测试编译不通过,可以使用指令清除文件重新编译。

tools\make clean_third_party

tools\make third_party

tools\make python

    第九步,将编译完成的第三方优化求解器代码加载到python的环境中,使用如下指令:

tools\make install_python
  通过该指令,包含第三方优化求解器的or-tools代码加载到python环境中,可以使用import的方式进行调用。

    至此,在windows环境下,使用cmake编译器编译第三方优化求解器到python开发环境的配置就完成了。

==========================

我是龙翔可乐

一个喜欢摄影的计算机老师

您可能感兴趣的与本文相关的镜像

Python3.9

Python3.9

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

[main] 正在配置项目: hello_world [proc] 执行命令: D:\Application\cmake\bin\cmake.EXE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=D:\Application\esp_idf_toos\v5.4.1_tools\tools\xtensa-esp-elf\esp-14.2.0_20241119\xtensa-esp-elf\bin\xtensa-esp-elf-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=D:\Application\esp_idf_toos\v5.4.1_tools\tools\xtensa-esp-elf\esp-14.2.0_20241119\xtensa-esp-elf\bin\xtensa-esp-elf-g++.exe -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE --no-warn-unused-cli -SD:/AppData/ESP-32/hello_world -Bd:/AppData/ESP-32/hello_world/build -G Ninja [cmake] Not searching for unused variables given on the command line. [cmake] -- IDF_TARGET not set, using default target: esp32 [cmake] -- Found Git: C:/Program Files/Git/cmd/git.exe (found version "2.47.1.windows.1") [cmake] -- The C compiler identification is GNU 14.2.0 [cmake] -- The CXX compiler identification is GNU 14.2.0 [cmake] -- The ASM compiler identification is GNU [cmake] -- Found assembler: D:/Application/esp_idf_toos/v5.4.1_tools/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/xtensa-esp32-elf-gcc.exe [cmake] -- Detecting C compiler ABI info [cmake] -- Detecting C compiler ABI info - done [cmake] -- Check for working C compiler: D:/Application/esp_idf_toos/v5.4.1_tools/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/xtensa-esp32-elf-gcc.exe - skipped [cmake] -- Detecting C compile features [cmake] -- Detecting C compile features - done [cmake] -- Detecting CXX compiler ABI info [cmake] -- Detecting CXX compiler ABI info - done [cmake] -- Check for working CXX compiler: D:/Application/esp_idf_toos/v5.4.1_tools/tools/xtensa-esp-elf/esp-14.2.0_20241119/xtensa-esp-elf/bin/xtensa-esp32-elf-g++.exe - skipped [cmake] -- Detecting CXX compile features [cmake] -- Detecting CXX compile features - done [cmake] -- git rev-parse returned 'fatal: not a git repository (or any of the parent directories): .git' [cmake] -- Could not use 'git describe' to determine PROJECT_VER. [cmake] -- Building ESP-IDF components for target esp32 [cmake] -- Checking Python dependencies... [cmake] Python requirements are satisfied. [cmake] Constraint file: D:\Application\esp_idf_toos\v5.4.1_tools\espidf.constraints.v5.4.txt [cmake] Requirement files: [cmake] - D:\Application\esp-idf\v5.4.1\esp-idf\tools\requirements\requirements.core.txt [cmake] Python being checked: D:/Application/esp_idf_toos/v5.4.1_tools/python_env/idf5.4_py3.11_env\Scripts\python.exe [cmake] CMake Error at D:/Application/esp-idf/v5.4.1/esp-idf/tools/cmake/build.cmake:610 (message): [cmake] [cmake] D:\Application\esp_idf_toos\v5.4.1_tools\tools\idf-python\3.11.2\python.exe: [cmake] Error while finding module specification for [cmake] 'idf_component_manager.prepare_components' (ModuleNotFoundError: No module [cmake] named 'idf_component_manager') [cmake] [cmake] Call Stack (most recent call first): [cmake] D:/Application/esp-idf/v5.4.1/esp-idf/tools/cmake/project.cmake:717 (idf_build_process) [cmake] CMakeLists.txt:6 (project) [cmake] [cmake] [cmake] -- Configuring incomplete, errors occurred! [proc] 命令“D:\Application\cmake\bin\cmake.EXE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=D:\Application\esp_idf_toos\v5.4.1_tools\tools\xtensa-esp-elf\esp-14.2.0_20241119\xtensa-esp-elf\bin\xtensa-esp-elf-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=D:\Application\esp_idf_toos\v5.4.1_tools\tools\xtensa-esp-elf\esp-14.2.0_20241119\xtensa-esp-elf\bin\xtensa-esp-elf-g++.exe -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE --no-warn-unused-cli -SD:/AppData/ESP-32/hello_world -Bd:/AppData/ESP-32/hello_world/build -G Ninja”已退出,代码为 1
05-29
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值