如何在Android和iOS上使用相同的C ++代码? [英] How to use the same C++ code for Android and iOS?

查看:190
本文介绍了如何在Android和iOS上使用相同的C ++代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android with 代码,因此CoreWrapper.mm文件。

  #importCoreWrapper.h

@implementation CoreWrapper

+(NSString * )concateneMyStringWithCppString:(NSString *)myString
{
return [NSString stringWithUTF8String:concateneMyStringWithCppString([myString UTF8String])];
}

@end


  • 核心包装器在正常的视图控制器上,首先是View Controller头,这样ViewController.h

      #import< UIKit / UIKit。 h。 
    #importCoreWrapper.h

    @interface ViewController:UIViewController
    @end

    li>

  • 现在的ViewController.mm文件:

      #importViewController.h 

    @interface ViewController()

    @end

    @implementation ViewController

    - (void)viewDidLoad
    {
    [super viewDidLoad];

    //一个普通的UITextView
    UITextView * txtHelloCpp = [[UITextView alloc] initWithFrame:CGRectMake(0,140,320,20)];
    //这里我们用共享的cpp代码设置文本。
    [txtHelloCpp setText:[CoreWrapper concateneMyStringWithCppString:@Objective-c]];
    //正常的东西
    [self.view addSubview:txtHelloCpp];
    [txtHelloCpp release];
    }

    @end


  • 所有为IOS,现在android转。要在Android上执行此操作,您需要,如果您了解葡萄牙语,您可以在 google docs here


    Android with NDK have support to C/C++ code and iOS with Objective-C++ have support too, so I want see a example of a program with native C/C++ code shared between Android and iOS.

    Observation: I'll go answer this question following the Q&A-style.

    解决方案

    Here a diagram of what i'll do:

    So follow the steps and let's go:

    1. Make a core.h file, here we define the method concateneMyStringWithCppString that runs on both Android as the iphone, this method will receive a string from android or iphone and will concatenate with a comum cpp string and return this concatened string.

      #ifndef __HelloCpp__Core__
      #define __HelloCpp__Core__
      
      #include <iostream>
      
      const char* concateneMyStringWithCppString(const char* myString);
      
      #endif /* defined(__HelloCpp__Core__) */
      

    2. Now the core.cpp file with the method implementation, we have a string "CPP_BASE_STRING" and will concatenate it with the param "const char* myString", and return it. The myString will be delivered by or through android or ios.

      #include "Core.h"
      
      const char* CPP_BASE_STRING = "cpp says hello world to %s";
      
      const char* concateneMyStringWithCppString(const char* myString) {
      char* concatenedString = new char[strlen(CPP_BASE_STRING) + strlen(myString)];    
          sprintf(concatenedString, CPP_BASE_STRING, myString);    
          return concatenedString;
      }
      

    3. So we need a wrapper to IOS and other to android, first will go make of IOS wrapper, first write thee wrapper header, CoreWrapper.h:

      #import <Foundation/Foundation.h>
      #import "Core.h"
      
      @interface CoreWrapper : NSObject
      
      + (NSString*) concateneMyStringWithCppString:(NSString*)myString;
      
      @end
      

    4. Now the wrapper code, it need be a Objective-C++ code, so CoreWrapper.mm file.

      #import "CoreWrapper.h"
      
      @implementation CoreWrapper
      
      + (NSString*) concateneMyStringWithCppString:(NSString*)myString
      {
          return [NSString stringWithUTF8String:concateneMyStringWithCppString([myString UTF8String])];
      }
      
      @end
      

    5. And use the core wrapper on a normal view controller, first the View Controller header, so ViewController.h

      #import <UIKit/UIKit.h>
      #import "CoreWrapper.h"
      
      @interface ViewController : UIViewController
      @end
      

    6. And now the ViewController.mm file:

      #import "ViewController.h"
      
      @interface ViewController ()
      
      @end
      
      @implementation ViewController
      
      - (void)viewDidLoad
      {
          [super viewDidLoad];
      
          // A normal UITextView
          UITextView* txtHelloCpp = [[UITextView alloc] initWithFrame:CGRectMake(0, 140, 320, 20)];
          // Here we set the text with shared cpp code.
          [txtHelloCpp setText:[CoreWrapper concateneMyStringWithCppString:@"Objective-c"]];
          // Normal things again
          [self.view addSubview:txtHelloCpp];
          [txtHelloCpp release];
      }
      
      @end
      

    7. So it is all for IOS, Now the android turn. To do it on android, you will need the Android NDK, an Android Wrapper in cpp and The application and Android make files, one thing by time:

    8. first Android.mk, you need link the shared cpp code "core.cpp" with cpp android wrapper, so in my example I did my Android.mk file like as follow.

      LOCAL_PATH := $(call my-dir)
      
      include $(CLEAR_VARS)
      
      LOCAL_MODULE    := HelloCpp
      LOCAL_SRC_FILES := CoreWrapper.cpp
      LOCAL_SRC_FILES += ../../CPP/Core.cpp
      
      LOCAL_C_INCLUDES := $(LOCAL_PATH)../../CPP/
      
      include $(BUILD_SHARED_LIBRARY)
      

    9. Note my folder folder in this examples is, "i will ignore IOS structure because the xcode organize automatically it":

      /-
       |-Android-
                |-jni -
                      |-Android.mk
                      |-Application.mk
                      |-CoreWrapper.cpp
                |-other android folders like res and src
       |-CPP-----
                |-Core.h
                |-Core.cpp
      

    10. now the small Application.mk:

      APP_STL := gnustl_static
      APP_ABI := all
      

    11. here the CoreWrapper.cpp: "important, you need know the android jni implementation"

      #include <string.h>
      #include <jni.h>
      #include "../../CPP/Core.h"
      
      extern "C" {
      
      JNIEXPORT jstring JNICALL Java_la_jurema_doses_hellocpp_MainActivity_concateneMyStringWithCppString(JNIEnv* env, jobject thiz, jstring myString) {
          return env->NewStringUTF(concateneMyStringWithCppString(env->GetStringUTFChars(myString, 0)));
      }
      
      }
      

    12. Now the implementation on java, using a normal Activity, the file MainActivity.java:

      public class MainActivity extends Activity {
      
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      
          // A normal textview
          TextView textView = new TextView(getApplicationContext());
          // set the text of textview with the string of shared cpp code
          textView.setText(concateneMyStringWithCppString("Javaaaa"));
          // normal things
          setContentView(textView);
      
          // only interface things nothng important
          textView.setTextSize(50);
          textView.setTextColor(Color.BLACK);
          textView.setGravity(Gravity.CENTER);
      }
      
      // very important
      private native String concateneMyStringWithCppString(String myString);
      static {
          System.loadLibrary("HelloCpp");
      }
      
      }
      

    So it's all, if you did all right you get something like that:

    You can get the full example on github here and if you understand Portuguese you can see my presentation of it on google docs here.

    这篇关于如何在Android和iOS上使用相同的C ++代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