Windows Qt调用GLPK

系统:win10
软件:Qt5.12.4(mingw32-7.3.0 64bit), cmder
GLPK版本:4.65

GLPK是遵循GPL协议的线性规划软件库,目前已经有MATLAB、Python、Julia等多种编程语言接口,不过官方GLPK库是C语言编写的,这样方便我们在Qt中移植调用。在Windows下如果使用mingw去编译GPLK会报错,网上找到了Windows版本GLPK,地址在:

http://winglpk.sourceforge.net/
在这里插入图片描述

其实下载了这个版本使用QT5.12.4自带的mingw编译还是会报错:
在这里插入图片描述


20200922 update
在mingw环境下进行编译,可以根据错误提示修改代码,修改src/stdc.c文件,在开头定义
#define __WOE__
原因:考虑到mingw是在win环境下编译的,stdc.c文件有段内容是控制MS Windows环境下编译接口的:
在这里插入图片描述
因此开头给个定义就可以控制了


不用担心,我们关系只是成功调用glpk库而已,以上资源下载后解压,得到以下目录结构:
在这里插入图片描述

  1. 库文件路径在w32w64,感觉自己系统环境自行选择
  2. 头文件路径在src

看一下库文件夹的内容:
在这里插入图片描述
可以看到有一个动态链接库glpk_4_65.dll,这个便是后面我们在Qt中需要调用的外部库。

根据winglpk的网址我们找到了参考网址:https://en.wikibooks.org/wiki/GLPK/Compiling_with_the_GLPK_library,里面简单介绍了了glpk在不同环境下的使用,以Qt为例,我们新建一个控制台工程,在Pro文件添加以下内容:

LIBS += -L"D:\numerical_compuattion\glpk-4.65\w64" -lglpk_4_65
INCLUDEPATH += "D:\numerical_compuattion\glpk-4.65\src"

(以上路径需要根据自己解压的winglpk路径自行更改)

main.c文件内容如下:

#include <QCoreApplication>
#include <QTextStream>
#include "glpk.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QTextStream out(stdout);
    QTextStream in(stdin);

    out << "GLPK version: " << glp_version() << "\n";
    out << "Press Enter\n";
    out.flush();
    in.readLine();

    return a.exec();
}

如果可以成功编译运行,说明成功调用了gplk库,如果编译失败,请检查文件路径是否正确。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
Windows 7上安装pulp和glpk步骤: 亲测环境: Windows 6.1.7601 Service Pack 1 Build 7601 x64 Python 2.7.11 PuLP 1.6.8 GLPK 4.34 安装步骤: 1、下载PuLP安装包:前提是,已安装python2.6以及2.6以上版本,在网页(https://pythonhosted.org/PuLP/main/installing_pulp_at_home.html)上点击PuLP zipfile下载pulp包,当然,也可以在我的资源里下载 2、安装PuLP:将zipfile解压缩,并在命令行窗口中,进入解压缩的目录,然后输入命令:setup.py install 3、下载glpk安装包:在网页(https://sourceforge.net/projects/gnuwin32/files/glpk/4.34/)上,下载glpk-4.34-setup.exe(也可以在我的资源里下载),然后双击默认安装 4、按照以上步骤,安装完以后,写一个.py的脚本并运行,脚本内容: from pulp import * pulp.pulpTestAll() 然后,会看到以下类似输出结果: D:\002-Task_150524\117-17data_thesis\004-code\testPulp.py Testing zero subtraction Testing inconsistant lp solution Testing continuous LP solution Testing maximize continuous LP solution Testing unbounded continuous LP solution Testing Long Names Testing repeated Names Testing zero constraint Testing zero objective Testing LpVariable (not LpAffineExpression) objective Testing Long lines in LP Testing LpAffineExpression divide Testing MIP solution Testing MIP solution with floats in objective Testing MIP relaxation Testing feasibility problem (no objective) Testing an infeasible problem Testing an integer infeasible problem Testing column based modelling Testing dual variables and slacks reporting Testing fractional constraints Testing elastic constraints (no change) Testing elastic constraints (freebound) Testing elastic constraints (penalty unchanged) Testing elastic constraints (penalty unbounded) * Solver pulp.solvers.PULP_CBC_CMD passed. Solver pulp.solvers.CPLEX_DLL unavailable Solver pulp.solvers.CPLEX_CMD unavailable Solver pulp.solvers.CPLEX_PY unavailable Solver pulp.solvers.COIN_CMD unavailable Solver pulp.solvers.COINMP_DLL unavailable Testing zero subtraction Testing inconsistant lp solution Testing continuous LP solution Testing maximize continuous LP solution Testing unbounded continuous LP solution Testing Long Names Testing repeated Names Testing zero constraint Testing zero objective Testing LpVariable (not LpAffineExpression) objective Testing LpAffineExpression divide Testing MIP solution Testing MIP solution with floats in objective Testing MIP relaxation Testing feasibility problem (no objective) Testing an infeasible problem Testing an integer infeasible problem Testing column based modelling Testing fractional constraints Testing elastic constraints (no change) Testing elastic constraints (freebound) Testing elastic constraints (penalty unchanged) Testing elastic constraints (penalty unbounded) * Solver pulp.solvers.GLPK_CMD passed. Solver pulp.solvers.XPRESS unavailable Solver pulp.solvers.GUROBI unavailable Solver pulp.solvers.GUROBI_CMD unavailable Solver pulp.solvers.PYGLPK unavailable Solver pulp.solvers.YAPOSIB unavailable 表示已经成功安装pulp和glpk
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值