8

I just updated Gurobi to version 7.5.1 on Linux (Ubuntu)--this is the newest version available.

Problem Any time I try to compile any code that uses Gurobi--for example, the examples included in /opt/gurobi751/linux64/examples, I just get a string of undefined reference errors (e.g. undefined reference to GRBModel::set(...)). I would consider the problem to be solved if I can go to the directory /opt/gurobi751/linux64/examples/build and run the command make run_diet_c++ and have it compile and run.

Attempted Fixes

(1) I have set $GUROBI_HOME in my .bashrc file--it points to the correct directory. $LD_LIBRARY_PATH and $PATH have been updated as well. They all point to the correct directories.

(2) I have a valid Gurobi license. If I write a .lp file and run it like gurobi_cl model.lp, it runs correctly. Running gurobi_cl --version gives the expected output (i.e. version 7.5.1).

(3) If I try to compile the C version (with make run_diet_c) everything works as expected.

More Information

I created the following test file in my home directory:

#include <stdio.h>
#include "gurobi_c++.h"

int main(int argc, char **argv)
{
    GRBVar x;
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

I then compile using g++ with the following command:

g++ -Wall test.cpp -o executable -I/opt/gurobi751/linux64/include -L/opt/gurobi751/linux64/lib -lgurobi_c++ -lgurobi75

This compiles and runs without complaining. However, I tried this example:

#include <stdio.h>
#include "gurobi_c++.h"

int main(int argc, char **argv)
{
    GRBEnv* env = 0;
    try {
        env = new GRBEnv();
        GRBModel model = GRBModel(*env);
        model.addVar(0, 1.0, 1.0, GRB_CONTINUOUS, "TheVar");
        model.update();
        model.optimize();
    } catch (...) {
        std::cout << "Exception during optimization" << std::endl;
    }

    delete env;

    return 0;
}

and compiled it with the same command, and it failed. So it seems like the include statement is working fine, but somehow it's not linking to the library correctly?

Please let me know if more information is needed. Also, if it's not clear, I don't know very much about the compilation and linking process, which is probably hindering me here.

David M.
  • 381
  • 3
  • 10
  • What Ubuntu release? Any changes from the default C++ compiler? Do the C examples compile and run from the Makefile? – Greg Glockner Oct 16 '17 at 22:20
  • @GregGlockner Ubuntu 16.04 LTS. Just using standard `g++` compiler with the flags that come in the sample makefile. The C examples do *not* compile and run either. – David M. Oct 16 '17 at 22:21
  • @GregGlockner More detail about C example: when I try I get `error while loading shared libraries: libgurobi75.so: cannot open shared object file: No such file or directory`. However, (1) `libgurobi75.so` is in the `include` directory, and (2) it still produces an executable that runs correctly...!! – David M. Oct 16 '17 at 22:24
  • Looks like you need to set the environment variables - see http://www.gurobi.com/documentation/current/quickstart_linux/software_installation_guid.html – Greg Glockner Oct 16 '17 at 22:41
  • @GregGlockner Those are all set as well. I will update my question to include this information. – David M. Oct 16 '17 at 22:56

1 Answers1

10

You need to compile the libgurobi_c++ for your g++ version.

First, go to the folder

cd /opt/gurobi751/linux64/src/build/

make

Now, you need copy the compiled file to lib folder:

cp libgurobi_c++.a ../../lib/

You will compile and run.

Grisotto
  • 600
  • 6
  • 13
  • 1
    I had forgotten about this question. Indeed, my g++ had been updated to version 5 without my realizing, and this was causing the issue. – David M. Feb 20 '18 at 02:19
  • 2
    In recent versions of gurobi (version 8), `libgurobi_c++.a` is a symlink. The above solution overwrites this symlink with an actual file. I'd recommend to do the following instead: `cd /opt/gurobi751/linux64/src/build/`, `make`, get the version of g++: `g++ --version`. Let's say your g++ version is 7.4. Rename and move `libgurobi_c++.a` to `../../libgurobi_g++7.4.a`: `mv libgurobi_c++.a ../../libgurobi_g++7.4.a`. `cd ../../lib/`. Finally, redirect the symlink: `ln -sf ./libgurobi_g++7.4.a libgurobi_c++.a`. – Joris Kinable Aug 20 '19 at 08:44