Cannot compile Gurobi examples in version 7.5.1 - c++

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.

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.

Related

Add SFML (third party library) to C++ project on Xcode

I'm learning how to build a simple UI in C++ on my Mac (OS 11.6) using Xcode.
As first step I'm compiling the "Hello world" program, my problem is that the build on Xcode fails but write my own command from terminal, instead, works.
This is the program, I'm using SFML :
#include <iostream>
#include "SFML/Graphics.hpp"
int main(int argc, const char * argv[]) {
// insert code here...
std::cout << "Hello, World!\n";
return 0;
}
I have no error here but when launching Run from Xcode this is the output, in Graphics.hpp file :
#include <SFML/Window.hpp>. //'SFML/Window.hpp' file not found
#include <SFML/Graphics/BlendMode.hpp>
#include <SFML/Graphics/CircleShape.hpp
//other header files
This is how the project is structured ("TestGui" is the project name) :
-TestGui.xcodeproj
-TestGui(folder)
--SFML(directory with all headers file available
-- main.cpp
SFML source code here
So I tried to compile it with my own hands from terminal with :
g++ main.cpp -I ./SFML -o main
and
clang++ main.cpp -I ./SFML -o main
In both cases it compiled, also run worked.
Since the error is linked to a file not found I tried to tell it where libraries are located, so in Xcode from Product->Scheme->Edit Scheme->Run->Arguments->Arguments passed on launch : added -I ./SFML. But the error is still alive.
Added SFML folder to targets from Xcode, didn't copy-pasted but maybe I did it wrong, this is my first time.
EDIT : SFML folder:
--SFML
--- many .hpp files
--- 5 folders (Audio, Graphic, Network, System and Window)
I tried to add also this argument : -L ./SFML but nothing.

Running OpenCV Program on Windows

I have a simple test program for OpenCV:
#include "opencv2/opencv.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgcodecs/imgcodecs.hpp"
#include <iostream>
int main(int argc, char **argv){
std::cout << "HELLO" << std::endl;
cv::Mat im=cv::imread((argc==2)? argv[1]: "testing.jpg",cv::IMREAD_COLOR);
if (im.empty()){
std::cout << "Cannot open image." << std::endl;
} else {
cv::namedWindow("DisplayWindow",cv::WINDOW_AUTOSIZE);
cv::imshow("DisplayWindow",im);
cv::waitKey(0);
}
return 0;
}
However, when run the program does nothing. Hello is not printed to the console, and it does not output an error.
./main
#Nothing.......
It is worth noting that the program terminates, but not in the proper way. (The return value is non-zero) I do not think that this is a linking error, as those would actually output an error.
Any ideas on what is happening and/or how to fix it? I am using a Windows computer if that changes anything.
Turns out the windows cmd prompt actually has some use. (VERY surprising, I gave up on it a long time ago)
I ran the test program from the windows cmd line and it said the following libraries were missing.
libstdc++-6.dll
libgcc_s_dw2-1.dll
libwinpthread-1.dll
To fix the standard C++ and C libraries I just statically linked them using the below command. (This is apparently a common practice on Windows due to issues with version control):
g++ -static-libgcc -static-libstdc++ ...rest of compile/link cmd...
To fix the winpthread dll I just copied the dll to the bin folder of my program and everything worked!

Program doesn't start when linking boost filesystem

I'm trying to run a helloworld program which uses boost filesystem.
I'm on Windows with MinGW 8.1 and boost 1.70.
The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;
int main()
{
cout << "Hello Boost!" << endl;
fs::path abHome{"C:/Users/Me"s};
fs::path jsonFile = abHome / "jsonFile.json"s;
if (!fs::exists(jsonFile)) {
cout << "Creating json file from scratch." << endl;
}
}
"Hello Boost" isn't ever printed to the console.
I've compiled with both CMake and g++ from command line to try to better understand what's going on:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"
I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).
How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)
The problem was, as #kenba pointed out, that the dynamic linking of the boost dlls was failing.
I erroneously thought I had linked the static version of the boost libraries.
To actually achieve that I should have used this command:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"
instead of the one I posted in the OP.

Linking jsoncpp (libjson)

I'm trying to link jsoncpp (lib_json) with a c++ project using cmake. It works perfectly fine on one computer, but on another one (with pretty much the same config) i get an error when i execute my app :
dyld: Library not loaded: buildscons/linux-gcc-4.2.1/src/lib_json/libjson_linux-gcc-4.2.1_libmt.dylib
Referenced from: path to executable
Reason: image not found
Any idea what might be causing this ? I don't even understand why it tries to look # buildscons/linux-gcc-4.2.1/src/lib_json/libjson_linux-gcc-4.2.1_libmt.dylib since i put jsoncpp in usr/lib/ and changed the name to libjsoncpp and cmake find the correct path/library.
I also built jsoncpp the exact same way on both computers.
I had the same problem. If you run tool -L libjson_linux-gcc-4.2.1_libmt.dylib you can see some weird relative address to your libjson.... I guess if you replicated this directory structure it would work but that's a bad solution.
What I did instead is that I used .a (libjson_linux-gcc-4.2.1_libmt.a) and linked it staticaly with my binary. In XCode simply under Build Settings -> Linking -> Other Linker Flags I added absolute path to my .a. For me it was /Users/martin/Downloads/jsoncpp-src-0.5.0/libs/linux-gcc-4.2.1/libjson_linux-gcc-4.2.1_libmt.a and that's all.
Of course, I don't know your use case, maybe you really need to link it dynamically.
EDIT: I see now, you mean libjson, and not libjsoncpp (they're different!)
In your titel you talk about jsoncpp, and that's what this answer is for.
But maybe it's useful for people who got confused by the titel too.
You can 'amalgamate' jsoncpp.
From jsoncpp source dir run python amalgamate.py which creates:
dist/jsoncpp.cpp
dist/json/json.h
dist/json/json-forwards.h
Now you have to compile jsoncpp.cpp once and just link against the resulting jsoncpp.o:
g++ -o jsoncpp.o -c jsoncpp.cpp (only once)
g++ -o executable jsoncpp.o main.cpp (every time)
If you get errors, you might have to #define JSON_IS_AMALGAMATION before including json/json.h, but ...
... I tried it and it worked for me. (without #define JSON_IS_AMALGAMATION, that is)
Used code:
#include "json/json.h"
#include "json/json-forwards.h"
int main(int argc, char *argv[])
{
Json::Reader reader;
Json::Value value;
if (!reader.parse("{\"hello\":\"world\"}", value, false))
{
std::cerr << "ERROR: Couldn't parse Json: " << reader.getFormattedErrorMessages() << std::endl;
return -1;
}
std::cout << value.toStyledString() << std::endl;
return 0;
}

Include paths not found while compiling with g++ on MacOS

I'm trying to compile the simplest program on MacOS 10.6 like:
$ g++ -o hello hello.cpp
the following source:
#include <iostream>
int main (int argc, char * const argv[]) {
std::cout << "Hello, World!\n";
return 0;
}
I'm getting the error:
hello.cpp:1:20: error: iostream: No such file or directory
hello.cpp: In function ‘int main(int, char* const*)’:
hello.cpp:4: error: ‘cout’ is not a member of ‘std’
So obviously I have to add the include path somewhere. My question is where can I find the include directories and how can add them globally (I don't want to provide the include path whenever I want to compile).
I just installed the XCode 3.1.4 and managed to compile it via Xcode, but not via command line. I found some header files in this directory:
/Xcode3.1.4/SDKs/MacOSX10.5.sdk/System/Library/Frameworks/Kernel.framework/Versions/A/Headers
and tried to add it to the HEADER_SEARCH_PATHS after reading this question, but no luck.
I'm developing on Linux and everything is working fine there, but I want to continue doing that on MacOS. Any help?
On my Mac, that include file is in /usr/include/c++/4.0.0/iostream . Are you sure
you have all the command-line development tools installed? They might not be by default;
I'm pretty sure I had to install it manually when I first set up my Mac. There should be a "developer tools" package somewhere on your OS X installation media.
Or, if you want to make sure you're getting the latest version, you can download it from:
http://developer.apple.com/technology/xcode.html
$ g++ -o example.bin example.cpp //to compile
$ ./example.bin //to run
It's code:
#include <iostream>
using namespace std;
int main () {
cout << "Hello, World!\n";
return 0;
}