Error (undefined symbols) compiling example mip_1.cpp from the command line
回答済み(Basic question, and variants of it have been asked before, but I still can't get it working.)
I'm trying to compile the example program from the command line, running:
g++ -I/Library/gurobi1000/macos_universal2/include -L/Library/gurobi1000/macos_universal2/lib mip1_c++.cpp -o mip1
I get the error:
Undefined symbols for architecture arm64:
"GRBLinExpr::GRBLinExpr(GRBVar, double)", referenced from:
_main in mip1_c++-b3503e.o
"GRBLinExpr::operator+(GRBLinExpr const&)", referenced from:
_main in mip1_c++-b3503e.o
...
_main in mip1_c++-b3503e.o
"operator+(GRBLinExpr const&, GRBLinExpr const&)", referenced from:
_main in mip1_c++-b3503e.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm on MacOS V12.6. The compiler details (output of `g++ -v`) are:
Apple clang version 14.0.0 (clang-1400.0.29.102)
Target: arm64-apple-darwin21.6.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
-
Hi Matthew,
Could you please try the approach discussed in How do I resolve "undefined reference" errors while linking Gurobi in C++?
Best regards,
Jaromił1 -
Dear Jaromił,
I tried what you suggest but I'm still getting the same error.
Here is the output of make, which looks good (I think):
build make
g++ -m64 -fPIC -O -I../../include -c ../cpp/Env.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/Model.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/attrprivate.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/Var.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/Constr.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/LinExpr.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/QuadExpr.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/Exception.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/Callback.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/Column.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/SOS.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/QConstr.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/GenConstr.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/TempConstr.cpp
g++ -m64 -fPIC -O -I../../include -c ../cpp/Batch.cpp
ar rv libgurobi_c++.a Env.o Model.o attrprivate.o Var.o Constr.o LinExpr.o QuadExpr.o Exception.o Callback.o Column.o SOS.o QConstr.o GenConstr.o TempConstr.o Batch.o
ar: creating archive libgurobi_c++.a
a - Env.o
a - Model.o
a - attrprivate.o
a - Var.o
a - Constr.o
a - LinExpr.o
a - QuadExpr.o
a - Exception.o
a - Callback.o
a - Column.o
a - SOS.o
a - QConstr.o
a - GenConstr.o
a - TempConstr.o
a - Batch.oI copied libgurobi_c++.a to ../../lib/. That worked, no permission issues. I checked the timestamp of the file in the lib directory just to make sure it was the newly created one.
But when I run
g++ -I/Library/gurobi1000/macos_universal2/include -L/Library/gurobi1000/macos_universal2/lib mip1_c++.cpp -o mip1
still the same error!
Yours,
Matthew0 -
Hi Matthew,
Could you have a look at the Makefile provided with the Gurobi Optimizer package? You can find it at /Library/gurobi1000/macos_universal2/examples/build.
Once you are in this directory, could you please try from the command line to execute
> make mip1_c++
If this works, then your original command is missing a few arguments, which you can deduce from the Makefile provided in the /Library/gurobi1000/macos_universal2/examples/build directory.
Best regards,
Jaromił1 -
Makefile forensics revealed that I was missing "-lgurobi_c++ -lgurobi100" from the invocation of g++. I figured that with "-L/Library/gurobi1000/macos_universal2/lib" set, the compiler would just know how to find all the relevant stuff in the lib subdirectory. I still find this bizarre because there are know files named Gurobi_c++.* or gurobi100.* in lib. Rather, there are files libgurobi_c++.a and libgurobi100.dylib. So does g++ just know how to deal with the "lib" prefix and the extensions? (Obviously, I have minimal experience with the C/C++ toolchain. Working on it!)
Thanks!!
Yours,
Matthew0 -
Hi Matthew,
The C++ compiler is really complex and I would not call myself an expert at all despite having to work with it for quite some time. Usually, it is best to try to search the gcc docs for clues. Of course, it is very hard when something is not working. Thus, it's often best to start with a working \(\texttt{make}\) command such as the one provided with the Gurobi Optimizer package and build up from there.
So does g++ just know how to deal with the "lib" prefix and the extensions? (Obviously, I have minimal experience with the C/C++ toolchain. Working on it!)
Yes, it indeed does. From the gcc documentation
-llibrary
-l librarySearch the library named library when linking. (The second alternative with the library as a separate argument is only for POSIX compliance and is not recommended.)It makes a difference where in the command you write this option; the linker searches and processes libraries and object files in the order they are specified. Thus, foo.o -lz bar.o searches library z after file foo.o but before bar.o. If bar.o refers to functions in z, those functions may not be loaded.
The linker searches a standard list of directories for the library, which is actually a file named liblibrary.a. The linker then uses this file as if it had been specified precisely by name.
The directories searched include several standard system directories plus any that you specify with -L.
Normally the files found this way are library files---archive files whose members are object files. The linker handles an archive file by scanning through it for members which define symbols that have so far been referenced but not defined. But if the file that is found is an ordinary object file, it is linked in the usual fashion. The only difference between using an -l option and specifying a file name is that -l surrounds library with lib and .a and searches several directories.
Best regards,
Jaromił1
サインインしてコメントを残してください。
コメント
5件のコメント