gpt4 book ai didi

android - VS2015跨平台Android和C++

转载 作者:太空宇宙 更新时间:2023-11-04 13:23:31 25 4
gpt4 key购买 nike

我正在使用 Visual Studio 2015 进行跨平台开发。我有现有的 C++ 项目,希望 android 端调用它。我读过有关 JNI 的资料,但我改用 VS2015。

所以我可以调用 C++ 非成员函数,但是我如何调用成员函数,例如我的 SharedObject1.h 文件中的 Multiply() 下面的函数。

class SharedObject1
{
public:
const char * getPlatformABI();
SharedObject1();
~SharedObject1();

int Multiply(int a, int b);
};

在我的 SharedObject1.cpp 文件中:

#include "SharedObject1.h"

#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "SharedObject1", __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "SharedObject1", __VA_ARGS__))

extern "C" {
/* This trivial function returns the platform ABI for which this dynamic native library is compiled.*/
const char * SharedObject1::getPlatformABI()
{
#if defined(__arm__)
#if defined(__ARM_ARCH_7A__)
#if defined(__ARM_NEON__)
#define ABI "armeabi-v7a/NEON"
#else
#define ABI "armeabi-v7a"
#endif
#else
#define ABI "armeabi"
#endif
#elif defined(__i386__)
#define ABI "x86"
#else
#define ABI "unknown"
#endif
LOGI("This dynamic shared library is compiled with ABI: %s", ABI);
return "This native library is compiled with ABI: %s" ABI ".";
}

void SharedObject1()
{
}

SharedObject1::SharedObject1()
{
}

SharedObject1::~SharedObject1()
{
}

int Add(int a, int b)
{
return (a + b);
}

int SharedObject1::Multiply(int a, int b)
{
return (a*b);
}
}

所以现在我可以在将 SharedObject1.so 文件添加到下面的项目然后使用 DllImport 之后在 VS2015 中从 Android 端调用函数 Add():

using System;
using Android.App;
using Android.Content;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.OS;
using System.Runtime.InteropServices;

namespace App1
{
[Activity(Label = "App1", MainLauncher = true, Icon = "@drawable/icon")]
public class MainActivity : Activity
{
int count = 1;

[DllImport("SharedObject1.so")]
public static extern int Add(int a, int b);

[DllImport("SharedObject1.so")]
public static extern int Multiply(int a, int b);

protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);

// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);

// Get our button from the layout resource,
// and attach an event to it
Button button = FindViewById<Button>(Resource.Id.MyButton);

button.Click += delegate { button.Text = string.Format("{0} clicks!", count++); };

try
{

EditText TV1 = FindViewById<EditText>(Resource.Id.editText1);
int c1 = Add(4, 6);
TV1.Text = c1.ToString();

EditText TV2 = FindViewById<EditText>(Resource.Id.editText2);
int c2 = Multiply(2, 50);
TV2.Text = c2.ToString();

}
catch (Exception Ex)
{
string msg = Ex.Message;
throw;
}
}
}
}

但我不知道如何调用类成员函数 Multiply(),因为它在调用 Multiply() 后总是抛出“System.EntryPointNotFoundException: Multiply”以外的异常。有人可以给我建议吗?谢谢。

------更新SharedObject1类------update(1)-----

#ifdef SHAREOBJECT_EXPORTS
#define SHAREOBJECT_API __declspec(dllexport)
#else
#define SHAREOBJECT_API __declspec(dllimport)
#endif

class SharedObject1
{
public:
const char * getPlatformABI();
SharedObject1();
~SharedObject1();

int Multiply(int a, int b);
};

那么我如何在 VS2015 格式中从 android 调用此类成员?

------更新SharedObject1类------update(2)------

#ifdef SHAREOBJECT_EXPORTS
#define SHAREOBJECT_API __declspec(dllexport)
#else
#define SHAREOBJECT_API __declspec(dllimport)
#endif

class SharedObject1
{
public:
const char * getPlatformABI();
SharedObject1();
~SharedObject1();

SharedObject1* getInstance()
{
SharedObject1* so = new SharedObject1();
return so;
}

int Multiply(int a, int b);
int Multiply_Obj(SharedObject* obj, int a, int b)
{
return obj->Multiply(a,b);
}
};

Multiply_Obj() 和 getInstance() 的调用者会是什么样子?

最佳答案

保持文件 SharedObject1.h 和 SharedObject1.cpp 与上面相同,并将更新 (1) 和 (2) 拉入新项目,然后项目源文件添加包含“SharedObject1.h”,这样就应该这样做。

#include "TestMathFuncLib.h"
#include "SharedObject1.h"

#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "TestMathFuncLib", __VA_ARGS__))
#define LOGW(...) ((void)__android_log_print(ANDROID_LOG_WARN, "TestMathFuncLib", __VA_ARGS__))

extern "C" {
/* This trivial function returns the platform ABI for which this dynamic native library is compiled.*/
const char * TestMathFuncLib::getPlatformABI()
{
#if defined(__arm__)
#if defined(__ARM_ARCH_7A__)
#if defined(__ARM_NEON__)
#define ABI "armeabi-v7a/NEON"
#else
#define ABI "armeabi-v7a"
#endif
#else
#define ABI "armeabi"
#endif
#elif defined(__i386__)
#define ABI "x86"
#else
#define ABI "unknown"
#endif
LOGI("This dynamic shared library is compiled with ABI: %s", ABI);
return "This native library is compiled with ABI: %s" ABI ".";
}

void TestMathFuncLib()
{
}

TestMathFuncLib::TestMathFuncLib()
{
}

TestMathFuncLib::~TestMathFuncLib()
{
}

SharedObject1* CreateTestClass()
{
return new SharedObject1();
}


void DisposeTestClass(SharedObject1* pObject)
{
if (pObject != NULL)
{
delete pObject;
pObject = NULL;
}
}


int TestMultiply(SharedObject1* pObject, int a, int b)
{
return pObject->Multiply(a, b);
}
}

现在在 android 端,只需使用 DllImport 调用 CreateTestClass(),然后将该对象传递给 TestMultiply(obj, int, int);它应该工作。

关于android - VS2015跨平台Android和C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34146388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com