MaxRev-Dev / gdal.netcore

GDAL 3.x C#/F# bindings for .NET apps
MIT License
161 stars 36 forks source link

[BUG] Unable to load shared library 'gdal_wrap' or one of its dependencies In CentOS 7 #156

Closed LGinC closed 1 month ago

LGinC commented 1 month ago
$ sudo dotnet Web.Host.dll

Unhandled exception. System.TypeInitializationException: The type initializer for 'OSGeo.GDAL.GdalPINVOKE' threw an exception.
 ---> System.TypeInitializationException: The type initializer for 'SWIGExceptionHelper' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'gdal_wrap' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libgdal_wrap: cannot open shared object file: No such file or directory
   at OSGeo.GDAL.GdalPINVOKE.SWIGExceptionHelper.SWIGRegisterExceptionCallbacks_Gdal(ExceptionDelegate applicationDelegate, ExceptionDelegate arithmeticDelegate, ExceptionDelegate divideByZeroDelegate, ExceptionDelegate indexOutOfRangeDelegate, ExceptionDelegate invalidCastDelegate, ExceptionDelegate invalidOperationDelegate, ExceptionDelegate ioDelegate, ExceptionDelegate nullReferenceDelegate, ExceptionDelegate outOfMemoryDelegate, ExceptionDelegate overflowDelegate, ExceptionDelegate systemExceptionDelegate)
   at OSGeo.GDAL.GdalPINVOKE.SWIGExceptionHelper..cctor()
   --- End of inner exception stack trace ---
   at OSGeo.GDAL.GdalPINVOKE.SWIGExceptionHelper..ctor()
   at OSGeo.GDAL.GdalPINVOKE..cctor()
   --- End of inner exception stack trace ---
   at OSGeo.GDAL.GdalPINVOKE.AllRegister()
   at OSGeo.GDAL.Gdal.AllRegister()
   at MaxRev.Gdal.Core.GdalBase.ConfigureGdalDrivers(String gdalDataFolder)
   at MaxRev.Gdal.Core.GdalBase.ConfigureAll()

it work fine in windows, and I just run dotnet build -c Release and copy release folder to linux server

Environment information:

$ glib-compile-resources --version
2.56.1

$ ls runtimes/linux-x64/native/
libaec.so.0           libcrypto.so.3.2.0    libgdal.so.35      libhdf5.so.310          libjxl.so.0.10          liblzma.so.5      libogr_wrap.so            libpq.so.5        libtiff.so.6        maxrev.gdal.core.libshared
libblosc.so.1         libcurl.so.4          libgdal_wrap.so    libhwy.so.1             libjxl_threads.so.0.10  libmariadb.so.3   libOpenEXR-3_2.so.31      libproj.so.25     liburiparser.so.1
libbrotlicommon.so.1  libdeflate.so.0       libgeos_c.so.1     libIex-3_2.so.31        libkmlbase.so.1         libmfhdf.so.0     libOpenEXRCore-3_2.so.31  libsharpyuv.so.0  libwebp.so.7
libbrotlidec.so.1     libdf.so.0            libgeos.so.3.11.3  libIlmThread-3_2.so.31  libkmldom.so.1          libminizip.so     libopenjp2.so.7           libsqlite3.so     libxerces-c-3.2.so
libbrotlienc.so.1     libexpat.so.1         libgeotiff.so.5    libImath-3_1.so.29      libkmlengine.so.1       libnetcdf.so.19   libosr_wrap.so            libssl.so.1.1     libxml2.so.2
libcfitsio.so.3       libfreexl.so.1        libgif.so          libjpeg.so.62           liblcms2.so             libodbcinst.so.2  libpcre.so                libssl.so.3.2.0   libz.so.1
libcrypto.so.1.1      libgdalconst_wrap.so  libhdf5_hl.so.310  libjxl_cms.so.0.10      liblz4.so               libodbc.so.2      libpng16.so.16            libszip.so.2.1    libzstd.so.1
<ItemGroup>
  <PackageReference Include="MaxRev.Gdal.Core" Version="3.9.1.247" />
  <PackageReference Include="MaxRev.Gdal.LinuxRuntime.Minimal" Version="3.9.1.247" />
  <PackageReference Include="MaxRev.Gdal.WindowsRuntime.Minimal" Version="3.9.1.247" />
</ItemGroup>
MaxRev-Dev commented 1 month ago

@LGinC CentOS 7 is not supported anymore. Consider moving to another distro or use docker. Read this doc section.

LGinC commented 1 month ago

OK, I install gdal from source code, and how to configure to make MaxRev.Gdal.Core find gdal library

$gdalinfo --version
GDAL 3.9.3, released 2024/10/07 (debug build)
MaxRev-Dev commented 1 month ago

@LGinC This requires advanced manual setup.

  1. Set GdalBase.EnableRuntimeValidation = false;. Before calling GdalBase.ConfigureAll();
  2. Install native binaries (with bindings option) and find the native dll files location <GdalFolder>\bin\gdal\csharp. Link them (add a reference) in your csproj project file to each gdal_csharp.dll, gdalconst_csharp.dll, ogr_csharp.dll and osr_csharp.dll. This will work only for one runtime. Use conditional mapping if you need Linux runtime.

The step 2 requires GDAL to be compiled with bindings enabled, otherwise works only via CLI.

So I wouldn't recommend this. The bindings are being compiled during the GDAL build and this generates corresponding C# interface. Binaries are placed in the same directory, so the startup knows what to utilize. You need either to use CLI tools or dockerized .NET app with GDAL packages.

LGinC commented 1 month ago

@MaxRev-Dev thx I found it change proj.db search directory and it work

if(OperatingSystem.IsLinux())
{
    GdalBase.EnableRuntimeValidation = false;
    GdalBase.ConfigureGdalDrivers();
    Proj.Configure("/usr/local/share/proj/");
}
else
    GdalBase.ConfigureAll();