Jump to content

GLPK/C-Sharp

From Wikibooks, open books for an open world

C# is a programming language, designed by Microsoft, and described by some as similar to Java [1]. An open source implementation of C# is provided by the Mono project.

C# has been standardized under the following documents:

GLPK for C#/CLI

[edit | edit source]

GLPK for C#/CLI provides a C# and common language interface (CLI) binding for GLPK. It is licensed under GPL V3. The coding is generated using SWIG.

GLPK for C#/CLI supports callback and terminal listeners. Errors occurring in the GLPK native library are thrown as C# exceptions. All functions of the GLPK API are available as methods of the GLPK object as shown in the example below:

using System;
using org.gnu.glpk;

class Program
{  
	static void Main (string[] args)
	{
		Console.WriteLine ("GLPK " + GLPK.glp_version ());
	}
}

Marshalling

[edit | edit source]

C# can call C library functions directly using marshalling. The following program invokes glp_version() to output the version of the GLPK library.

/*
 * This program demonstrates calling the GLPK library with C#.
 */
using System;
using System.Runtime.InteropServices;

public class Test {

    [DllImport ("glpk")]
    static extern IntPtr glp_version();

    public static void Main(string[] args) {
        Console.WriteLine("GLPK " + version());
    }

    public static string version() {
        return Marshal.PtrToStringAnsi(glp_version());
    }
}

To compile the program with Mono use

mcs test.cs

To compile the program with Visual C# use

csc test.cs

Visual Studio

[edit | edit source]

When GLPK is compiled with Visual Studio C++, both .dll and .lib files are created. The easiest (usual) way to the use GLPK in a Visual Studio C# project in this case is to make the C++ GLPK project a dependency for the C# project and include the .lib file.

GLPK#

[edit | edit source]

GLPK# (GLPK Sharp) provides a C# language binding for GLPK and is licensed under CeCILL version 2.

GLPK# is written in C++/CLI and would normally be statically linked to GLPK. As at July 2012, GLPK# supports most of the GLPK 4.45 API, including terminal callbacks and branch-and-cut callbacks. See the dual French/English project website for details.

References

[edit | edit source]
  1. Dormann, Scott (2007). "The history of C#". {{cite journal}}: Cite journal requires |journal= (help)