Undefined references problem when compiling c++ test code
AnsweredHello. I have been experiencing multiple undefined reference problems when trying to compile a very simple c++ source code. In fact, the code is the same as "mip1_c++.cpp", except that I changed "#include "gurobi_c++.h" to "#include "/media/34GB/Arquivos-de-Programas-Linux/gurobi801/linux64/include/gurobi_c++.h" that it is where the file it is in my system.
The first three error lines are:
copy_mip1_c++.cpp:(.text+0x2b): undefined reference to GRBEnv::GRBEnv(bool)
copy_mip1_c++.cpp:(.text+0x44): undefined reference to GRBModel::GRBModel(GRBEnv const&)
copy_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> >)
I also added the lib dir to my ld.so.conf:
/media/34GB/Arquivos-de-Programas-Linux/gurobi801/linux64/lib
and then executed ldconfig, to make the system aware of changes on it.
Yet, these error messages persist. What shall I do to solve it? Thanks for your time
-
Official comment
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?. -
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,
Matthias0 -
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++.cppBut 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 -
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 -
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 -
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++.cppand 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 -
For Gurobi 8.0, it might be -lgurobi80 .
0 -
True, however, the problem withstands. Any ideas?
0 -
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++ -lgurobi801 -
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 -
Dear Alexandre,
The explanation is literally a simple online search for "gcc linking" away:
https://www.rapidtables.com/code/linux/gcc/gcc-l.htmlProgramming 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,
Matthias1
Post is closed for comments.
Comments
11 comments