Discussion:
hxcpp: Include haXe code into a Mac app
Renaud Pradenc
2011-09-19 16:41:56 UTC
Permalink
Hi everyone,


I'm trying to convert a haXe application that already runs under Flash and Windows to the Mac. Therefore, I plan to convert haXe code to C++, which I'll integrate in my Objective-C/Cocoa application. I need to keep an ObjC base since I need to use Cocoa's objects to provide platform-specific features.

Now, I'm facing difficulties generating the C++ code and using it in my application.



Approach 1: Generate an executable
==================================

I tried the example given on the "Getting graphically started with haXe/C++" page.
At first, this did not compile under Mac OS 10.7 because Apple now only includes SDKs 10.6 and 10.7 and hxcpp looks for SDK 10.5. Passing -D MAC_USE_CURRENT_SDK switches the Base SDK to 10.7, which is fine, but also switches the Deployment target to 10.7, which prevents it from running under 10.6. I modified hxcpp 2,08,0's mac-toolchain.xml to use 10.6 for both the Base SDK and Deployment Target.

This generates an executable, which I don't need. Anyway I tried to include the generated C++ code into my Xcode project, but the compiler complained some symbols, like "Class", were already defined.

It did not go further because this approach demands that the C++ files are included to the Xcode projet.



Approach 2: Generate a static library
=====================================

This is the approach adopted on the iPhone platform.


My sample haXe class code:

class Test {
static function main() {
trace("Hello World !");
}

static function printMessage() {
trace("See my message !");
}
}

haxe is passed the arguments:

-cpp cpp
-debug
-main Test
-D HXCPP_M64
-D static


Now the problems:
1) The build process stops with the message:
ar -cr Test-debug.a @all_objs
ar: @all_objs: No such file or directory

If I type "ar -cr Test-debug.a all_objs" (notice the absence of @), the build seems to complete, and I get a Test-debug.a file which is a text file with contains the names of the paths to the .o files.

2) Now, I'm unable to use the library within the Xcode project. I get this error message:
ld: warning: ignoring file Test-debug.a, file was built for archive which is not the architecture being linked (x86_64)


Question:
========
Does anyone know how I could turn the object files to a valid static library ?
Did anyone managed to create a static lib for Mac OS X ?


Thanks in advance !

Renaud Pradenc
Ceroce.com
--
haXe - an open source web programming language
http://haxe.org
Joshua Granick
2011-09-19 17:43:57 UTC
Permalink
Have you tried NME yet?

If you're setup with the install tool, you should be able to compile for
Flash, Windows and Mac OS, like this:



haxelib run nme test MyProject.nmml flash
haxelib run nme test MyProject.nmml cpp (while running Windows)
haxelib run nme test MyProject.nmml cpp (while running OS X)
Post by Renaud Pradenc
Hi everyone,
I'm trying to convert a haXe application that already runs under Flash
and Windows to the Mac. Therefore, I plan to convert haXe code to C++,
which I'll integrate in my Objective-C/Cocoa application. I need to keep
an ObjC base since I need to use Cocoa's objects to provide
platform-specific features.
Now, I'm facing difficulties generating the C++ code and using it in my application.
Approach 1: Generate an executable
==================================
I tried the example given on the "Getting graphically started with haXe/C++" page.
At first, this did not compile under Mac OS 10.7 because Apple now only
includes SDKs 10.6 and 10.7 and hxcpp looks for SDK 10.5. Passing -D
MAC_USE_CURRENT_SDK switches the Base SDK to 10.7, which is fine, but
also switches the Deployment target to 10.7, which prevents it from
running under 10.6. I modified hxcpp 2,08,0's mac-toolchain.xml to use
10.6 for both the Base SDK and Deployment Target.
This generates an executable, which I don't need. Anyway I tried to
include the generated C++ code into my Xcode project, but the compiler
complained some symbols, like "Class", were already defined.
It did not go further because this approach demands that the C++ files
are included to the Xcode projet.
Approach 2: Generate a static library
=====================================
This is the approach adopted on the iPhone platform.
class Test {
static function main() {
trace("Hello World !");
}
static function printMessage() {
trace("See my message !");
}
}
-cpp cpp
-debug
-main Test
-D HXCPP_M64
-D static
build seems to complete, and I get a Test-debug.a file which is a text
file with contains the names of the paths to the .o files.
ld: warning: ignoring file Test-debug.a, file was built for archive
which is not the architecture being linked (x86_64)
========
Does anyone know how I could turn the object files to a valid static library ?
Did anyone managed to create a static lib for Mac OS X ?
Thanks in advance !
Renaud Pradenc
Ceroce.com
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
Renaud Pradenc
2011-09-20 07:58:22 UTC
Permalink
Hi Joshua,

