6 ms·
Yeah, I already write cross-platform code with C++ and it is really not as bad as anyone leads you to believe. Create one single C++ entry point object that dri
by rburhum 14y ago
Yeah, I already write cross-platform code with C++ and it is really not as bad as anyone leads you to believe. Create one single C++ entry point object that drives everything else. Use sqlite for storage, jsonc for serializing/deserializing json, pthreads for threading and curl for making network requests. Your main c++ class has to also receive the events (like touch) and forward as appropriate. You can leverage STL and boost if you wish. For iOS, you end up dragging and dropping the entire project to XCode - valid C++ code is valid Objective C++. For Android, you just use the ndk to create a simple Java class that mirrors your entry point C++ class and the implementation is just JNI calls to the C++ class. It is common to have a secondary "utilities" class that does hardware specific things if you wish. Your GuI is quickly put together using the standard OS tools - but as long as you stick to forwarding the work to the main C++ class, all that GUI code tends to be minimal (except for really fancy platform specefic GuI code like transitions). Oh, and I use OpenGL for drawing most cool things, so I get to reuse that, too - not to mention all my code is really fast. There you go - cross platform iOS / Android / Mac / Linux / Windows development.
- kvnn 14y agoThis is the first I've heard of this approach and I want to hear more. Do you have any code you can share? Are there any resources in general I should check out?
- 54mf 14y agoSeconding this. Any books or articles worth reading?
- rburhum 14y agoThe Android jni documentation shows how to call C++ and is the only doc I have glanced through. If you stick to passing ints,doubles and strings through it it is really easy. For iOS is way simpler, since your wrapper is just a file that ends with ".mm". Google it. If there is enough interest I can create a small article describing the process and post a starter project on github.
- kvnn 14y ago> If there is enough interest I can create a small article describing the process and post a starter project on github. I think you'd find a lot of interested for that. I'd appreciate it a lot.
- andrewmlevy 14y agoIf you're doing game development, check out cocos-2dx. It's a c++ port of the cocos2d-iphone framework. Here's some more info: http://www.cocos2d-x.org/projects/cocos2d-x/wiki http://www.cocos2d-x.org/projects/cocos2d-x/wiki
- jrajav 14y agoI've tried using the NDK too, specifically to compile the Objective C framework ObjFW [1] on one of the few platforms where it's not yet confirmed working. Unfortunately, the gcc they include in the NDK has Objective C disabled for seemingly no reason, and bootstrapping your own gcc or clang toolchain is pretty tough. I'm sure you don't see as much of a need with C++, but have you ever tried replacing parts of the toolchain? [1] https://webkeks.org/objfw/ https://webkeks.org/objfw/
- andybak 14y ago"the gcc they include in the NDK has Objective C disabled for seemingly no reason" - I wonder how heavily they modified gcc to cope with security models etc. Quite possibly there's a very good reason. It might have been a ton of work to get it working properly.
- kkowalczyk 14y agoThere is a very good reason for disabling Objective C in NDK. Objective C, the syntax, has very little value without Objective C runtime/standard library and Objective C runtime/stdlib is huge (compared to C and C++ standard libraries). NDK has a very specific goal: if the code would be too slow when compiled to Dalvik VM, you can get speed with NDK, writing in C/C++, but at the cost of loosing access to almost all of the APIs that Android provides. This tradeoff more or less makes sense only for games. Implementing Objective C runtime and standard library would be huge undertaking and the code, when used, would occupy more memory. Also, this code would mostly duplicate C++ standard library, just with a different syntax. It doesn't make sense to allow people using Objective C in NDK.
- chj 14y agoI am afraid that debugging your c++ code on Android will be a lot of pain.
- jrajav 14y agoThe NDK includes gdb.
- rburhum 14y agoIt is a pain to debug C++ on Android even if gdb comes with the ndk. Practically speaking, it turns out debugging C++ code can be done on any platform (Xcode on Mac, codeblocks on Linux, whatever). My entry point C++ class has a unit test that tests all the components... I have yet to find a significant bug that was not reproducible on another platform one way or another. For the record, if you dont want to figure out the NDK gdb setup mess, printf outputs to logcat.
- sshumaker 14y agoToss in lua(jit) and you have a nice dev environment. I wrote a lua->java bridge and modified LTC (lua tiny cocoa, a lua objective c bridge) and with those you can write nearly your entire cross-platform app (ui, too) in C / Lua. And with luajit's FFI support, it's almost a better C than C.
- rburhum 14y agoInteresting. I do like to use the native gui Api for each platform to get the native look/feel. Does lua/jit have its own implementation of buttons, tableviews and such or does it use the native controls of the system?
- sshumaker 14y agoWell, that's the point of the java and objective-c bridge: it lets you call out to native APIs, so you have all native components.
- erichocean 14y agoThere you go - cross platform iOS / Android / Mac / Linux / Windows development. The only thing missing is...the browser. We're doing the same thing, but with JavaScript as the application code, and unique front-ends for HTML5 browsers, older browsers, Android, iOS, mobile browsers, etc. It's trivial to get a JavaScript VM running on any given platform.
- rburhum 14y agoWhy do you say the browser is missing? My app, in certain parts, uses the native OS's Webkit views for rendering all kinds dynamically generated mustache forms. In fact, there are all kinds of javascript hooks that call native OpenGL code.