GLPK/C-Sharp

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

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:

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 unsafe 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 -unsafe test.cs

To compile the program with Visual C# use

csc /unsafe test.cs

GLPK#[edit]

GLPK# (GLPK Sharp) provides C# language bindings 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.

Visual C++[edit]

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

References[edit]