1

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.

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

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

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

The log.txt file contains the following lines:

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'.

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?

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)

WHAT WORKED IN THE END:

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.

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

Hooray!

2
  • 1
    you say "If I run the program in VisualStudio...," and later "now, if I run the program in VisualStudio..."; I guess there's a typo somewhere
    – knocte
    Aug 28, 2019 at 6:01
  • @knocte I meant to say that if I run it in Visual Studio after adding that <dllmap>. I edited my post for clarity, thanks!
    – rcplusplus
    Aug 29, 2019 at 19:47

1 Answer 1

3

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

enter image description here

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) Set DYLD_LIBRARY_PATH in your Run Configuration to match your dylib location:

enter image description here

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

9
  • So right now the DLLs are in bin/Debug, and when I use the absolute path to that folder, everything works. Is there a way to state more generally to look in the executable folder? I also tried copying the DLLs to /usr/local/lib using that for DYLD_LIBRARY_PATH, but it doesn't work. It doesn't throw an exception, it just seems to freeze and do nothing.
    – rcplusplus
    Aug 21, 2019 at 0:57
  • @rcplusplus I think it is ${TargetPath} but can not remember if VS4M does the expansion for env. vars. before setting them for that run session (it does for post/pre commands, etc..) Aug 21, 2019 at 1:05
  • Ok something just weird has happened. I removed the environment variable from the list and everything still works... The .dylib files are in both bin/Debug and /usr/local/lib. Not sure why everything magically works now??
    – rcplusplus
    Aug 21, 2019 at 1:06
  • I spoke to soon everything has broken again
    – rcplusplus
    Aug 21, 2019 at 2:03
  • 1
    Idiot of the day award should go to me because I forgot to set the platform target. So I've officially gone in a full circle...
    – rcplusplus
    Aug 21, 2019 at 2:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.