Thank you for answering.

No, I did not look into NME because it is a wrapper for the SDL and I don't need it. My Mac application will provide the C++ API for drawing.
What I'm actually trying to do is to call the C++ API from the haXe code. I obviously need to convert the haXe code to C++ which is the difficult part.


Thank you,

Renaud Pradenc
www.ceroce.com
Post by Joshua Granick
Have you tried NME yet?
haxelib run nme test MyProject.nmml flash
haxelib run nme test MyProject.nmml cpp (while running Windows)
haxelib run nme test MyProject.nmml cpp (while running OS X)
Post by Renaud Pradenc
Hi everyone,
I'm trying to convert a haXe application that already runs under Flash and Windows to the Mac. Therefore, I plan to convert haXe code to C++, which I'll integrate in my Objective-C/Cocoa application. I need to keep an ObjC base since I need to use Cocoa's objects to provide platform-specific features.
Now, I'm facing difficulties generating the C++ code and using it in my application.
Approach 1: Generate an executable
==================================
I tried the example given on the "Getting graphically started with haXe/C++" page.
At first, this did not compile under Mac OS 10.7 because Apple now only includes SDKs 10.6 and 10.7 and hxcpp looks for SDK 10.5. Passing -D MAC_USE_CURRENT_SDK switches the Base SDK to 10.7, which is fine, but also switches the Deployment target to 10.7, which prevents it from running under 10.6. I modified hxcpp 2,08,0's mac-toolchain.xml to use 10.6 for both the Base SDK and Deployment Target.
This generates an executable, which I don't need. Anyway I tried to include the generated C++ code into my Xcode project, but the compiler complained some symbols, like "Class", were already defined.
It did not go further because this approach demands that the C++ files are included to the Xcode projet.
Approach 2: Generate a static library
=====================================
This is the approach adopted on the iPhone platform.
class Test {
static function main() {
trace("Hello World !");
}
static function printMessage() {
trace("See my message !");
}
}
-cpp cpp
-debug
-main Test
-D HXCPP_M64
-D static
ld: warning: ignoring file Test-debug.a, file was built for archive which is not the architecture being linked (x86_64)
========
Does anyone know how I could turn the object files to a valid static library ?
Did anyone managed to create a static lib for Mac OS X ?
Thanks in advance !
Renaud Pradenc
Ceroce.com
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
Gamehaxe
2011-09-21 10:51:38 UTC
Permalink
Hi,
I think what has happened here is that you have put the ascii file
into your library, rather than the files referred to by the ascii file.

The @files is controlled by the "fromfile" entry in the ar
section of the $HXCPP/build-tool/gcc-toolchain.xml file.
You coud set GCC_OLD in your config file, or add an unconditional
<fromfile value="" />
entry.

Hugh
Post by Renaud Pradenc
build seems to complete, and I get a Test-debug.a file which is a text
file with contains the names of the paths to the .o files.
ld: warning: ignoring file Test-debug.a, file was built for archive
which is not the architecture being linked (x86_64)
--
haXe - an open source web programming language
http://haxe.org
Renaud Pradenc
2011-09-21 12:28:24 UTC
Permalink
Hi Hugh,


I would not say I understand the whole build process but as far as I can tell:

- the paths to object files are stored in all_objs

- this step breaks the build process:
ar -cr Test-debug.a @all_objs
ar complains: "ar: @all_objs: No such file or directory"

- to continue the build process, I type:
ar -cr Test-debug.a all_objs

