Hxcpp 1.0, neash 1.0, NME 1.0
The release this week of haXe version 2.0.4 officially includes c++ as a build target, for Windows, Mac, Linux and iPhone. You can download and install from haxe.org. In addition to the standard includes, you will need the “hxcpp” library, which can be insatlled with the included haxelib management tool.
Coincident with the hxcpp release, I have updated the neash and NME libraries to versions 1.0. You can also download these via the haxelib tool too. There are several incrental improvements, and the iPhone target has been added!
Getting started with the iPhone
Getting started with the iPhone is quite tricky at the moment, mainly because of the pain of setting up an Xcode project. Also, getting the simplest program onto the device is hard due to the code signing requirements. So if you can already get one of the existing application templates to work, you are half way there.
Note that this solution uses the “SDL” library, and must statically link against this. SDL is covered by the LGPL license, and this has implications should you choose to release your software. I am hoping to remove the LGPL restiction at a later date.
The binaries used here are have been compiled for the “2.2.1″ iPhone SDK. So choose this version when compiling for simulator or device.
- Download and install components
- Get haxe & neko: Visit haxe.org
- Get hxcpp: haxelib install hxcpp
- Get nme: haxelib install nme
- Get neash: haxelib install neash
- Get the sdl-static libs for iphone: I have created a project with binary builds of these. You can get the latest builds directly from subversion svn code at:
http://code.google.com/p/sdl-static/source/checkout.
Or get the snapshot bundle from this site and install somewhere handy:
sdl-static-iphone-1.0.zip
- Get Xcode with iphone sdk support – visit apple.com
- Get a Developer key (you can try simulator without it). You will need to pay to sign up as a developer on the apple site.
-
Fire up Xcode and do File > New Project.
Choose iPhone OS > Application. Here choose a “Windows-Based Application”
but infact we will use the delegate setup in the SDL code, so we will have
to delete the one created by the wizard.Select a name & directory for the project. I’m calling it “Haxe Test”.
Now as it stands, you should be able to build for the Simulator and
get a lovely white screen and a program called “Haxe Test” in the simulator
start screen.Next thing is to delete(to trash) the “…AppDelegate.h” “…AppDelegate.m”,
the “Nib Files” group, Resources/MainWindow.xib and “main.m”.
Finally, select the “Haxe Test” executable (in the Targets section) and from the “Get Info” –
“Properties” tab, clear the reference to “MainWindow”.We will add replacements for these soon.
-
Add “main.cpp” from the NME project.
Select the top-level project folder and then use Action > Add > Existing Files.
It is probably in /usr/lib/haxe/lib/nme/1,0/ndll/iPhone/ or
similar depending on which version of NME you have installed. It can be
very painful to get xcode to load from this location, unless you hit
Command-Shift-G at the “Add” dialog and type (at least some) of this filename in.
Choose to “Copy to destinations folder” so
that you can mess with it if you wish. Note: you need to have a cpp mainline
in order to automatically link in the correct runtime libraries. -
Add the libNME.iphoneos.a and libNME.iphonesim.a files from the haxelib NME project.
You can add them both and the linker
will select the correct on depending on your build. They are in the same place
as main.cpp, you you should be able to use “iPhone” from the pull-down box
in the add dialog. Probably best not to copy these files – in case you want
to change them at some stage. -
Add the whole sdl-static/lib/iPhone directory.
Again probably best not to copy.
I used the “Recursively create groups” option. These will be where you stored them
in step 1. -
Add the whole hxcpp/bin/iPhone directory like above.
Again, this will
be in a path like /usr/lib/haxe/lib/hxcpp/1,0,2/bin/iPhone/. -
Add the hxcpp include directory to the include path.
Use the “Info” button
to get the project properties, and on the build tab, under “Search Paths”
add something like /usr/lib/haxe/lib/hxcpp/1,0,2/include/ to “Header Search Path” - Now we are ready for the haxe code. If you have and existing project,
then you can adapt the following instructions.Create a new file from Xcode (Other/Empty File] Here I have called it “HaxeTest.hx”, and unticked the “Targets” option. I’m prety sure there is a way to get “Haxe File” to appear as on option here – but I don’t know the details.
In the haxe file, enter something like (Note the window size):
import flash.display.Sprite; import flash.display.Shape; class HaxeTest extends Sprite { public function new() { super(); flash.Lib.current.addChild(this); var circle:Shape = new Shape( ); circle.graphics.beginFill( 0xff9933 , 1 ); circle.graphics.drawCircle( 0 , 0 , 40 ); circle.x = 150; circle.y = 200; addChild( circle ); } static public function main() { neash.Lib.mOpenGL = true; neash.Lib.Init("HaxeTest",320,480); neash.Lib.SetBackgroundColour(0x447733); new HaxeTest(); neash.Lib.ShowFPS(); neash.Lib.Run(); } }This is the “main” file for haxe, and the hxcpp compile will create a library matching
this class name. - Set up a build script to build changes you make to your haxe files into a library.
Xcode has a few issues with a straight custom build script order due to incorrect
dependency checking. This can be worked around by first adding a custom target.Highlight the “Targets” in the Groups & Files and use the “Action > Add > New Target..”
Choose “Other > Shell Script Target” and call it something like “Compile Haxe”.
Close the pop-up and go back to the explorer. There should be a “Run Script”
entry under the “Compile Haxe” target if you expand it out.Get info on “Run Scipt” and enter the following script
if [ "$CURRENT_ARCH" = "i386" ] then haxe -main HaxeTest -cpp cpp -lib neash -lib nme --remap neko:cpp --remap flash:neash -D iphonesim else haxe -main HaxeTest -cpp cpp -lib neash -lib nme --remap neko:cpp --remap flash:neash -D iphoneos fi
You can untick the “Show Environment” if you do not need to debug this.One last step – drag the “Compile Haxe” target into the “Haxe Test” target.
It should now also show up as first item “under” the “Haxe Test” target.
The build order should now be correct. (See image at end of post) - Now you are ready to do the build. The first time you build, the build
results will show “Running custom shell script…” for quite a while.
Haxe compiles to cpp very quickly, but it take a while for the cpp files
to compile to a library. You can see the progress if you expand out the
middle tab bit.At this stage, you should get a bunch or errors when linking, but also haxe
should have created a library for you. Add this library to the project -
it should be in the local cpp/HaxeTest.iphonesim.a. - Compiling now gets a bunch of unresolved functions from frameworks.
Add the following frameworks to the project (Add > Existing Frameworks):- QuartzCore
- OpenGLES
- AudioToolbox
These can be found in /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS2.2.1.sdk/System/Library/Frameworks/.
- Run!
So you should be good to go. Open up the debug console so you can see
any traces/printfs. - Change the target to “Device – IPhone OS” from the pull-down and hit “Build and Go”.
Again, this takes quite a while the first time.
Now add the new cpp/HaxeTest.iphoneos.a library to the project. - Now you need to sort out your code signing. If you have not done so already,
setup you apple developer account & certificates on the apple web site.
Go to the info of the “Haxe Test” executable and the “properties” tab.
Change the “Identifier” to match one of your cerificates. Make sure to
match your company URL. You may want to use “*” when creating your
profile for easy changing.Under the “Build” tab, under the “Code Signing” bit
in the “Any iPhone Device” pull down your profile. If you don’t have one then
you will need to create one on the apple website. - Connect up your iPhone(iPod touch) and build! W00t!
I have had all sorts of errors when trying to upload to the device.
So far, they have been solved by getting out of the car, walking around it and getting back in.
ie, Disconnect and power down ipod. Fully exit Xcode and the start it all up and try again. Also, uninstalling the app from the “Windows > Orgainiser” directory can help.
But now the easy bit. Change to HaxeTest.hx file, and hit Build & Go. It is that simple.
Errors should show up nicely in xcode.
You can add data files (eg, pngs, xml etc) to the project and they will be copied to device so you can open them with a relative path.
In the properties of the “Info.plist” you can set a Icon File – don’t forget to add the icon to the project too.
Not covered here (because I have not fully sorted it out myself):
- Syntax highlighting in XCode
- Debug build (hxcpp can do then – it’s a matter of setting up Xcode)
- Code completion in Xcode
- Automating this procedure!
Edit: Add framework path, SDL version, MainWindow clearing.

