Skip to main content

Undefined references problem when compiling c++ test code

Answered

Comments

10 comments

  • Thomas Opfer
    Gurobi-versary
    Thought Leader

    As I said, please put the libraries at the end of the command. Please try

    g++ -I/media/34GB/Arquivos-de-Programas-Linux/gurobi801/linux64/include/
    -L/media/34GB/Arquivos-de-Programas-Linux/gurobi801/linux64/lib
    -o mip1_c++ mip1_c++.cpp -lgurobi_c++ -lgurobi80
    1
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Dear Alexandre,

    The explanation is literally a simple online search for "gcc linking" away:
    https://www.rapidtables.com/code/linux/gcc/gcc-l.html

    Programming can be pretty hard and complicated at times, especially when working with lower-level languages like C and C++. You should probably try to complete some tutorial classes to get a better understanding of the general concepts and to avoid future frustrations and road blocks. I highly recommend checking out Python for a more user-friendly experience.

    Also, please upgrade to Gurobi version 9. I don't see any reason for starting with an outdated version that is superseded by another version 8 release. At the very least, you should be using Gurobi 8.1.1 if you really need to work with version 8.

    Please don't get me wrong - I really want to help you and don't want to sound discouraging!

    All the best,
    Matthias

    1
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Hi Alexandre,

    You should not modify the include instructions in the code but rather tell the compiler where to find the include directory via:

    g++ -I/media/34GB/Arquivos-de-Programas-Linux/gurobi801/linux64/include/

    The same holds for library paths:

    g++ -L/media/34GB/Arquivos-de-Programas-Linux/gurobi801/linux64/lib

    You might want to check out CMake for a more human-readable way of defining build and compile instructions.

    Cheers,
    Matthias

    0
  • Alexandre Faria
    Gurobi-versary
    First Comment
    First Question

    Hello. I tried to compile the original mip1 with:

    g++ -I/media/34GB/Arquivos-de-Programas-Linux/gurobi801/linux64/include/
    -L/media/34GB/Arquivos-de-Programas-Linux/gurobi801/linux64/lib/
    -o mip1_c++ mip1_c++.cpp

    But the same errors keep showing up:

    /tmp/ccLVCNbc.o: In function `main':
    mip1_c++.cpp:(.text+0x2b): undefined reference to `GRBEnv::GRBEnv(bool)'
    mip1_c++.cpp:(.text+0x44): undefined reference to `GRBModel::GRBModel(GRBEnv const&)'
    mip1_c++.cpp:(.text+0x8d): undefined reference to `GRBModel::addVar(double, double, double, char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
    0
  • Thomas Opfer
    Gurobi-versary
    Thought Leader

    Undefined references mean that you forgot the library in the compiler command. I am not sure which library is missing, but I guess it is libgurobi_c++.a, so try adding -lgurobi_c++ to the end of your compiler command.

    0
  • Matthias Miltenberger
    Gurobi Staff Gurobi Staff

    Thomas is correct. The commands or compiler options in my previous post only set additional paths to look for headers and libraries - you still need to specify the actual libraries, though, via

    -lgurobi_c++ -lgurobi90
    0
  • Alexandre Faria
    Gurobi-versary
    First Comment
    First Question

    Ok, this time I tried with:

    g++ -I/media/34GB/Arquivos-de-Programas-Linux/gurobi801/linux64/include/
    -L/media/34GB/Arquivos-de-Programas-Linux/gurobi801/linux64/lib
    -lgurobi_c++ -lgurobi90 -o mip1_c++ mip1_c++.cpp

    and received: /usr/bin/ld: cannot find -lgurobi90

    Looking for it with sudo find /media/34GB/Arquivos-de-Programas-Linux/gurobi801/ -name gurobi90* returns no result. Where it is (or should be?)

    If I omit the gurobi90, keeping everything else equal, I still receive the same:

    mip1_c++.cpp:(.text+0x2b): undefined reference to `GRBEnv::GRBEnv(bool)'
    mip1_c++.cpp:(.text+0x44): undefined reference to `GRBModel::GRBModel(GRBEnv const&)'
    mip1_c++.cpp:(.text+0x8d): undefined reference to `GRBModel::addVar(double, double, double, char, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
    0
  • Thomas Opfer
    Gurobi-versary
    Thought Leader

    For Gurobi 8.0, it might be -lgurobi80 .

    0
  • Alexandre Faria
    Gurobi-versary
    First Comment
    First Question

    True, however, the problem withstands. Any ideas?

    0
  • Alexandre Faria
    Gurobi-versary
    First Comment
    First Question

    Thanks, it did work. What -lgurobi_c++ and -lgurobi80 does that -L/... does not? I mean, -L already points to /lib, where libgurobi.so.8.0.1 and libgurobi_g++4.2.a are....

    0

Please sign in to leave a comment.