Creating Xpress Solver Applications in C or C++
Topics covered in this chapter:
When creating an application that is written in the C or C++ programming language, there are two steps:
- Compile the source code into an object file.
- Link the object file and the Xpress library into an executable.
Both steps are rather different on Windows and Unix platforms, so things are explained in two different sections.
We will give instructions for compiling and linking a C application. Creating an application written in C++ is basically the same and only requires some additional settings. We will specify these settings where necessary.
In the below we assume that you have code in a file called app.c. An example could be this
/* A very simple Xpress application that solves this optimization problem: * Minimize * 3x1 + 5x2 * Subject To * 2x1 + x2 >= 3 * 2x1 + 2x2 >= 5 * x1 + 4x2 >= 4 * x1,x2 >= 0 * and displays the results. * For more detailed examples please see the examples/solver/optimizer/c * directory in your Xpress installation. */ #include <xprs.h> #include <stdio.h> int main(void) { double obj[] = { 3, 5 }; double lb[] = { 0, 0 }; double ub[] = { XPRS_PLUSINFINITY, XPRS_PLUSINFINITY }; double rhs[] = { 3, 5, 4 }; int rowind[] = { 0, 1, 2, 0, 1, 2 }; double rowcoef[] = { 2, 2, 1, 1, 2, 4 }; int start[] = { 0, 3, 6 }; double x[2]; XPRSprob prob; if ( XPRSinit("") ) { char buffer[256]; XPRSgetlicerrmsg(buffer, sizeof(buffer)); fprintf(stderr, "License not found: %s\n", buffer); return -1; } if ( XPRScreateprob(&prob) ) return -1; if ( XPRSloadlp(prob, "problem", 2, 3, "GGG", rhs, NULL, obj, start, NULL, rowind, rowcoef, lb, ub) ) return -1; if ( XPRSaddnames(prob, XPRS_NAMES_COLUMN, "x1\0x2", 0, 1) ) return -1; if ( XPRSoptimize(prob, "", NULL, NULL) ) return -1; if ( XPRSgetsolution(prob, NULL, x, 0, 1) ) return -1; printf("x1 = %f\n", x[0]); printf("x2 = %f\n", x[1]); XPRSdestroyprob(prob); XPRSfree(); return 0; }
This solves the very simple optimization problem
\begin{array}{lrcl} \text{minimize} \\ & 3 x_1 + 5 x_2 \\ \text{subject to} \\ & 2 x_1 + x_2 & \geq & 3 \\ & 2 x_1 + 2x_2 & \geq & 5 \\ & x_1 + 4x_2 & \geq & 4 \\ & x_1,\,x_2 & \geq & 0 \end{array}and displays the results.
Windows
We assume that you have the cl compiler installed. The instructions provided here probably work with other compilers (mutatis mutandis) as well, but we tested them only with the cl compiler. We also assume that you have setup your console environment for use with that compiler.
Remember that we assume that Xpress was installed into C:\xpressmp. Adjust the paths in the commands below if you installed it into a different folder.
When compiling the source file into an object file the only Xpress-specific thing the compiler needs to know is where to find the xprs.h header file. The directory that includes that file is specified using the /I option and the object file is created like so:
cl /IC:\xpressmp\include /c /Foapp.obj app.cWhen compiling a C++ application you should ask the compiler to correctly define the __cplusplus macro since the Xpress C++ header files use this macro. Since the Xpress C++ API requires C++17 it also makes sense to explicitly ask the compiler to support that standard:
cl /IC:\xpressmp\include /Zc:__cplusplus /std:c++17 /c /Foapp.obj app.cpp
Once object files are created, they have to be linked into an executable. For this the compiler (or linker) needs to know what symbols the Xpress library provides. To this end, the xprs.lib file is added to the link step:
cl /Feapp.exe app.obj C:\xpressmp\lib\xprs.libWhen creating a C++ application you also have to link the xprscxx.lib library:
cl /Feapp.exe app.obj C:\xpressmp\lib\xprscxx.lib C:\xpressmp\lib\xprs.lib
In order to run the application, you need to make sure that the Xpress libraries can be found at runtime.
One way to do this is to copy xprs.dll and xprl.dll from C:\xpressmp\bin to the same place as your application (exe file). For C++ applications you also have to copy the xprscxx.dll file.
Another option is to modify the PATH environment variable to contain the C:\xpressmp\bin directory.
Once you made the libraries available, you can run the application from the console as
app.exe
Creating an application with Visual Studio
- Create a new project of type "C++ Console Application" (choose this even if you want to create a C program).
- Make sure your appliction is configured for platform "x64" (and not "x86"). The configured platform is displayed in the top bar. If necessary, you can change it there, or from the menu bar go to "Project -> Properties" and modify the "Platform".
- Replace the default template source code file by the file above or (if your project is empty) add the above source code file to your project.
- Go to "Project -> Properties".
- In the dialog perform the following changes (if you installed Xpress into another folder than C:\xpressmp then adjust the information accordingly):
- In "Configuration Properties -> C/C++ -> General" add C:\xpressmp\include to "Additional Include Directories".
- In "Configuration Properties -> Linker -> General" add C:\xpressmp\lib to "Additional Library Directories".
- In "Configuration Properties -> Linker -> Input" add xprs.lib to "Additional Dependencies". If your source code uses the Xpress C++ header files then also add xprscxx.lib.
- If the source code uses the Xpress C++ header files then in "Configuration Properties -> C/C++ -> Command line" add /Zc:__cplusplus to the "Additional Options" section.
- At this point your configuration is complete for building your project. You can not yet run it from Visual Studio. To run your project you must make the Xpress DLLs available to the application. One way to do that is copying them to your project directory as follows:
- Select "Project -> Add Existing Item..."
- Select C:\xpressmp\bin\xprs.dll.
- Then find the xprs.dll entry in the project explorer, right click on it and select "Properties ...".
- In the dialog change the "General -> Item Type" property to "Copy file" and click "Ok".
- Do the same with C:\xpressmp\bin\xprl.dll.
- If you are using the Xpress C++ API then do the same with C:\xpressmp\bin\xprscxx.dll
- Unless you made your license globally available in some way, you have to set the XPAUTH_PATH environment variable so that Xpress can find your license. To this end, select your application in the solution explorer. Then go to "Project -> Properties -> Configuration Properties -> Debugging" and add the appropriate setting of XPAUTH_PATH to "Environment". This would be something like XPAUTH_PATH=/path/to/xpauth.xpr.
- Now you can run your project by selecting "Debug -> Start Debugging" or "Debug -> Start Without Debugging" from the menu bar.
Unix
When compiling the source file into an object file the only Xpress-specific thing the compiler needs to know is where to find the xprs.h header file. The directory that includes that file is specified using the -I option. In a console issue (remember that we assume you installed Xpress into /xpressmp, adjust the path if you installed into a different folder):
cc -I/xpressmp/include -c -o app.o app.cIf you are using the Xpress C++ API then you should use a C++ compiler instead and tell the compiler to support at least C++17:
c++ -std=c++17 -I/xpressmp/include -c -o app.o app.cpp
Once object files are created, they have to be linked into an executable. For this the compiler (or linker) needs to know where the Xpress library is located (specified via the -L option) and that is has to link with that library (specified via the -l option):
cc -L/xpressmp/lib -o app app.o -lxprs -lmIf you are using the Xpress C++ API then you also need to link libxprsccx.so and should use c++ for linking:
c++ -L/xpressmp/lib -o app app.o -lxprscxx -lxprs -lm
In order to run the application, you need to make sure that the runtime linker can find the Xpress libraries. There are different ways to do that:
Configuring the environment to find Xpress libraries
The runtime linker looks at environment variables to find libraries required by the application. The environment variable has different names on different operation systems:
- On Linux the environment variable is called LD_LIBRARY_PATH. To run the application in a shell do
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/xpressmp/lib ./app
- On MacOS the environment variable is called DYLD_LIBRARY_PATH. To run the application in a shell do
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/xpressmp/lib ./app
Note that in almost all shells you can also specify environment variables at the time you execute a command. That allows setting them without "polluting" the global namespace. For example, on Linux using bash you can do:
LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/xpressmp/lib ./app
Hard coding the library location into the binary
If you know the place at which Xpress libraries are located at runtime then you can hard-code that location into the application binary. With that the binary will run without any additional environment settings.
In order to hard-code the library location into the binary, add the following flags to the linker command (we assume you link with gcc or clang):
- Linux: -Wl,--enable-new-dtags,-rpath,/xpressmp/lib
- MacOS: -Wl,-rpath,/xpressmp/lib
If you added the above flags to the linker command, for example on Linux,
cc -L/xpressmp/lib -Wl,--enable-new-dtags,-rpath,/xpressmp/lib -o app app.o -lxprs -lm
then you can execute the application as
./app
If your application will be in the same runtime location as the libraries (in the same folder), then instead of /xpressmp/lib you can specify $ORIGIN on Linux (note that the '$' may have to be quoted) and @executable_path on MacOS. Either of the two will make the runtime linker search the folder containing the application for libraries.
Creating an application with Xcode
- Create new project using the "Command Line Tool" template, found on the "macOS" tab.
- Fill in the necessary fields, selecting "C++"" as the language.
- Replace the default template source code file by the file above.
- Click your project in the left-hand pane, open the "Build Settings" tab, and adjust the following:
- In the "Search Paths" section, add /xpressmp/include to "Header Search Paths".
- In the "Search Paths" section, add /xpressmp/lib to "Library Search Paths".
- In the "Linking - General" section, add /xpressmp/lib to "Runtime Search Paths".
- Now open the "Build Phases" tab and do the following:
- Find the "Link Binary With Libraries" section.
- Click the plus button, and in the dialog, click "Add Other" and select "Add Files".
- Browse to /xpressmp/lib and select libxprs.dylib.
- If you plan to use the Xpress C++ API, add libxprscxx.dylib in the same way.
- Unless you made your license globally available in some way, you have to set the XPAUTH_PATH environment variable so that Xpress can find your license:
- Hold the "Alt" key and click the run button in the toolbar.
- Select "Run" in the left-hand pane.
- Select the "Arguments" tab and add the appropriate setting of XPAUTH_PATH to "Environment Variables". This would be something like XPAUTH_PATH=/path/to/xpauth.xpr.
- Now you can run your project by clicking the run button in the toolbar.
Example makefiles
Xpress also ships with examples and makefiles to build them. These files are located in /xpressmp/examples/solver/optimizer/c and /xpressmp/examples/solver/nonlinear/c.
The makefiles assume that the XPRESSDIR variable is set to the installation directory of Xpress. XPRESSDIR can be set either as environment variable or as makefile variable.
In order to build an example using the provided makefiles you can either build the example in the installation or copy it and build it in a different place. We illustrate this for the tsp.c example on Linux:
- Building the example in the installation:
cd /xpressmp/lib/examples/solver/optimizer/c make XPRESSDIR=/xpressmp tsp
- Copying the example and building it in a different place:
cp /xpressmp/lib/examples/solver/optimizer/c/tsp.c . make XPRESSDIR=/xpressmp -f /xpressmp/lib/examples/solver/optimizer/c/makefile tsp
On Windows things are almost identical, you just have to use nmake instead of make and the target has to be tsp.exe instead of just tsp.
Note that building examples through the provided makefiles also gives an easy way to figure out the required compiler and linker flags: when running the makefiles, make shows the commands that are executed. From this output you can read the flags required to compile and link Xpress applications.
Linking with the static Xpress library (Linux)
On Linux the Solver library is also available as static library: libxprs-static.a. Linking with the static library reduces your application's dependencies by one. Note that the dependency on libxprl.so is still there, though.
The compilation (object file creation) step is the same as described above. The only thing that changes is the link step:
cc -L/xpressmp/lib -o app-static app.o -lxprs-static -lxprl -lm -lpthread -ldl
The generated binary will not require the libxprs.so library at runtime since the relevant parts of this library are now contained in the application binary.
Create Xpress Solver Applications in Eclipse
In order to build C/C++ applications in Eclipse, you need the CDT project. We assume you have this available in your Eclipse installation and have configured everything properly so that you can build C/C++ applications (you can build a "Hello World" program). Below we only specify the additional things you have to add to your C/C++ project configuration to make the Xpress Solver libraries available.
Remember that we assume that Xpress was installed into C:\xpressmp on Windows and /xpressmp on other platforms. Adjust the paths in the configurations below if you installed it into a different folder.
- Select your project in the Project Explorer, select "Project -> Properties"
- Go to "C/C++ General -> Paths and Symbols"
- On the "Includes" tab add /xpressmp/include
- On the "Library Paths" tab add C:\xpressmp\bin on Windows and /xpressmp/lib on other platforms
- On the "Libraries" tab add xprs and xprl. On Windows these are xprs.dll and xprl.dll located in C:\xpressmp\bin. On Linux these are libxprs.so and libxprl.so located in /xpressmp/lib. On MacOS these are libxprs.dylib and libxprl.dylib located in /xpressmp/lib. If you use the Xpress C++ API then you also have to add the xprscxx library. This works completely analogous to the libraries mentioned above. For C++ on Windows you should also add /Zc:__cplusplus and /std:c++17 to the compiler flags. For Unix platforms you should add -std=c++17 (or a higher C++ standard) to the compilation flags for C++ applications.
- Click "Apply and Close"
- Open the run configuration for your project
- Go to the "Environment" tab
- Depending on your platform, add an environment variable that points to the location of the Xpress Solver libraries:
- On Linux add LD_LIBRARY_PATH pointing to /xpressmp/lib
- On MacOS add DYLD_LIBRARY_PATH pointing to /xpressmp/lib
- On Windows add PATH pointing to /xpressmp/bin
- Unless you made your license globally available, add an environment variable XPAUTH_PATH that points to your xpauth.xpr file.
- Click "Ok"
© 2001-2025 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.