Using IBM ILOG CPLEX Optimizers with Microsoft Visual C++
This file describes how to use Microsoft Visual Studio to:
- build and run the C and C++ examples delivered with the CPLEX® Optimizers. (See dotnet.html for the C# and VB.NET examples.)
- create a C or C++ project and link the target with CPLEX.
CPLEX Optimizers installation folder
Throughout this document, the CPLEX installation folder is referred to as
, and the Concert Technology installation folder is referred to as
.
For example, if CPLEX Studio has been installed in the folder C:\Program Files\IBM\ILOG\CPLEX_Studio125
references to\include
represent C:\Program Files\IBM\ILOG\CPLEX_Studio125\cplex\include
. Concert Technology is located in C:\Program Files\IBM\ILOG\CPLEX_Studio125\concert
.
Libraries
The Concert Technology libraries are delivered in four static formats for Visual Studio 2008 (
x86_windows_vs2008
or
x64_windows_vs2008
), so that a static executable can be linked with libcmt.lib msvcrt.lib, libcmtd.lib, or msvcrtd.lib. These four formats use the standard template library (STL) and are compiled using the namespace
std
. Similarly, for Visual Studio 2010(
x86_windows_vs2010
or
x64_windows_vs2010
), the same four formats are supported: multi-threaded, multi-threaded DLL, multi-threadeded debug and multi-threaded DLL debug.
The CPLEX library is delivered as a DLL so that it can be used with any of the library formats, but there is an associated library file of each format with which to link.
Library locations
Visual Studio 2008 libraries can be found in the following directories:
- multi-threaded STL |
: \lib\x86_windows_vs2008\stat_mta\ilocplex.lib : \lib\x86_windows_vs2008\stat_mta\cplex125.lib : \lib\x86_windows_vs2008\stat_mta\concert.lib : \lib\x64_windows_vs2008\stat_mta\ilocplex.lib : \lib\x64_windows_vs2008\stat_mta\cplex125.lib : \lib\x64_windows_vs2008\stat_mta\concert.lib : These libraries must be linked with libcmt.lib . |
- multi-threaded-DLL STL |
: \lib\x86_windows_vs2008\stat_mda\ilocplex.lib : \lib\x86_windows_vs2008\stat_mda\cplex125.lib : \lib\x86_windows_vs2008\stat_mda\concert.lib : \lib\x64_windows_vs2008\stat_mda\ilocplex.lib : \lib\x64_windows_vs2008\stat_mda\cplex125.lib : \lib\x64_windows_vs2008\stat_mda\concert.lib : These libraries must be linked with msvcrt.lib . |
- multi-threaded STL debug |
: \lib\x86_windows_vs2008\stat_mtd\ilocplex.lib : \lib\x86_windows_vs2008\stat_mtd\cplex125.lib : \lib\x86_windows_vs2008\stat_mtd\concert.lib : \lib\x64_windows_vs2008\stat_mtd\ilocplex.lib : \lib\x64_windows_vs2008\stat_mtd\cplex125.lib : \lib\x64_windows_vs2008\stat_mtd\concert.lib : These libraries must be linked with libcmtd.lib . |
- multi-threaded-DLL STL debug |
: \lib\x86_windows_vs2008\stat_mdd\ilocplex.lib : \lib\x86_windows_vs2008\stat_mdd\cplex125.lib : \lib\x86_windows_vs2008\stat_mdd\concert.lib : \lib\x64_windows_vs2008\stat_mdd\ilocplex.lib : \lib\x64_windows_vs2008\stat_mdd\cplex125.lib : \lib\x64_windows_vs2008\stat_mdd\concert.lib : These libraries must be linked with msvcrtd.lib . |
Visual Studio 2010 libraries can be found in the following directories:
- multi-threaded STL |
: \lib\x86_windows_vs2010\stat_mta\ilocplex.lib : \lib\x86_windows_vs2010\stat_mta\cplex125.lib : \lib\x86_windows_vs2010\stat_mta\concert.lib : \lib\x64_windows_vs2010\stat_mta\ilocplex.lib : \lib\x64_windows_vs2010\stat_mta\cplex125.lib : \lib\x64_windows_vs2010\stat_mta\concert.lib : These libraries must be linked with libcmt.lib . |
- multi-threaded-DLL STL |
: \lib\x86_windows_vs2010\stat_mda\ilocplex.lib : \lib\x86_windows_vs2010\stat_mda\cplex125.lib : \lib\x86_windows_vs2010\stat_mda\concert.lib : \lib\x64_windows_vs2010\stat_mda\ilocplex.lib : \lib\x64_windows_vs2010\stat_mda\cplex125.lib : \lib\x64_windows_vs2010\stat_mda\concert.lib : These libraries must be linked with msvcrt.lib . |
- multi-threaded STL debug |
: \lib\x86_windows_vs2010\stat_mtd\ilocplex.lib : \lib\x86_windows_vs2010\stat_mtd\cplex125.lib : \lib\x86_windows_vs2010\stat_mtd\concert.lib : \lib\x64_windows_vs2010\stat_mtd\ilocplex.lib : \lib\x64_windows_vs2010\stat_mtd\cplex125.lib : \lib\x64_windows_vs2010\stat_mtd\concert.lib : These libraries must be linked with libcmtd.lib . |
- multi-threaded-DLL STL debug |
: \lib\x86_windows_vs2010\stat_mda\ilocplex.lib : \lib\x86_windows_vs2010\stat_mda\cplex125.lib : \lib\x86_windows_vs2010\stat_mda\concert.lib : \lib\x64_windows_vs2010\stat_mda\ilocplex.lib : \lib\x64_windows_vs2010\stat_mda\cplex125.lib : \lib\x64_windows_vs2010\stat_mda\concert.lib : These libraries must be linked with msvcrtd.lib . |
Building and running CPLEX examples
The C and C++ CPLEX examples have all been gathered in one project for each type of static format (
mta
and
mda
). The instructions below use the
mta
format for the Visual Studio 2008 environment, but similar instructions apply when you use the project file for another format or with Visual Studio 2010. The related file for the
mda
format is
\examples\x86_windows_vs2008\stat_mda\examples.sln
. Be aware that the order of these instructions is important.
- Start Microsoft Visual Studio 2008.
- From the File menu, choose Open Project/Solution.
The Open Project dialog box appears.
- Select the folder
\examples\x86_windows_vs2008\stat_mta
.
- Select the
examples.sln
file and click Open.
- To build only one example (for example,
blend
):
- Select the
blend
project in the Solution Explorer window.
- From the Build menu, choose Build blend.
Wait for the completion of the building process.
- To build all of the examples:
- From the Build menu, choose Build Solution
Wait for the completion of the building process.
- To run an example (for example,
blend
):
- Open a command prompt window by running the Visual Studio 2008 Command Prompt.
In the window Visual Studio 2008 Command Prompt:
- Type
set path=%path%;\bin\x86_win32
so that cplex125.dll
is on the path.
- Type
\examples\x86_windows_vs2008\stat_mta\blend
.
- The result is then displayed. The setting of the
path
environment variable is necessary only if this folder is not already on the path
. The default installer action is to modify the path to include this folder.
Building your own project which links with CPLEX
Note:
The following information applies to the Visual C++ 2008 multi-threaded STL library. If you use another version of the library, set the
Runtime Library
option to match the library version. If you use Visual Studio 2010, these instructions apply, except that
x86_windows_vs2008
must be replaced with
x86_windows_vs2010
whenever a path name is specified.
Let's assume that you want to build a target named test.exe
and have:
- a source file named
test.cpp
which uses Concert Technology or test.c
which uses the C API of the CPLEX Callable Library;
- a folder where this file is located and which, for the sake of simplicity, we'll refer to as
.
One way to achieve that goal is to create a project named test.vcproj
as described here. Be aware that the order of instructions is important. Note that project files for VS2010 have the extension vcxproj
.
- Start Microsoft Visual Studio 2008.
- The next step is to build the
test.sln
solution.
From the File menu, select New > Project....
The New Project dialog box appears.
- In the Project types pane, select Visual C++ and Win32.
- In the Templates pane, select the Win32 Console Application icon.
- Fill in the project name (
test
).
- If necessary, correct the location of the project (to
).
- Click OK.
When the Win32 Application Wizard appears, carry out these steps:
- Click on Application Settings.
- Select Console Application as Application type.
- Make sure that Empty project is checked in Additional Options.
- Click Finish.
This step creates a solution, test
, with a single project, test
. You can see the contents of the solution by selecting Solution Explorer in theView menu.
- Now you must add your source file to the project. From the Project menu, choose Add Existing Item...
- Move to the folder
and select test.cpp
or test.c
.
- Click Open.
- Next, you have to set some options so that the project knows where to find the CPLEX and Concert include files and the CPLEX and Concert libraries.
From the Project menu, choose Properties.
The test Property Pages dialog box appears.
In the Configuration drop-down list, select Release.
Select C/C++ in the Configuration Properties tree.
- Select General:
- In the Additional Include Directories field, add the directories:
- For Debug Information Format, choose Disabled (/Zd).
- Choose No for Detect 64-bit Portability Issues. (These settings are not available in the Visual Studio 2010 IDE and can be omitted there.)
- Select Preprocessor:
- Add
IL_STD
to the Preprocessor Definitions field. This choice defines the macro IL_STD which is needed to use the STL.
- Select Code Generation:
- Set Runtime Library to Multi-threaded (/MT).
Select Linker in the Configuration Properties tree.
- Select General and then select Additional Library Directoriess. Add the files:
\lib\x86_windows_vs2008\stat_mta
\lib\x86_windows_vs2008\stat_mta
- Select Input and then select Additional Dependencies. Add the files:
cplex125.lib
ilocplex.lib
concert.lib
The latter two are necessary only if you are using Concert Technology.
Click OK to close the test Property Pages dialog box.
- Next, you have to set the default project configuration.
From the Build menu, select Configuration Manager...
- Select Release in the Active Solution Configuration drop-down list.
- Click Close.
- Finally, to build the project, from the Build menu, select Build Solution.
After completion of the compiling and linking process, the target is created. The full path of the
test.exe
is
\test\Release\test.exe
.
Remark:
From the Concert point of view, the only difference between the Win32 Release and Win32 Debug targets is:
- the
NDEBUG
macro is defined for the Win32 Release target.
- the
NDEBUG
macro is not defined for the Win32 Debug target.
For this reason, we suggested using Release in the test.sln example, even though it is not the default proposed by Visual C++. Refer to the Visual C++ Reference Manual for full information about Release and Debug configurations.
The interaction of the NDEBUG
macro and the Concert inline member functions is documented in the Concepts section of the CPLEX C++ API Reference Manual.
Building the example projects of the remote object
Building and running the remote object examples is slightly different from building other examples because the remote object examples involve additional libraries and executables. The IBM ILOG CPLEX distribution provides project files for the MPI and process transports. (Refer to the topics about the remote object in the user manual to learn more details about transports.) You can easily adapt configurations for the TCP/IP transport from the process transport configurations.
Setting up for the MPI transport
The MPI transport requires an MPI library. The IBM ILOG CPLEX distribution does not come with an MPI library; you must install that library yourself. In order to reference the installed MPI library in an easy way, create a property sheet like this:
- Click View->Property Manager.
- Right-click on the solution or project, and select "Add New Project Property Sheet"
- Enter a suitable name for the configuration; for example, "MPI Configuration", and confirm.
- Open the newly created property sheet and navigate to Common Properties > User Macro.
- Click Add Macro, and create a macro with the following data:
-
Name
-
MPIROOT
-
Value
-
The path to your MPI installation
- Set this macro as the environment variable in the build environment by checking true.
- Save the property sheet. If you want to share the same configuration between multiple projects, then save it in a central place that can be accessed from all projects.
Running the parbenders example
The IBM ILOG CPLEX distribution contains preconfigured project files for running the parbenders example with either the process transport or the MPI transport.
Running parbenders with the process transport
- Open the parbenders.process project file.
- Navigate to Project >Properties >Configuration Properties >Debugging and configure the following values:
-
Command arguments
-
-bin=cplex.exe
-
Environment
-
PATH=C:\path\to\dlls where
C:\path\to\dlls is the folder that contains the
cplex125.dll.
- Press Ctrl+F5 to run the project.
Running parbenders with the MPI transport
- Open the parbenders.mpi project file.
- Create an MPI property sheet (as for the process transport) for the project, or add an existing MPI property sheet to the project.
- Navigate to Project >Properties >Configuration Properties >Debugging and configure the following values:
-
Command
-
$(MPIROOT)\bin\miprun.exe
-
Command arguments
-
-H localhost -np 1 $(TargetFileName) : -np 1 cplex.exe -worker=mpi : -np 1 cplex.exe -worker=mpi : -np 1 cplex.exe -worker=mpi : -np 1 cplex.exe -worker=mpi : -np 1 cplex.exe -worker=mpi
-
Environment
-
PATH=;$(TargetDir);$(MPIROOT)\bin;C:\path\to\dlls where
MPIROOT is configured in the MPI property sheet and
C:\path\to\dlls is the folder that contains
cplex125.dll.
- Press Ctrl+F5 to run the project.
Running parbenders with the TCP/IP transport
There is no preconfigured project file for the TCP/IP transport. Instead, you can start from the parbenders.process configuration and make the following adjustments:
- Open a console and change directory to the folder that contains the cplex.exe binary. Enter:
cplex.exe -worker=tcpip -address=127.0.0.1:12345
to start the worker.
- Open the parbenders.process project file.
- Navigate to Project >Properties >Configuration Properties >Debugging and configure the following values:
-
Command arguments
-
-address=127.0.0.1:12345 -address=127.0.0.1:12345 -address=127.0.0.1:12345 -address=127.0.0.1:12345 -address=127.0.0.1:12345
-
Environment
-
PATH=C:\path\to\dlls where
C:\path\to\dlls is the folder that contains the
cplex125.dll.
- In the preprocessor configuration, change USE_PROCESS to USE_TCPIP.
- Press Ctrl+F5 to run the project.
Running parmipopt with the process transport
- Open the parmipopt.process solution file.
- Change the configuration type from Debug to Release.
- Select the parmipopt.process.userfunc project and build it.
- Select the parmipopt.process.master project.
- Navigate to Project >Properties >Configuration Properties >Debugging and configure the following options:
-
Command Arguments
-
-bin=cplex.exe -model=..\..\data\location.lp -machine=localhost -machine=localhost
-
Environment
-
PATH=C:\path\to\dlls;$(TargetDir) where
PATH=C:\path\to\dlls is the folder that contains
cplex125.dll.
- Press Ctrl+F5 to run the project.
Running parmipopt with the MPI transport
- Open the parmipopt.mpi solution file.
- Change the configuration type from Debug to Release.
- Create an MPI property sheet (as you did before) for the two projects, or add an existing MPI property sheet to them.
- Select the parmipopt.process.userfunc project and build it.
- Select the parmipopt.mpi.master project.
- Navigate to Project >Properties >Configuration Properties >Debugging and configure the following options:
-
Command
-
$(MPIROOT)\bin\miprun.exe
-
CommandArguments
-
-H localhost -np 1 $(TargetFileName) -model=..\..\data\location.lp : -np 1 cplex.exe -worker=mpi -userfunction=parmipopt_userfunction=REGISTER_USERFUNCTION -logfile=parmipopt0.log : -np 1 cplex.exe -worker=mpi -userfunction=REGISTER_USERFUNCTION -logfile=parmipopt1.log
-
Environment
-
PATH=;$(TargetDir);$(MPIROOT)\bin;C:\path\to\dlls where
MPIROOT is configured in the MPI property sheet and
C:\path\to\dlls is the folder that contains
cplex125.dll.
- Press Ctrl+F5 to run the project.
Running parmipopt with the TCP/IP transport
As for parbenders, there is no preconfigured project file for the TCP/IP transport because it is again easy to adapt the process transport configuration.
- Open the parmipopt.process solution file.
- Change the configuration type from Debug to Release.
- In the preprocessor configuration of both the master and the userfunc project, change USE_PROCESS to USE_TCPIP.
- Select the parmipopt.master project.
- Navigate to Project >Properties >Configuration Properties >Debugging and configure the following values:
-
Command arguments
-
-address=127.0.0.1:12345 -address=127.0.0.1:12345
-
Environment
-
PATH=C:\path\to\dlls where
C:\path\to\dlls is the folder that contains the
cplex125.dll.
- Build the parmipopt.userfunc project. (This build will create only a DLL, not an executable binary.)
- Open a console, and change directory to the folder that contains the cplex.exe binary. Enter:
set PATH=%PATH%;C:\path\to\userfunc
cplex.exe -worker=tcpip -address=127.0.0.1:12345 -userfunction=parmipopt_userfunction=REGISTER_USERFUNCTION
to start the worker. Here, C:\path\to\userfunc is the folder that contains the parmipopt_userfunction.dll created in the previous step.
- Go back to Visual Studio, and press Ctrl+F5 to run the project.