✅ Help with using C++ library in C# code

hello, I am trying to use a c++ library using a .dylib (mac dll) and its giving me a weird error that i can't find a lot of info on, maybe because its unique to osx?
willow@willows-mbp csharp % set DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/lib/darwin/libclang_rt.asan_osx_dynamic.dylib
willow@willows-mbp csharp % dotnet run
==10641==ERROR: Interceptors are not working. This may be because AddressSanitizer is loaded too late (e.g. via dlopen). Please launch the executable with:
DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/lib/darwin/libclang_rt.asan_osx_dynamic.dylib
"interceptors not installed" && 0
willow@willows-mbp csharp % set DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/lib/darwin/libclang_rt.asan_osx_dynamic.dylib
willow@willows-mbp csharp % dotnet run
==10641==ERROR: Interceptors are not working. This may be because AddressSanitizer is loaded too late (e.g. via dlopen). Please launch the executable with:
DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/clang/15.0.0/lib/darwin/libclang_rt.asan_osx_dynamic.dylib
"interceptors not installed" && 0
as u can see I tried setting the environment variable to the asan dylib but it didn't work. Idk why asan is coming up at all, I compiled the dylib without address sanitization. The error only occurs when I try to call this function:
[DllImport("libSpaceGame.dylib")]
public static extern void Run();
[DllImport("libSpaceGame.dylib")]
public static extern void Run();
ive never tried to use c++ with c# before, so im very new to this. Any insight is appreicated! The dylib is in the correct folder, as it complained about that before and I moved it
10 Replies
jcotton42
jcotton422mo ago
Are the functions in the header in an external C block?
mtreit
mtreit2mo ago
I don't know much about this topic but don't use you export to set an environment variable?
butteredcoffee
butteredcoffeeOP2mo ago
honestly idk much about environment variables [LibraryImport("libSpaceGame.dylib")] also tried this and it gave the same error just to test, I am compiling this:
#include <stdio.h>

void run()
{
printf("test");
}
#include <stdio.h>

void run()
{
printf("test");
}
sorry i dk if that answers ur question
jcotton42
jcotton422mo ago
Is that a .c or .cpp file? But also I don’t see the necessary stuff to export the function.
butteredcoffee
butteredcoffeeOP2mo ago
.cpp oh i need to export it in cpp how would I do that do you have a resource? im not finding what I need
jcotton42
jcotton422mo ago
$cpp
MODiX
MODiX2mo ago
We're partnered with Together C & C++, check them out here: https://discord.gg/tccpp
butteredcoffee
butteredcoffeeOP2mo ago
oh lol fair
butteredcoffee
butteredcoffeeOP2mo ago
Medium
Bridging Worlds: A Practical Guide to C++ and C# Integration
In the world of software development, choosing the right tool for the job is paramount. C# and the .NET ecosystem offer incredible…
butteredcoffee
butteredcoffeeOP2mo ago
ok it worked with the proper c++ thanks!
#define API __attribute__((visibility("default")))

extern "C" {
API void Run() {
printf("HELLOOO");
}
}
#define API __attribute__((visibility("default")))

extern "C" {
API void Run() {
printf("HELLOOO");
}
}
just for good answer practice I used this code on mac

Did you find this page helpful?