ERGO-Code / HiGHS

Linear optimization software
MIT License
915 stars 171 forks source link

Linking HiGHS with Visual C++ #694

Open ggeog89 opened 2 years ago

ggeog89 commented 2 years ago

@odow I would like to use HiGHS to solve MILP optimization problems related to production scheduling. However, my initial efforts to link Visual C++ with the prebuild binaries ("x86_64-w64-mingw32-cxx11.tar.gz") and solve a simple example ("call_highs_from_cpp.cpp") were unsuccessful. In particular, I statically link the "libhighs.dll.a" file but it seems that the library cannot find the class Highs in order to instantiate it. While searching for a solution I tried to run the C example ("call_highs_from_c.c") and there were no linking problems. It seems that "libhighs.dll.a" can export Highslpcall (C) but not Highs (C++). Is it possible to use the prebuild binaries with Visual C++ ? Thank you

odow commented 2 years ago

I haven't tried linking the binaries to C++; we only use the C interface. Did you try dynamically linking with /bin/libhighs.dll?

If that doesn't work, HiGHS is not difficult to compile, so perhaps the easiest thing is to compile a version yourself.

Here are the flags we use when compiling HiGHS: https://github.com/JuliaPackaging/Yggdrasil/blob/750e6c51e56a196c1343befd00e1bbf0a60fd8d9/H/HiGHS/build_tarballs.jl#L29-L35 if we need to change some to enable linking for C++ let me know.

I would like to use HiGHS to solve MILP optimization problems related to production scheduling

Have you considered using a modeling language like JuMP?

There are tutorials to get started: https://jump.dev/JuMP.jl/stable/tutorials/getting_started/getting_started_with_julia/ https://github.com/jump-dev/HiGHS.jl#use-with-jump

ggeog89 commented 2 years ago

Thank you odow,

unfortunately, I wasn't able to successfully dynamically link the binaries with the dll. Therefore, I tried to compile HiGHS using cmake. I have followed the instructions found in highs.dev, however, I wasn't able to build HiGHS. Based on the instructions I am running cmake, but then I have to also run the make command, which is not available in Windows. In conclusion, I wasn't able to follow these instructions, therefore I have used the Cmake GUI, with Visual Studio. I ended up with the necessary dlls and while I could build the solution in Visual Studio, I couldn't run the project because of the following error:

"Unhandled exception at 0x00007FFA6BECFDC8 (highs.dll) in Test_HiGHS_V0.2.exe: 0xC0000005: Access violation writing location 0x0000000000000000."

Do you know why I get this error?

Despite all that I was able to run HiGHS from C and C# (using the .dll generated from cmake). Ideally, I would like to work with C# (or C++ if I can fix the error mentioned above), but in the example "call_highs_from_csharp.cs" only an example for an LP problem is presented. Is it possible to solve an MILP problem using the C# api?

I know that JuMP is an efficient alternative, but I want to connect HiGHS with a .net application.

odow commented 2 years ago

Caveat: I don't have any experience compiling HiGHS on Windows, or linking a DLL to C#.

which is not available in Windows

https://stackoverflow.com/questions/32127524/how-to-install-and-use-make-in-windows

Is it possible to solve an MILP problem using the C# api?

In theory, yes, but it looks like the C# interface needs updating because it's missing the relevant methods: https://github.com/ERGO-Code/HiGHS/blob/master/src/interfaces/highs_csharp_api.cs

lgottwald commented 2 years ago

I don't think you can use prebuilt mingw binaries to link with MSVC C++. I would think they are likely not binary compatible. In that case just compile the HiGHS library your self in Visual C++ with Cmake

lgottwald commented 2 years ago

Generally, with C++, you always need to use the same compiler the library was built with to build code linking against the library.

There are some compatibilities that compiler vendors might additionally guarantee, e.g. when compiling with the GNU compiler you could look up for which particular compiler versions the ABI (Application Binary Interface) for C++ is compatible. Same when using the MSVC compiler you might find out from Microsoft documentation which versions create binary compatible output.

Only for C code there is an agreed on standard for how the binary interface must look like, so that you can call C library functions in pre-built binaries of any compiler. So above package uses mingw and I think the GNU compiler so it is clear that you cannot use MSVC to call C++ code in that library.

ggeog89 commented 2 years ago

Thank you for the explanation! I have figured out that I cannot use MSVC with the prebuilt binaries. My issue is that I compile the library using Cmake and Visual C++, I get the libraries, I get no errors (build completes) BUT I cannot run the created application due to this error:

"Exception thrown at 0x7C60AEED (highs.dll) in TestHiGHS.exe: 0xC0000005: Access violation writing location 0x00D60000. Unhandled exception at 0x0000000F in TestHiGHS.exe: 0xC00001A5: An invalid exception handler routine has been detected (parameters: 0x00000003)."

Do you know anyone that have successfully run HiGHS using MSVC?

lgottwald commented 2 years ago

Yes, we have HiGHS built with MSVC in our continuous integration runs, so in principle it should work. I would need to see your code for looking into why this might fail.

ggeog89 commented 2 years ago

You can check my code here https://github.com/ggeog89/TestHiGHS.git I haven't done much. Just create the project, link with the necessary dependencies, and try to create a HiGHS instance.

mckib2 commented 2 years ago

If you don't need the tests (it appears to be a runtime error with the unit tests), you might try building with the -DBUILD_TESTING=OFF

peno64 commented 1 year ago

I compiled HiGHS with Visual Studio as follows and it generates the exe, dll and lib files and I can link this to a test C application. First you need cmake which is not part of visual studio. It can be installed from here: https://cmake.org/download/ Then do the following, starting from the HiGHS folder: mkdir build cd build cmake -DFAST_BUILD=off .. cmake --build . --config Release This will generate under bin\Release the following files: capi_unit_tests.exe csharpexample.exe highs.dll highs.exe HighsCsharp.dll unit_tests.exe

Note to run the csharpexample file you need to copy highs.dll to libhighs.dll Under bin\lib\Release, highs.lib is generated to link a c program with such that it can call the dll

Note that I added to the last cmake command --config Release This to generate the release version of the binaries because by default it generates the debug versions which are alot larger and possibly slower.

jajhall commented 1 year ago

Debug binaries are about 4-5 times slower than 'release'