GLPK/Compiling with the GLPK library

From Wikibooks, open books for an open world
Jump to: navigation, search

This page shows how to create trivial programs which link to the GLPK library. The intention is to show users how to get started with the GLPK API.


Compiling with GCC[edit]

The following example shows how to compile a minimal C language program using the GLPK library and the GNU GCC compiler. The program outputs the GLPK version number.

On Linux or Mac OS X, open a console and create the source file test.c by typing (or use a text editor if you prefer):

cat << EOF > test.c
#include "stdio.h"
#include "glpk.h"

int main(int argc, const char *argv[])
{
    printf("GLPK Version: %s\n", glp_version());
    return 0;
}
EOF

Compile and link this program with the following command:

gcc test.c -lglpk -o test

Then execute the application using:

./test

Compiling with Visual Studio C++[edit]

The following example shows how to compile a minimal C language program using the GLPK library and Microsoft Visual Studio C++ 2008 (the C code and the necessary setup steps also work with Visual Studio C++ 2010). The program outputs the GLPK version number.

Create a new project of type "Win32 Console Application" with the name TESTGLPK. Then edit source file TESTGLPK.cpp:

// TESTGLPK.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include "stdio.h"
#include "glpk.h"

int _tmain(int argc, _TCHAR* argv[])
{
    printf("%s\n", glp_version());
    printf("Press Enter\n");
    getchar();
    return 0;
}

Then edit the properties of project TESTGLPK:

  • in Configuration Properties — C/C++: set "Additional Include Directories" to the path with glpk.h, perhaps: "C:\Program Files\glpk\glpk-4.45\include"
  • in Configuration Properties — Linker — Input: set "Additional Dependencies" to the GLPK library, perhaps: "C:\Program Files\glpk\glpk-4.45\w32\glpk_4_45.lib"

The GLPK DLL (e.g. glpk_4_47.dll) has to be in a spot where MS Windows will find it upon execution of the TESTGLPK application. Suitable places include the directory where the TESTGLPK executable is located in, or the Windows system directory.[1] For some screenshots, see Trapp (2009).[2]

Compiling with Qt[edit]

Qt is a cross platform application and UI framework. Qt is published under a LGPL license.

The following example shows how to compile a minimal Qt program which uses the GLPK library. The program outputs the GLPK version number.

Open QT Creator and create a new QT console application with the project name qtglpk. Then add the following lines (assuming GLPK version 4.45) to the project file qtglpk.pro:

LIBS        += -L"C:\Program Files\glpk\glpk-4.45\w32" -lglpk_4_45
INCLUDEPATH += "C:\Program Files\glpk\glpk-4.45\include"

You may need to adjust the paths and the library name to suit your installation.

Then change the text of main.cpp to:

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

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

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

Finally, press CTRL + R to build and run the application.

References[edit]