Hi Matthew,
Did you try compiling for the “2.2.1″ device, and then running on your 3.0 device? I thought that this would be the most compatible way. However, if it is crashing, I will upgrade sdl-static to 3.0 too. You could try this yourself if you like there is an “IPHONE_VER” in the sdl makefile – you could edit this, and then type “make”. A few people have had this issue, so I will address it soon.
Huge
Yeh, 2.2.1 apps work under OS 3.0, this is definitely the way to go to work on all iPhones.
I thought I’d tried pretty much all combinations trying to get it to work, but it wouldn’t surprise me if I’d missed one! Thanks for the advice and I’ll try it out right away.
Nice Work!
Can we run haXe code in landscape mode?
Have you know any method to use touchscreen (single- or multi-touch), pvr textures and accelerometer with haXe?
Hi Jozsef,
You can run in landscape more by applying a rotation and translation to your game. I’m not sure if fonts will work properly though – will have to check it out.
The touches on the screen show up as a mouse event. The accelerometer shows up as a 3-axis joystick. PVR textures ? are you talking about compressed ones? There is no api to do this at the moment, although under the covers, it uses OpenglES textures, which do support some compression.
Thanks for your aswers!
“PVR textures ? are you talking about compressed ones?”
Yes. We have games with 2048×2048 multi-layered backgrounds – sliced up to four 1024×1024 textures – and we would like to port it to the iPhone using the compressed textures (PVRT).
It would require a small change to the “NME” library – something along the lines of “create texture from compressed data”. Or do you create it from normal RGB data and ask opengles to compress it? Either way if you do not frequently update it, it should not be too complicated to add.
Huge you said they come up as MouseEvent’s, can you provide a work example as I’ve failed to get any to register.
Wow, impressive work!
Jarrad,
I listen MOUSE_DOWN and MOUSE_UP events from the stage and then use the stage.mouseX and stage.mouseY to get the coordinates on MOUSE_MOVE.
Huge,
You said that the accelerometer shows up as a 3-axis joystick. Can we get these values in Haxe?
Hi,
Yes, I think the mouse move events are not coming through because the mouse is “Down” – so the mouse position is a good work-around for now.
The joystick is in nme.Joystick. You will have to “#if !flash” it at the moment, because there is no neash wrapper.
You basically get the world’s “gravity” acceleration resolved along the devices axis.
var joystick = new nme.Joystick(0);
trace( joystick.getAxis(0),joystick.getAxis(1),joystick.getAxis(2));
You can divide each value by 0x7fff because SDL scales them like this.
ahhhh, i had my eventlsiteners on displayobjects. putting them onto stage works perfectly.
Accelerometer is awesome too
@ Phil Harvey
I have experienced pretty much the exact same thing as you.
When I launch the compiled Simple.exe ,it opens up in a window but crashes immediatly.
The same happens if I compile all the other provides Samples (thank Huge !).
But if I write a most basic HelloWorld.hx with some basic math operations and String concats the compiled HelloWorld.exe runs as expected (no window of course).
Is it more probable that I am missing some files during compilation or should I look for problems in neash?
I will write a slightly more advanced prog without neash library,…
Please let me know if you made any progress!
o.krylow(at)gmx.de
Bye, O.
Hi Oli,
I have just released a 1.0.6 version of HXCPP, I hope this fixes the problem.
Hi Huge,
unfortunatelly not, it got worse
.
Now I get compiler errors about “unresolved external symbols”.
They all revolve around the hxGC files.
After compiling a second time (Simple.hx out of the Samples folder) with the -DINTERNAL_GC flag set I got the exact same errors.
(I stored the compiler output in both cases, if you’d like to see…)
Is this problem platform/cpu specific or have I simply overlooked to setup something the right way?
Nonetheless, in my opinion HXCPP is breathtaking, I’m simply amazed! Thanks for sharing it!
Oli, try completely deleting the directory that hxcpp creates (usually “cpp”) so all the obj files get completely recreated.
Hugh
Actually, found a silly bug for the unresolved – I’ve updated to 1.0.7
Huge
YESSS !
Simply amazing.
If you could spare the time, would you explain a little about what is going on behind the secenes in hxcpp (just in general terms).
I am really curious how this works! And tons of questions:
How can we use existing c++ libraries with hxcpp?
Out of curiousity I tried to convert a c++ sample library from adobe with alchemy to a swc file. then using –gen-hx-classes haxe provided the hx header files…
Unfortunatelly ,when I compiled to swf I got 2 runtime errors and when compiled to cpp a fatal compiler error.
I am guessing there is an easier way using the cpp API, isn’t there?
I apologize ,if my questions seem silly. I am still trying to wrap my head around C++… (and Haxe for that matter;) )
Good nigth, O.
Great work man! I’m hungry for more!
I’ve ported my game from as3 to haxe->as3 and now I would like to also test haxe->cpp. I managed to get the compiler to compile smoothly, but the main problem are resources. I’m using FlashDevelop and embeded resources.
What method for dealing with resoruces do you propose, so the code would be usable in both flash and cpp compilation targets?
Oli, try completely deleting the directory that hxcpp creates (usually "cpp") so all the obj files get completely recreated.
Hugh…
Great job Huge! I will install it and have a try when I have some time. Hope I can start some game dev on XCODE.
Pingback: Objective-C migration « Iphone SandBox
This looks really interesting. Curious, does the API coverage include the network elements for flash? I would love to be able to compile executables that can connect to FMS and sync with RSOs, etc.
Milo,
Sorry, I have not dome much in the wait of network API. haxe has some built-in http/socket access, but I have not looked at the potocols.
Hi yfan,
You can use the haxe “resources” to embed data into flash and c++ executables, but I don’t think this is the most efficient.
I think you will need some platform-dependent code to load from a file on the iPhone, and from swf-mill embedded resources on flash.
You could make a reasonably simple class to handle the differences for you – infact I have started on one, but its not releasable yet.
Hugh
What a great job. The only issue I have and probably a lot of people is the need for an apple to port it too. Oh and I need to charge the iPod Touch and update it (its been flat for a couple of months).
Thanks for your answer. It seems that I’m not going to get away with not using command line compilation.
Looking forward for your class for cross platform resource management.
Can you post any kind of timeline for upcoming hxcpp releases?
Cheers
.
Is there any chance you could share the whole xcodeproj folder?
(Got 2887 errors when building
)
Thanks …
A good news.
According to the SDL website, an alternative commercial license will be available when SDL 1.3 is released.
Good grief this is exciting!
Jarrad, I had the same problem with garbled images. It looks like somewhere in neash or SDL it is assuming your images have an alpha channel. I switched to a PNG with alpha and now it comes out fine. There is one non-obvious trick to make PNGs work at all though. Xcode by default does some modification to PNG files when building an iPhone app. To disable that, select the image in the group tree on the left, press Cmd-I and then in the General tab change the File Type from image.png to just image.
Hey I’m developing my app in Windows using Visual C++ with http://www.DragonFireSDK.com and it’s going great, do you have any experience with that program? DragonFireSDK uses a C++ layer to call Obj-C functions, it has nice documentation and the API is easy to learn.
Is gamehaxe for Windows?
Pingback: OOgtech.org » Getting Started with haXe
Hi Huge,
the possibility to use HAXE for develop iPhone app is very cool, my compliments.
Unfortunately I have issues running this example in Simulator (3.0, 3.1, 3.1.2), the app start and close. I have traced and the app exit when run this code: neash.Lib.Init(“HaxeTest”,320,480);
The IPHONE_VER in BuildCommon.xml is set to 3.1.2
I have also 2 others questions:
- what’s the status of the project? your last post was in August.
- you know if there are apps submited in AppStore build with Haxe-c++?
Hi monty,
Funny you should mention this. I’ve been working in phases, where I will work on one project for a while to get momentum going and finish a chunk off before moving on. I’m just putting the finishing touches on the latest hxcpp backend, and about to re-start work on the iPhone project, and hopefully I can give this a new boost of energy. So as for the status, I would say “soon to be very active again”.
I don’t think there is anything yet – currently you would need to license libSDL for that. However, I hope to resolve this and help get something to the app store soon – now I have freed up a bit more time.
Hugh
Hi,
Re: crash in 3.0 device. I think I’ve fixed this now – you will need the last haxe, hxcpp, nme, neash & sdl-static. Also, I had to compile for “3.0″ device, not “3.1″ device otherwise I go a jump-out-of-range error.
Hugh
Hi Huge,
thanks for the answers.
I have tried to update all but still have issue, however I’ll try to remove all and restart clean project.
I glad to heard that the project will continue, a question about SDL, the v 1.3 have changed license now is commercial for Iphone the price is not so hight but I think that will be great to have all opensource and free, what about openframework? can be a valid alternative.
If you need beta-tester send me a email.
Monty
Hi Monty,
If you can compile the sdl-static yourself, then you should get the line-number (or at least function name) that caused the crash – that would help.
I have recently been compiling for the iPhone 3.0, so it can be made to work.
As for libraries, I’ve started a rewrite that will not use SDL for iphone. Hopefully it will also be quite a bit more efficient than the current NME so it should be good all around.
Hey Hugh,
I’ve the latest XCode with 3.1.2 iPhone SDK, all libraries the latest version (obviously – I did download them today), and everything went pretty much fine until step 12…
Yes, it was compiling for a minute or so. It DID throw all those errors, mainly in the “CompileHaxe”, but one or so also in the main project file.
You said it was OK, and the haXe should’ve created a library in “local cpp”… What?
Where can I find this library again?
Thanks.
Hi Aurel300,
When you compile the haxe code using hxcpp, the output files are put in a sub directory. If you have followed the instructions, there should be a new “cpp” directory inside your project dir. The lib files should be in there. If you passed different commands the the haxe compiler, the sub directory may be of a different name.
Oh… Hehe, I thought it will be visible somewhere in XCode, just now I realized it is in the actual directory.
Thanks for the fast reply…
Unfortunately…
It still kidna fails. On step 14 – it should have run? It does not. If I kept the “Compile Haxe” script in the project, it gave me 27 errors, and when I removed it – 7.
One of them is “hxObject.h was not found”, for example. Other include “printf was not defined” and random stuff with excepting “;” or “(“, and so on.
Hi,
If you get reference to “hxObject.h” it means you are using an old version of haxe (or maybe hxcpp).
Try deleting the “cpp” directory before you start, and make sure haxe is version 2.05, and hxcpp is version 2.05.1.
Hugh
Not working at all.
T_T
I have the latest versions. Even if I knew I’ve downloaded them just two days ago, ./haxelib list clearly states the fact (lol).
Tried deleting cpp directory and “build and run”, but it does the same…
I have no idea what is the reason for this… It looks like it doesn’t know that main.cpp is a c++ file or something…
Hi Aurel300,
Oh yes, silly me, I forgot I even had the main.cpp in the nme directory! (You would not believe the number of files I had to update for the latest release). I have not updated this file for haxe 2.05. The change is simple, you need to change:
#include <hxObject.h>
to
#include <hxcpp.h>
Hope this works!
Hey,
thanks again – it did work! …almost -.- I get some different error now, though:
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/g++-4.2 failed with exit code 1
… wow so many errors here.
Hi,
Do you think you could post 1 of the errors (no need to post them all), or use something like http://pastebin.com. This would help – probably something simple that I have forgotten again.
Hugh
You know, I actually posted it. That part:
Command /Developer/Platforms/iPhoneSimulator.platform/Developer/usr/bin/g++-4.2 failed with exit code 1
is the error. When I said “so many errors here” I was referring to the overall project, not this build.
Hello.
I have few error about include (string.h etc…) when trying to compile my .hx file :
I’m using haxe -main HaxeTest -cpp cpp -lib neash -lib nme –remap neko:cpp –remap flash:neash -D iphonesim
And it’s saying things like /usr/lib/haxe/lib/hxcpp/2,05,1//include/hxcpp.h:6:20: error: string.h: No such file or directory
I have string.h in my /usr/include… What should I do ?
When I let XCode compile it for me, I have no .a in the cpp/ dir.
Umm… So? Did you find anything?
Hi Aurel300,
I have been away from iPhone development for a little while now, but I will be picking it up again soon.
I was hoping there might be a little more of an error message that will help me pinpoint the problem. In the build results panel, you can expand out the middle bit so you get 3 sections. In the middle panel, there should be some more messages – it would be helpful if you could post these.
Hugh