2

I am very much new to cocos2dx. I have a cocos2dx project made in xcode. for gesture recognizers I used cocoa touch's native code(UIGestureRecognizers) in cocos2dx using .mm files.

Now I want to build this project with android sdk too.

The Error i am getting by building the android project using build_native.sh file, in terminal is Like The Following

1

../android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/HelloWorldScene.o: in function HelloWorld::menuCloseCallback(cocos2d::CCObject*):jni/../../Classes/HelloWorldScene.cpp:88: error: undefined reference to 'XBridge::doSth()'

2

../android-ndk-r9d/toolchains/arm-linux-androideabi-4.6/prebuilt/darwin-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld: ./obj/local/armeabi/objs/cocos2dcpp_shared/__/__/Classes/HelloWorldScene.o: in function HelloWorld::menuCloseCallback(cocos2d::CCObject*):jni/../../Classes/HelloWorldScene.cpp:85: error: undefined reference to 'XBridge::imageName'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libcocos2dcpp.so] Error 1
make: Leaving directory `../cocos2d-x/projects/MyGame/proj.android'

What Am I missing Here ? I just started learning cocos2dx. Can i even use the native iOS code like this in an android project ? or i am just shooting blanks ?

My Code is as written below.

CODE IN MY HelloWorldScene.cpp

void HelloWorld::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
    cocos2d::CCString * imageNameString = cocos2d::CCString::create("image.png");

XBridge::imageName = imageNameString->getCString();
XBridge::doSth();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    //exit(0);
#endif
#endif
}    

Code In XBridge.h

#ifndef xbridge_XBridgeViewController_h
#define xbridge_XBridgeViewController_h

#include "cocos2d.h"

class XBridge {

public:
    static std::string imageName;
    static void doSth();
};

#endif

Code In XBridge.mm

#include "XBridge.h"
#include "AppController.h"
#include "RootViewController.h"
#include "SpriteVC.h"

using namespace cocos2d;
std::string XBridge::imageName;
void XBridge::doSth()
{
    id sth = [[UIApplication sharedApplication] delegate];
    if ([sth isKindOfClass:[AppController class]])
    {

        printf("XBridge::doSth imageName %s\n",imageName.c_str());

        SpriteVC *SPVC = [[SpriteVC alloc] initWithNibName:nil bundle:nil];

        SPVC.imageNameString = [NSString stringWithFormat:@"%s",imageName.c_str()];
        [SPVC setUpImage];
        NSLog(@"XBridge::doSth imageName == %@",[NSString stringWithFormat:@"%s",imageName.c_str()]);

        //SPVC.imageView.frame = CGRectMake(480, 320, 333, 333);

        AppController *controller = (AppController *)sth;

        [controller.viewController.view addSubview:SPVC.photoImage];
    }
}
Pawan Joshi
  • 1,581
  • 3
  • 20
  • 40
  • Have you done the android env configuration? Installing android sdk and setting the bash variables? –  Jun 19 '14 at 07:46
  • @Mayerz : yes absolutely, the rest of the class are getting build perfactly. doesn't seems to find reference for this "undefined reference to 'XBridge::doSth()'" Method only – Pawan Joshi Jun 19 '14 at 07:47
  • 1
    iOS code will not work in android. u need to write seprately for android. check this link:- https://developer.android.com/training/gestures/index.html – Sri Jun 19 '14 at 07:49
  • And maybe check this also, http://stackoverflow.com/questions/1061005/calling-objective-c-method-from-c-method –  Jun 19 '14 at 07:50

1 Answers1

0

You can't use iOS Cocoa Touch API for Android even though you actually can compile Objective-C and Objective-C++ code using Android NDK Clang toolchain. Because Android NDK doesn't provide iOS API at all like UIApplication, AppController, and so forth. Cocos2d-x just provides Cocos2d-x API in C++ and some plugins for Android.

If you want to use iOS API for Android, take a look at Apportable.

Kazuki Sakamoto
  • 13,929
  • 2
  • 34
  • 96
  • Apportable is sort of not a 'free' thing anymore btw. It will be again someday I am told. They are moving their SDK into SpriteBuilder where they can attempt to monetize their porting efforts. – Hunter-Orionnoir Sep 22 '14 at 19:08