Using C++ class in XAMARIN

MichaelMelamudMichaelMelamud Michae MelamudUSMember ✭✭

Suppose i have the following class compiled under GCC (for armv7) , also added the -fvisibility=default flag

extern "C" class abc
{
    private :

    int a ;

    public :

    int  getValue()
    {
        return 100;
    }
};



extern "C" int  GetCounter()
{
    abc* p = new abc();
    int i  = p->getValue();

 return i;
}

When Calling from the C# I recive DLL Not found exception , this is the call code :

[DllImport("tstcpp")]     public extern static int GetCounter();
.......
int cnt = GetCounter(); //Here i get the exception 

But when using the following code the GetCounter function works just fine

int GetCounter()
{
 int i = 123;
 return i;
}

this is the call code ( The same as above ) :

[DllImport("tstcpp")]     public extern static int GetCounter();
.......
int cnt = GetCounter(); //Here i get the exception 

Please anyone can help me and tell me why does it happen?

Thanks
Michael

Posts

  • moljacmoljac Miljenko Cvjetko HRBeta ✭✭✭
    1. Have You tried
      [DllImport("tstcpp.so")]
    2. Check if Your dll is deployed (I think this woudl be):

      adb shell ls -al /data/data//lib

    3. send mono deeper into debug universe:

      adb shell setprop debug.mono.env MONO_LOG_LEVEL=debug

    see here for some more ideas http://forums.xamarin.com/discussion/4289/interop-shared-gnu-stl/p1

  • JonathanPryorJonathanPryor Jonathan Pryor USXamarin Team Xamurai
    extern "C" class abc
    

    This is not valid C++. You cannot extern "C" a class declaration.

    Are you using multiple .so files? That may be a problem.

  • MichaelMelamudMichaelMelamud Michae Melamud USMember ✭✭

    Maybe it because its should be a managed dll ? and that why i cant use the classes?

  • moljacmoljac Miljenko Cvjetko HRBeta ✭✭✭

    Nope for managed references - no DllImport is needed.

    c has no notion of classes and c++ mangles the names of methods, so it is harder to "know" their names.

    BTW, try not scattering Your questions across several threads, so we can answer everything in less threads and cross-referencing is not necessary.

  • MichaelMelamudMichaelMelamud Michae Melamud USMember ✭✭

    Ok , sorry.

    But still what could be the problem? if it was a "mangeld" name issue i would recive entry point not found error and not dll not found exception .

    I just dont understand what is the problem when i am adding a class and trying to use a function that usses the class ?

    Is it possible at all to work with c++ native classes with XAMARIN ?

  • JonathanPryorJonathanPryor Jonathan Pryor USXamarin Team Xamurai

    Is it possible at all to work with c++ native classes with XAMARIN ?

    Yes.

    I still don't know why it's not working for you, but I locally modified an ndk-build-based project to use C++ code, and it Works For Me™:

    Android.mk

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    LOCAL_LDLIBS    := -llog
    LOCAL_MODULE    := foo
    LOCAL_SRC_FILES := foo.c cpp.cpp
    

    foo.c

    cpp.cpp:

    class MyClass {
    public:
        int value() const;
    };
    
    int MyClass::value() const
    {
        return 42;
    }
    
    extern "C" int cpp_GetValue()
    {
        const MyClass *p = new MyClass();
        int v = p->value();
        delete p;
        return v;
    }
    

    Hello.cs additions:

        [DllImport ("foo")]
        static extern int cpp_GetValue();
    
        void TestDllImport (TextView textview)
        {
            GoMonodroidGo ();
            Console.WriteLine ("# C++ value: {0}", cpp_GetValue ());
        }
    
  • alaindeloinalaindeloin alain deloin USMember

    Hi All,

    I have a question about native interrop with monotouch in a android project .

    I've the following declaration in a cpp source file : extern "C" __declspec(dllexport) int myAdditionfunction(int a, int b) and when I try to compile with ndk builder i've got the following error :" error: expected constructor,destructor or type conversion before '(' token ..." for this line.

    I search on the web but I have not found any answer...

  • LarryGugerLarryGuger Larry Guger USMember

    Hi All,

    I am from the Microsoft C++ team and we are very interested in talking to developers that are incorporating C++ libraries into their Xamarin apps. If you are doing that I would be very excited if you would reach out to me directly. My email alias is larrygug. The domain is microsoft.com. :-)

    Larry

Sign In or Register to comment.