lballabio / QuantLib

The QuantLib C++ library
http://quantlib.org
Other
5.43k stars 1.8k forks source link

Unable to load DLL 'NQuantLibc' exception when using QuantLib Nuget package #2109

Closed lukedavis8767 closed 3 weeks ago

lukedavis8767 commented 3 weeks ago

Hi I have added the QuantLib Nuget Package Version 1.36.0 to my project via Nuget.Org and I am getting the following error when attempting to run my code. Note that code compilation is perfectly fine and VS is able to pick up QuantLib libraries fine:

System.TypeInitializationException: The type initializer for 'QuantLib.NQuantLibcPINVOKE' threw an exception. ---> System.TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception. ---> System.DllNotFoundException: Unable to load DLL 'NQuantLibc': The specified module could not be found. (Exception from HRESULT: 0x8007007E) at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper.SWIGRegisterExceptionCallbacks_NQuantLibc(ExceptionDelegate applicationDelegate, ExceptionDelegate arithmeticDelegate, ExceptionDelegate divideByZeroDelegate, ExceptionDelegate indexOutOfRangeDelegate, ExceptionDelegate invalidCastDelegate, ExceptionDelegate invalidOperationDelegate, ExceptionDelegate ioDelegate, ExceptionDelegate nullReferenceDelegate, ExceptionDelegate outOfMemoryDelegate, ExceptionDelegate overflowDelegate, ExceptionDelegate systemExceptionDelegate) at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper..cctor() --- End of inner exception stack trace --- at QuantLib.NQuantLibcPINVOKE.SWIGExceptionHelper..ctor() at QuantLib.NQuantLibcPINVOKE..cctor() --- End of inner exception stack trace --- at QuantLib.NQuantLibcPINVOKE.new_Date__SWIG_1(Int32 jarg1, Int32 jarg2, Int32 jarg3) at QuantLib.Date..ctor(Int32 d, Month m, Int32 y) at MYPROGRAM

sweemer commented 3 weeks ago

Which .NET version and OS are you using? And how are you running your program? (i.e. dotnet run, dotnet publish, etc)

lukedavis8767 commented 3 weeks ago

This is using net472, Windows 11, when running an .exe program. QuantLib version 1.36.0. (I believe this is compatible with net472?)

I have tested building in both Debug and Release but same error. AnyCPU for both. To build I have tested using MSBuild.exe on the .csproj and also built the project in Visual Studio. I am running the program directly from Powershell, running the generated .exe file.

lballabio commented 3 weeks ago

The shared library is inside the nupkg but probably not in the right place for net472; I found documentation for where the newer .Net expects it but not for netstandard. Can you point us at some other nupkg that (like QuantLib) also contains native code, as in, C or C++? That might help figuring it out.

If not, you might be able to extract NQuantLibc.dll from the nupkg and put it somewhere in your path or where the runtime can find it.

lukedavis8767 commented 3 weeks ago

I have copied the NQuantLib.dll from "quantlib.1.36.0\runtimes\win-x64\native\NQuantLibc.dll" directly into my bin directory alongside my generated .exe. as @lballabio suggested. This seems to work fine, and we only run the script occasionally anyway so this workaround is good, thanks!

sweemer commented 3 weeks ago

I found documentation for where the newer .Net expects it but not for netstandard.

For the avoidance of doubt, net472 is .NET Framework, not .NET Standard. Indeed I think the root problem is that the nupkg is built with .NET 6, which is not backwards compatible with .NET Framework.

According to nuget.org, QuantLib is available for netstandard2.0, but this would not be true if the nupkg is really built only with .NET 6. @lballabio Want to confirm this and remove all supported frameworks except for net6.0 from the nuget.org metadata?

In the long run, we should actually target .NET Standard so that both .NET Framework and .NET users can all use it.

lukedavis8767 commented 3 weeks ago

According to nuget.org, QuantLib is available for netstandard2.0, but this would not be true if the nupkg is really built only with .NET 6. @lballabio Want to confirm this and remove all supported frameworks except for net6.0 from the nuget.org metadata?

Just to chime back in, I believe QuantLib is already multi-targeting Net Standard 2.0 along with Net 6,7,8. These .dlls are under the nuget package directory: quantlib.1.36.0\lib\ and the package appears to work fine with our Net Framework 4.7.2 project.

I think it's also still worth multi-targeting Net 6.0 onwards as users can leverage the performance improvements of it.

sweemer commented 3 weeks ago

OK now I see the target for netstandard2.0 in the nupkg, thanks for pointing that out. I didn't see any references to netstandard in the project so I assumed it wasn't being targeted anywhere. Sorry for the confusion.