Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DllNotFoundException running C# sim86_test.cs (Ubuntu) #22

Closed
ethanfischer opened this issue Apr 13, 2023 · 13 comments
Closed

DllNotFoundException running C# sim86_test.cs (Ubuntu) #22

ethanfischer opened this issue Apr 13, 2023 · 13 comments

Comments

@ethanfischer
Copy link
Contributor

ethanfischer commented Apr 13, 2023

Hi,

When I follow the instructions at the top of sim86_test.cs, ie. copying the sim86_shared_debug.dll next to sim86.cs and sim86.test, etc, and running dotnet run from that directory, I get the following exception:

Exception has occurred: CLR/System.DllNotFoundException
An unhandled exception of type 'System.DllNotFoundException' occurred in sim86.dll: 'Unable to load shared library 'sim86_shared_debug' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libsim86_shared_debug: cannot open shared object file: No such file or directory'
   at Sim86.Native.Sim86_GetVersion()
   at Sim86.GetVersion() in /home/ethan/repos/computer_enhance/perfaware/sim86/shared/contrib_csharp/sim86.cs:line 291
   at Program.<Main>$(String[] args) in /home/ethan/repos/computer_enhance/perfaware/sim86/shared/contrib_csharp/sim86_test.cs:line 23

I'm new to calling into c++ dlls so maybe I'm missing something obvious but figured I post here in case anyone can help.

image

I'm running it in vscode on Ubuntu 20.04

@mmozeiko
Copy link

For non-Windows platforms you will need to build .so file manually. The .dll file is provided only for Windows.
When you build it with all symbols exported, then having it named libsim86_shared_debug.so should work with current C# code.

@ethanfischer ethanfischer changed the title DllNotFoundException running C# sim86_test.cs DllNotFoundException running C# sim86_test.cs (Ubuntu) Apr 14, 2023
@ethanfischer
Copy link
Contributor Author

ethanfischer commented Apr 14, 2023

Aha, I had a feeling it was a linux issue. Should I be building the file sim86_lib.cpp? I ran these two commands

gcc -c -fPIC sim86_lib.cpp -o sim86_lib.o
gcc -shared sim86_lib.o -o sim86_shared_debug.so
then copied the outputed sim86_shared_debug.so into the contrib_csharp folder and ran dotnet run, but I'm still getting this error

Unhandled exception. System.DllNotFoundException: Unable to load shared library 'sim86_shared_debug' or one of its dependencies. In order to help diagnose loading problems, consider setting the LD_DEBUG environment variable: libsim86_shared_debug: cannot open shared object file: No such file or directory
   at Sim86.Native.Sim86_GetVersion()
   at Sim86.GetVersion() in /home/ethan/repos/computer_enhance/perfaware/sim86/shared/contrib_csharp/sim86.cs:line 291
   at Program.<Main>$(String[] args) in /home/ethan/repos/computer_enhance/perfaware/sim86/shared/contrib_csharp/sim86_test.cs:line 23

image

@mmozeiko
Copy link

Can you try renaming it to libsim86_shared_debug.so ?

@ethanfischer
Copy link
Contributor Author

Oh yeah, I did that as well. Still no luck

image

@mmozeiko
Copy link

mmozeiko commented Apr 14, 2023

Ok, then one last thing - can you try putting it into contrib_cshasp/bin/Debug/netX.X/ folder? So it is next to sim86.dll inside there.

Alternatively setup LD_LIBRARY_PATH - do

export LD_LIBRARY_PATH=/path/to/computer_enhance/perfaware/sim86/shared/contrib_csharp

before running IDE or dotnet run command.

@ethanfischer
Copy link
Contributor Author

Putting it in contrib_cshasp/bin/Debug/netX.X/ worked! Thank you so much

@KimboTodd
Copy link

KimboTodd commented Jun 8, 2023

For anyone still getting an error. I'm running this on a Mac and was able to solve with a tweak to the above instructions:

gcc -c -fPIC sim86_lib.cpp -o sim86_lib.o
gcc -shared sim86_lib.o -o sim86_shared_debug.so

Gave me the error:

In file included from sim86_lib.cpp:25:
./sim86_decode.cpp:46:18: error: scalar initializer cannot be empty
    u32 Result = {};
    
./sim86_decode.cpp:194:29: error: expected expression
                   Term0 = {};
                            ^

This appears to be remedied by setting the version of C++ like so -std=c++20

gcc -c -fPIC -std=c++20 sim86_lib.cpp -o sim86_lib.o  
gcc -shared -std=c++20 sim86_lib.o -o sim86_shared_debug.so

FYI: There was no output on the console, but there were new files. I am unfamiliar with C++ so I don't know if this is a solution or a hacky workaround that will bite me later. Feel free to let me know if you know of a better way. Running clang++ --version my version of clang is:
Apple clang version 15.0.0 (clang-1500.0.28.1.1)

Then copy the file sim86_shared_debug.so into contrib_cshasp/bin/Debug/netX.X/ and rename to libsim86_shared_debug note there is no extension

@mmozeiko
Copy link

mmozeiko commented Jun 8, 2023

On Mac the extension for shared libraries should be .dylib not .so. Then there should be no need to modify dll constant value - as .NET automatically appends correct extension (.dll for Windows, .so for Linux and .dylib for macOS)

@KimboTodd
Copy link

KimboTodd commented Jun 8, 2023

@mmozeiko I just tested out removing the .so from the dll constant value and I get the error:

   at Sim86.Native.Sim86_GetVersion()
   at Sim86.GetVersion() in /Users/kimtodd/repos/computer_enhance/perfaware/sim86/shared/contrib_csharp/sim86.cs:line 290
   at Program.<Main>$(String[] args) in /Users/kimtodd/repos/computer_enhance/perfaware/sim86/shared/contrib_csharp/sim86_test.cs:line 23

Putting .so extension back in the constant fixes this error and I get the output:

 contrib_csharp git:(main) ✗ dotnet run                                                                                                                                                            725ms
Sim86 Version: 4
8086 Instruction Instruction Encoding Count: 133
Size:2 Op:add Flags:0x00000008
Size:3 Op:add Flags:0x00000008
....

Hmmm. If I remove .so from the constant and from the file itself everything continues to work 👍

Also, thanks for creating this wrapper and helping out in this issue. I really appreciate it!

@mmozeiko
Copy link

mmozeiko commented Jun 8, 2023

And did you build the binary with .dylib extension? gcc -shared sim86_lib.o -o sim86_shared_debug.dylib

@KimboTodd
Copy link

KimboTodd commented Jun 8, 2023

No. Should I? Is that better to go back and redo it, even if it's already working?

I have very very little idea how c++ works. Thanks for understanding. 😀

@mmozeiko
Copy link

mmozeiko commented Jun 8, 2023

As I said before - that's the extension for shared libraries on macOS. .so is for Linux. .dylib is for macOS. And .dll for Windows.

@KimboTodd
Copy link

I went ahead and rebuild with gcc -shared -std=c++20 sim86_lib.o -o sim86_shared_debug.dylib and it still continues to work. Seems like the output -o filename has no effect on the actual contents. Putting sim86_shared_debug.dylib or sim86_shared_debug into the contrib_cshasp/bin/Debug/netX.X/ folder both work. Today I learned.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants