首页 新闻 会员 周边

请问.NET Core平台下调用GDAL库相关dll的问题

0
悬赏园豆:50 [已解决问题] 解决于 2018-04-23 10:06

GDAL本身是C++编写的,通过DllImport / PINVOKE的方式封装为C#可以调用的类:

1)GdalPINVOKE中引入gdal_wrap.dll:

[DllImport("gdal_wrap", EntryPoint="CSharp_SetConfigOption")]
public static extern void SetConfigOption(string jarg1, string jarg2);

2) Gdal.cs中调用GdalPINVOKE

public static void SetConfigOption(string pszKey, string pszValue) {
GdalPINVOKE.SetConfigOption(pszKey, pszValue);
if (GdalPINVOKE.SWIGPendingException.Pending) throw GdalPINVOKE.SWIGPendingException.Retrieve();
}

3)实际使用中调用Gdal:

Environment.SetEnvironmentVariable("GEOTIFF_CSV", gdalData);
Gdal.SetConfigOption("GEOTIFF_CSV", gdalData);

 

现在,在windows 操作系统中,以上的过程可以正常调用;但是在Linux环境下调用的时候,提示无法加载gdal_wrap.dll,缺少dll或它的依赖项:

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 DLL 'gdal_wrap': The specified module or one of its dependencies could not be found.
(Exception from HRESULT: 0x8007007E)

我尝试把可能依赖的dll都放到运行目录下,仍然无法正常调用。

请问,有没有高手碰到过类似的问题?

 

countryer的主页 countryer | 初学一级 | 园豆:154
提问于:2018-04-05 01:54

问题原因:在Linux中编译的过程中,由于系统本身的配置不同,编译的结果是不一样的,不同的系统编译的包不能通用,所以Nuget上的GDAL.Core是不能使用的。
解决方案:使用的时候,在系统中重新编译一下

countryer 6年前
< >
分享
最佳答案
1

试试NuGet包:Gdal.Core

收获园豆:30
dudu | 高人七级 |园豆:30994 | 2018-04-05 14:11

我装了一下Gdal.Core,调用的地方也改成调用Gdal.Core里的函数,但是还是提示无法加载gdal_wrap,找不到dll或它的依赖项:

直接把所有的so文件放到和编译的dll同样的路径也不行

 

countryer | 园豆:154 (初学一级) | 2018-04-05 15:42

@countryer: linux用的是哪个发行版?

dudu | 园豆:30994 (高人七级) | 2018-04-05 15:55

@dudu: ubuntu 16.04 和 Centos 7两个

countryer | 园豆:154 (初学一级) | 2018-04-05 16:16

@dudu: 如果把路径改成错误的,会提示找不到文件:

Error:
An assembly specified in the application dependencies manifest (HHGIS.Core.deps.json) was not found:
package: 'Gdal.Core', version: '1.0.0'
path: 'publish/runtimes/runtimes/linux-x64/native/gdal_wrap.so'

感觉Gdal.Core好像有其他依赖的文件没有打包在一起似的

countryer | 园豆:154 (初学一级) | 2018-04-05 16:21

@countryer: 试试 apt install gdal-bin 安装 GDAL

dudu | 园豆:30994 (高人七级) | 2018-04-05 16:29

@countryer: 试试将 gdal_wrap.dll 复制到 ~/.nuget/packages/gdal.core/1.0.0/lib/netstandard2.0 文件夹中

dudu | 园豆:30994 (高人七级) | 2018-04-05 22:48

@dudu: 我把Gdal.Core里的dll和so文件放进去试了一下,还是提示gdal_wrap的错误

countryer | 园豆:154 (初学一级) | 2018-04-06 00:37
其他回答(2)
0

c++的dll在Linux是没法直接使用的. Linux下c++的动态库是so, 有源码切不依赖Windows的特性的话,可以将c++的源码用gcc编译为so,在调用

收获园豆:20
秋壶冰月 | 园豆:5903 (大侠五级) | 2018-04-05 13:41

谢谢回复~

我换成GDAL.Core里带的SO文件试了一下,还是提示 :Unable to load DLL 'gdal_wrap': The specified module or one of its dependencies could not be found.

Linux环境下,SO文件需要放到什么特殊的位置吗?还是需要配置一下环境变量之类的才能指向这个文件?

支持(0) 反对(0) countryer | 园豆:154 (初学一级) | 2018-04-05 15:44

@countryer:  在用gcc 加上 -fPIC 与位置无关  -shared 生成动态库  ,然后将动态库放到程序所在目录,pinvoke调用就好了

支持(0) 反对(0) 秋壶冰月 | 园豆:5903 (大侠五级) | 2018-04-06 22:57
0

请问这个问题您解决了吗?我现在也遇到同样的问题。window下正常,linux下报错,跟您的错误一样的。

邸彬彬 | 园豆:202 (菜鸟二级) | 2020-05-13 12:15

已解决,使用ldd gdal_wrap.so查看缺少哪些链接库,一一安装上就行了。

支持(0) 反对(0) 邸彬彬 | 园豆:202 (菜鸟二级) | 2020-05-14 13:41
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册