This time, I get a lib file Test-debug.a, but this file is in the "archive format", which looks like this:
!<arch>
all_objs 1316607593 504 20 100644 841 `
obj/darwin64-dbg//src/Test.o
obj/darwin64-dbg//src/haxe/Log.o
obj/darwin64-dbg//src/Std.o
obj/darwin64-dbg//src/__boot__.o
obj/darwin64-dbg//src/__resources__.o
obj/darwin64-dbg//src/__lib__.o
obj/darwin64-dbg//src/hx/Anon.o
obj/darwin64-dbg//src/hx/Boot.o
etc.

- the original script uses:
ranlib Test-debug.a

This step is needed to convert the lib from the archive format to the mach-o format.
Unfortunately, I get this message:
ranlib: warning for library: Test-debug.a the table of contents is empty (no object file members in the library define global symbols)
(Test-debug.a is only 1 KB).

- I tried Apple's own tool, libtool, with success. In the end, I get the library. Now, I'm struggling to include it in my own C++ code, but that's another story.


Do you mean the @files notation only works with older versions of GCC ?
Anyway, there's still this issue with ranlib. I'm afraid I can't modify the build script myself because I don't understand it :-(


Thank you Hugh for all your work !

Renaud Pradenc
www.ceroce.com
Post by Gamehaxe
Hi,
I think what has happened here is that you have put the ascii file
into your library, rather than the files referred to by the ascii file.
section of the $HXCPP/build-tool/gcc-toolchain.xml file.
You coud set GCC_OLD in your config file, or add an unconditional
<fromfile value="" />
entry.
Hugh
Post by Renaud Pradenc
ld: warning: ignoring file Test-debug.a, file was built for archive which is not the architecture being linked (x86_64)
--
haXe - an open source web programming language
http://haxe.org
Gamehaxe
2011-09-21 14:24:00 UTC
Permalink
Hi,
Post by Renaud Pradenc
ar -cr Test-debug.a all_objs
because this says "put the file 'all_objs' into the library"

You could try:

ar -cr Test-debug.a `cat all_objs`

which lists out all the files and puts *them* into the library.

The GCC_OLD was because old versions of gcc did not allow the @files
syntax.
But it appears that the version you have also does not like it.

By setting the "fromfile" syntax to "", you disable this feature and
make the build tool put the full list on the command line.

Hugh
--
haXe - an open source web programming language
http://haxe.org
Renaud Pradenc
2011-09-21 15:10:13 UTC
Permalink
Hi,


I tried
ar -cr Test-debug.a `cat all_objs`
with success.

I tried passing GCC_OLD and it also works.
The version of gcc is gcc-llvm 4.2.1.



Thank you !

Renaud Pradenc
www.ceroce.com
Post by Gamehaxe
Hi,
Post by Renaud Pradenc
ar -cr Test-debug.a all_objs
because this says "put the file 'all_objs' into the library"
ar -cr Test-debug.a `cat all_objs`
which lists out all the files and puts *them* into the library.
But it appears that the version you have also does not like it.
By setting the "fromfile" syntax to "", you disable this feature and
make the build tool put the full list on the command line.
Hugh
--
haXe - an open source web programming language
http://haxe.org
Renaud Pradenc
2011-09-22 14:13:52 UTC
Permalink
Hi everyone,


I have built a static library thanks to hxcpp and now, I am trying to use this lib inside a Xcode project. However I get these errors when linking the executable:

Undefined symbols for architecture x86_64:
"_std_register_prims", referenced from:
_hxRunLibrary in Test-debug.a(RunLibs.o)
"_regexp_register_prims", referenced from:
_hxRunLibrary in Test-debug.a(RunLibs.o)
"_zlib_register_prims", referenced from:
_hxRunLibrary in Test-debug.a(RunLibs.o)
ld: symbol(s) not found for architecture x86_64

(Test-debug.a is the name of my static library).
These 3 symbols are defined respectively in std.dylib, regexp.dylib and zlib.dylib.

I found another message on this list of someone already asking the exact same question.
However, this person did not answer to Hugh asking "And with the -v option" ?

So here is it is: (and you may see that the 3 dylibs ARE referenced).

"/Developer/usr/bin/ld" -demangle -dynamic -arch x86_64 -macosx_version_min 10.6.0 -syslibroot /Developer/SDKs/MacOSX10.7.sdk -o /Users/renaud/Library/Developer/Xcode/DerivedData/simple-glgzywenqzsobcgubbifymbgyzmn/Build/Products/Debug/simple -lcrt1.10.6.o -L/Users/renaud/Library/Developer/Xcode/DerivedData/simple-glgzywenqzsobcgubbifymbgyzmn/Build/Products/Debug -L/usr/lib/haxe/lib/hxcpp/2,08,0/bin/Mac64 -L/Users/renaud/Desktop/compilSimple/simple/../cpp -L/usr/lib/haxe/lib/hxcpp/dev/bin/Mac64 -L/Users/renaud/Desktop/compilSimple/simple/simple -filelist /Users/renaud/Library/Developer/Xcode/DerivedData/simple-glgzywenqzsobcgubbifymbgyzmn/Build/Intermediates/simple.build/Debug/simple.build/Objects-normal/x86_64/simple.LinkFileList /Users/renaud/Desktop/compilSimple/simple/simple/Test-debug.a /Users/renaud/Desktop/compilSimple/simple/../../../../../../usr/lib/haxe/lib/hxcpp/2,08,0/bin/Mac64/regexp.dylib /Users/renaud/Desktop/compilSimple/simple/../../../../../../usr/lib/haxe/lib/hxcpp/2,08,0/bin/Mac64/std.dylib /Users/renaud/Desktop/compilSimple/simple/../../../../../../usr/lib/haxe/lib/hxcpp/2,08,0/bin/Mac64/zlib.dylib -lstdc++ -lSystem -F/Users/renaud/Library/Developer/Xcode/DerivedData/simple-glgzywenqzsobcgubbifymbgyzmn/Build/Products/Debug



Has anyone any idea why the 3 symbols can not be found in their libs ?


Thanks !


Renaud Pradenc
www.ceroce.com
--
haXe - an open source web programming language
http://haxe.org
Gamehaxe
2011-09-22 16:00:01 UTC
Permalink
Hi,
For the static build, you must link against the static versions of
std/regexp/zlib.a
These do not come with the system, so you must build them yourself.

See:
http://code.google.com/p/hxcpp/source/browse/trunk/src/ExampleMain.cpp

for more details.

Hugh
Post by Renaud Pradenc
Hi everyone,
I have built a static library thanks to hxcpp and now, I am trying to
use this lib inside a Xcode project. However I get these errors when
_hxRunLibrary in Test-debug.a(RunLibs.o)
_hxRunLibrary in Test-debug.a(RunLibs.o)
_hxRunLibrary in Test-debug.a(RunLibs.o)
ld: symbol(s) not found for architecture x86_64
(Test-debug.a is the name of my static library).
These 3 symbols are defined respectively in std.dylib, regexp.dylib and zlib.dylib.
I found another message on this list of someone already asking the exact same question.
However, this person did not answer to Hugh asking "And with the -v option" ?
So here is it is: (and you may see that the 3 dylibs ARE referenced).
"/Developer/usr/bin/ld" -demangle -dynamic -arch x86_64
-macosx_version_min 10.6.0 -syslibroot /Developer/SDKs/MacOSX10.7.sdk -o
/Users/renaud/Library/Developer/Xcode/DerivedData/simple-glgzywenqzsobcgubbifymbgyzmn/Build/Products/Debug/simple
-lcrt1.10.6.o
-L/Users/renaud/Library/Developer/Xcode/DerivedData/simple-glgzywenqzsobcgubbifymbgyzmn/Build/Products/Debug
-L/usr/lib/haxe/lib/hxcpp/2,08,0/bin/Mac64
-L/Users/renaud/Desktop/compilSimple/simple/../cpp
-L/usr/lib/haxe/lib/hxcpp/dev/bin/Mac64
-L/Users/renaud/Desktop/compilSimple/simple/simple -filelist
/Users/renaud/Library/Developer/Xcode/DerivedData/simple-glgzywenqzsobcgubbifymbgyzmn/Build/Intermediates/simple.build/Debug/simple.build/Objects-normal/x86_64/simple.LinkFileList
/Users/renaud/Desktop/compilSimple/simple/simple/Test-debug.a
/Users/renaud/Desktop/compilSimple/simple/../../../../../../usr/lib/haxe/lib/hxcpp/2,08,0/bin/Mac64/regexp.dylib
/Users/renaud/Desktop/compilSimple/simple/../../../../../../usr/lib/haxe/lib/hxcpp/2,08,0/bin/Mac64/std.dylib
/Users/renaud/Desktop/compilSimple/simple/../../../../../../usr/lib/haxe/lib/hxcpp/2,08,0/bin/Mac64/zlib.dylib
-lstdc++ -lSystem
-F/Users/renaud/Library/Developer/Xcode/DerivedData/simple-glgzywenqzsobcgubbifymbgyzmn/Build/Products/Debug
Has anyone any idea why the 3 symbols can not be found in their libs ?
Thanks !
Renaud Pradenc
www.ceroce.com
--
haXe - an open source web programming language
http://haxe.org
--
haXe - an open source web programming language
http://haxe.org
Loading...