Creating a new Visual Studio project that uses CBC

Note: sample code for this tutorial can be found at This code/project assumes Cbc is located in C:\COIN\Cbc, but this is easy to change.

Before Starting

  1. I'm assuming you are using Visual Studio 2010, but the principles are the same for 2008.
  2. Get the latest version of the CBC repository.
    1. If you haven't already, get a SVN client like TortoiseSVN.
    2. Create a folder, e.g. C:\COIN
    3. In this folder, right-click and select "Checkout"
    4. Set the URL to the CBC version you want to use, e.g.
    5. Set the checkout directory to something sensible, like C:\COIN\Cbc
    6. Click OK, and CBC will be downloaded.
  3. Open up the CBC Visual Studio 2010 solution file (C:\COIN\Cbc\Cbc\MSVisualStudio\v10\Cbc.sln)
  4. Build these projects:
    1. libCbc
    2. libCbcSolver
    3. libCgl
    4. libClp
    5. libCoinUtils
    6. libOsi
    7. libOsiCbc
    8. libOsiClp
  5. If you look under C:\COIN\Cbc\Cbc\MSVisualStudio\v10\Win32\Debug, you'll see the .lib files made from each of these projects.

Create Your Project

  1. Open up VS2010 and select New Project.
  2. We want a Win32 Console Application - give it a name and location, click OK.
  3. In the wizard, click Next once, then check "Empty Project" and click Finish.
  4. Add a new C++ file, lets call it main.cpp, and add the following code (which is based on an example in the repository)
#include "OsiClp/OsiClpSolverInterface.hpp"
#include "CbcModel.hpp"
#include "CoinModel.hpp"

#include <cstdio>

int main() {
  const int numcols = 2;
  const int numrows = 1;
  double obj[] = { 1.0, 1.0}; // obj: Max x0 + x1
  // Column-major sparse "A" matrix: x0 + 2 x1 <= 3.9
  int start[] = {0, 1, 2};      // where in index columns start (?)
  int index[] = {0, 0};         // row indexs for the columns
  double values[] = {1.0, 2.0}; // the values in the sparse matrix
  double rowlb[]  = {0.0};
  double rowub[]  = {3.9};

  //          0 <= x0 <= 10 and integer
  //          0 <= x1 <= 10
  double collb[] = {0.0, 0.0};
  double colub[] = {10.0, 10.0};
  OsiClpSolverInterface model;
  model.loadProblem(numcols, numrows, start, index, values, 
                    collb, colub, obj, rowlb, rowub);
  model.setInteger(0); // Sets x0 to integer
  model.setObjSense(-1.0); // Maximise

  CbcModel solver(model);
  bool optimal = solver.isProvenOptimal();
  const double *val = solver.getColSolution();
  printf("Solution %g %g\n", val[0], val[1]);

  return 0;

Setting Project Settings

  1. The first thing we need to do is to tell VS where to find those files we included. These files can be found throughout the CBC repository. Open up your project properties, go to C/C++, General. Add the following directories to your Additional Include Directories:
    1. C:\COIN\Cbc\Osi\src\Osi
    2. C:\COIN\Cbc\Cbc\src
    3. C:\COIN\Cbc\CoinUtils?\src
    4. C:\COIN\Cbc\Clp\src
  2. Next, we are going to point VS towards those .lib files we made earlier. Under the project settings, Linker, Input page, there is an "Additional Dependencies" field. Add the following file names:
    1. libCbc.lib
    2. libCgl.lib
    3. libClp.lib
    4. libCoinUtils.lib
    5. libOsi.lib
    6. libOsiClp.lib
  3. Finally, tell VS where these library files are by going to Linker, General and adding C:\COIN\Cbc\Cbc\MSVisualStudio\v10\Win32\Debug to the "Additional Library Directories"

Testing It Worked

  1. Build your new project by pressing F7, then run with Control+F5
  2. There should be some output from Clp and Cbc, before the final line of "Solution 3 0.45"

What Next

Check out the examples in the examples directories of Cbc and Clp for all the various ways you can use these libraries!

Last modified 10 years ago Last modified on Oct 28, 2011 2:49:22 PM

Attachments (3)

Download all attachments as: .zip