可以使用Mono运行程序,但不能使用Visual Studio Mac运行程序 [英] Can run program with Mono but not with Visual Studio Mac

查看:241
本文介绍了可以使用Mono运行程序,但不能使用Visual Studio Mac运行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个在Visual Studio Mac 2019中制作的C#程序.它使用SFML.Net框架,该框架取决于动态库:libcsfml-graphics.2.5.0.dylib.在内部,框架具有称为sfRenderWindow_createUnicode(...)的方法.此方法对CSFML.graphics使用DLLImport.如果我在Visual Studio中运行该程序,则会收到带有消息csfml-graphicsDllNotFoundException.

So I have a C# program that I'm making in Visual Studio Mac 2019. It uses the SFML.Net framework which depends on a dynamic library: libcsfml-graphics.2.5.0.dylib. Internally, the framework has a method called sfRenderWindow_createUnicode(...). This method uses a DLLImport for CSFML.graphics. If I run the program in Visual Studio, I get a DllNotFoundException with message csfml-graphics.

在Mono的全局config文件中,我有一个DLL映射:

In my global config file for Mono, I have a DLL Mapping:

<dllmap dll="csfml-graphics" target="libcsfml-graphics.2.5.0.dylib" />

添加后,如果我在Visual Studio中运行程序,我仍然会得到DllNotFoundException. 但是,消息现在显示为libcsfml-graphics.2.5.0.dylib.因此,它正在寻找正确的库,但找不到它? .dylib文件与.exe在同一文件夹中.

After adding that, if I run my program in Visual Studio, I still get a DllNotFoundException. However, the message now says libcsfml-graphics.2.5.0.dylib. So it's looking for the right library but can't find it? The .dylib file is in the same folder as the .exe.

奇怪的是,我可以从终端运行程序,如下所示:

The weird part is I can run the program from the terminal like so:

MONO_LOG_LEVEL=debug MONO_LOG_MASK=dll mono hello-csharp.exe > log.txt

log.txt文件包含以下几行:

Mono: DllImport attempting to load: 'libcsfml-graphics.2.5.0.dylib'.
Mono: DllImport loaded library '/Users/rutvik/Desktop/hello-csharp/hello-csharp/bin/Debug/libcsfml-graphics.2.5.0.dylib'.
Mono: DllImport searching in: 'libcsfml-graphics.2.5.0.dylib' ('/Users/rutvik/Desktop/hello-csharp/hello-csharp/bin/Debug/libcsfml-graphics.2.5.0.dylib').
Mono: Searching for 'sfRenderWindow_createUnicode'.
Mono: Probing 'sfRenderWindow_createUnicode'.
Mono: Found as 'sfRenderWindow_createUnicode'.

因此出于某种原因,它可以找到.dylib. Visual Studio Mac有什么不同之处?以及如何配置它使其起作用?

So it can find the .dylib for some reason. What is Visual Studio Mac doing differently? And how do I configure it to make it work?

作为参考,这是针对libcsfml-graphics.2.5.0.dylibotool -L:

For reference, here is otool -L run against libcsfml-graphics.2.5.0.dylib:

libcsfml-graphics.2.5.0.dylib:
    libcsfml-graphics.2.5.dylib (compatibility version 2.5.0, current version 2.5.0)
    @rpath/sfml-graphics.framework/Versions/2.5.1/sfml-graphics (compatibility version 2.5.0, current version 2.5.1)
    @rpath/sfml-window.framework/Versions/2.5.1/sfml-window (compatibility version 2.5.0, current version 2.5.1)
    @rpath/sfml-system.framework/Versions/2.5.1/sfml-system (compatibility version 2.5.0, current version 2.5.1)
    /usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.50.4)

最终结果:

仅作总结,我将所有.dylib文件都放在了/usr/local/lib中.诀窍是按照接受的答案将平台目标设置为x64.实际上,实际上根本不需要DYLD_LIBRARY_PATH环境.而且.dylib文件不必与.exe放在同一文件夹中.

Just to summarize, I put all the .dylib files in /usr/local/lib. The trick was to set the platform target to x64 as specified in the accepted answer. The DYLD_LIBRARY_PATH environment actually turned out to be not needed at all. And the .dylib files do not have to be in the same folder as the .exe.

现在无论是从Visual Studio运行还是在终端中以mono运行,都会产生相同的结果.

Now both running from Visual Studio and running with mono in the terminal yield identical results.

万岁!

推荐答案

1)确保您的Platform Target与您的ABI类型(x32或x64位)匹配:

1) Make sure your Platform Target matches your ABI type (x32 or x64 bit):

VS4M将默认启动32位Mono版本,因为大多数项目目标默认为x32.当然,如果您要生产脂肪" dylib,则无所谓.

VS4M will launch the 32-bit Mono version by default as most project targets default to x32. Of course this does not matter if you are producing "fat" dylibs.

2)在Run Configuration中将DYLD_LIBRARY_PATH设置为与dylib位置匹配:

2) Set DYLD_LIBRARY_PATH in your Run Configuration to match your dylib location:

re: https: //www.mono-project.com/docs/advanced/pinvoke/#macos-framework-and-dylib-search-path

这篇关于可以使用Mono运行程序,但不能使用Visual Studio Mac运行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