Use hxcpp to build a library used in a c++ project

575 views
Skip to first unread message

Nathan Hüsken

unread,
Jul 24, 2015, 4:24:07 AM7/24/15
to haxe...@googlegroups.com
Hey,

As the title suggests, I want to use hxcpp to create a library form my
haxe library for C++.

I found this post: http://old.haxe.org/forum/thread/3395#nabble-td6179190
And this documentation: http://old.haxe.org/doc/cpp/ffi?lang=en

The documentation suggest, that I should compile my main into a ndll and
then compile everything together using hxcpp.

I want to use this in a bigger project, and therefor I would prefer if
the haxe code would produce a static linkable C++ library.
Is that possible?
How would I generate the "BuildFile.xml"?

Thanks!
Nathan

Hugh

unread,
Jul 27, 2015, 1:35:23 AM7/27/15
to Haxe, nathan....@posteo.de
There are some more notes here:

This will build the .lib file.  I assume then that you have a larget project you are going to link with?  If this project already exists, then you can use whatever makesystem this is using, rather than writing a build.xml.

Hugh

basteln

unread,
Jul 30, 2015, 5:35:54 PM7/30/15
to Haxe, nathan....@posteo.de

Alexander Kuzmenko

unread,
Aug 20, 2015, 7:02:45 AM8/20/15
to Haxe, nathan....@posteo.de
Hello.
In company where i am working now i need to prove the ability of using Haxe code from existing C++ project.

I've managed to build 'libMain-debug.a', but now i'm trying to  compile this file: https://github.com/HaxeFoundation/hxcpp/blob/master/src/ExampleMain.cpp
with following line:
g++ -o main.bin -pthread src/ExampleMain.cpp mylib/libMain-debug.a $HXCPP_LIB_LINUX64/libstd.a $HXCPP_LIB_LINUX64/libregexp.a $HXCPP_LIB_LINUX64/libzlib.a

Which fails with a lot of errors like
lib/libstd.a(febcda1e_Init.o): In function `std_main':
Init.cpp:(.text+0xc): undefined reference to `val_id'
<...>
lib/libstd.a(febcda1e_Misc.o): In function `double_of_bytes(_value*, _value*)':
Misc.cpp:(.text+0x20): undefined reference to `val_type'
Misc.cpp:(.text+0x3d): undefined reference to `hx_fail'
Misc.cpp:(.text+0x45): undefined reference to `val_to_buffer'
Misc.cpp:(.text+0x56): undefined reference to `buffer_data'
Misc.cpp:(.text+0x79): undefined reference to `val_bool'
<...>
and so on with various `undefined reference to ...`
Am i missing something?

I have zero knowledge in C++ world if that matters :)

понедельник, 27 июля 2015 г., 8:35:23 UTC+3 пользователь Hugh написал:

Nathan Hüsken

unread,
Aug 20, 2015, 7:06:44 AM8/20/15
to haxe...@googlegroups.com
Try

g++ -o main.bin -pthread src/ExampleMain.cpp mylib/libMain-debug.a $HXCPP_LIB_LINUX64/libstd.a $HXCPP_LIB_LINUX64/libregexp.a $HXCPP_LIB_LINUX64/libzlib.a mylib/libMain-debug.a

(two occurences of libMain-debug.a). Also try setting the -pthread at the end.

I also discovered that you have to add "-DHXCPP_VISIT_ALLOCS" or you will get segmentation faults.

Best Regards,
Nathan
--
To post to this group haxe...@googlegroups.com
http://groups.google.com/group/haxelang?hl=en
---
You received this message because you are subscribed to the Google Groups "Haxe" group.
For more options, visit https://groups.google.com/d/optout.

Alexander Kuzmenko

unread,
Aug 20, 2015, 7:48:56 AM8/20/15
to Haxe
Thanks! That helped!

четверг, 20 августа 2015 г., 14:06:44 UTC+3 пользователь Nathan Hüsken написал:

Alexander Kuzmenko

unread,
Aug 20, 2015, 8:30:01 AM8/20/15
to Haxe
Now i'm stack with unexpected behavior of my lib )

In Haxe i have class nexters.Battle with two methods with identical behavior:
public function addTowers (amount:Int, hp:Float, damage:Float)
   
{
        trace
('adding towers: $amount');
   
}


   
public function addSoldiers (amount:Int, hp:Float, damage:Float)
   
{
        trace
('adding soldiers: $amount');
   
}

I'm using them from C++ side like this:
battle->addTowers(1, 10050001, 86301);
battle
->addSoldiers(3, 425001, 95001);

Now the result in console output is
Battle.hx:20: adding towers: 3

While expected output is
Battle.hx:20: adding towers: 1
Battle.hx:36: adding soldiers: 3

And I have no idea what's going on :)
If i compile standalone hxcpp app, then those methods work as expected.

четверг, 20 августа 2015 г., 14:48:56 UTC+3 пользователь Alexander Kuzmenko написал:

Alexander Kuzmenko

unread,
Aug 20, 2015, 8:36:02 AM8/20/15
to Haxe
I mean "I'm stuck", not "stack" :)

четверг, 20 августа 2015 г., 15:30:01 UTC+3 пользователь Alexander Kuzmenko написал:

Alexander Kuzmenko

unread,
Aug 20, 2015, 8:40:20 AM8/20/15
to Haxe
Since generated cpp sources for that class are identical in both standalone hxcpp app and static lib, i guess i missed something in initialization of my lib from the C++ side.

четверг, 20 августа 2015 г., 15:30:01 UTC+3 пользователь Alexander Kuzmenko написал:

Nathan Hüsken

unread,
Aug 20, 2015, 8:46:02 AM8/20/15
to haxe...@googlegroups.com
Or maybe some compiler flag? Maybe try adding all the compiler flags you see when compiling the standalone app? Just an Idea, i hace no clue :-)

Alexander Kuzmenko

unread,
Aug 20, 2015, 8:57:57 AM8/20/15
to Haxe
No success )

четверг, 20 августа 2015 г., 15:46:02 UTC+3 пользователь Nathan Hüsken написал:

Alexander Kuzmenko

unread,
Aug 20, 2015, 10:55:16 AM8/20/15
to Haxe
It looks like `addSoldiers` method is pointing at the body of `addTowers`, while `addTowers` does not have body at all.

пятница, 24 июля 2015 г., 11:24:07 UTC+3 пользователь Nathan Hüsken написал:

Hugh

unread,
Aug 21, 2015, 12:37:16 AM8/21/15
to Haxe
This looks like a classic case of compiling with different flags.
The two that spring to mind are HXCPP_VISIT_ALLOCS and HXCPP_API_LEVEL. There could also be some debug flags.
When you compile the other cpp files, you can see the "g++" command line, and the defines that are on that - you will need these if you want to interact with generated header files directly.

The alternative is to write some custom api code, and get this compiled at the same time as the hxcpp code.  This api would contain only types that do not refer to the haxe headers directly, and add a "glue layer".  External code would then interact only with this API, and therefore be isolated from the hxcpp compile flags.  This might be quite some work, but there is an interesting option of using macros to generate this API, which I think would be pretty nice.

Lastly, you could write a build.xml file for your custom application and get hxcpp to compile up your whole project with the same flags at the same time.  This may avoid the need for additional makefiles etc.

Hugh

Alexander Kuzmenko

unread,
Aug 21, 2015, 4:44:58 AM8/21/15
to Haxe
Thanks Hugh and Nathan!

I copy-pasted -DHXCPP_* flags which were used by hxcppp to build my lib. Now my lib behaves in external C++ app as expected!

пятница, 21 августа 2015 г., 7:37:16 UTC+3 пользователь Hugh написал:
Reply all
Reply to author
Forward
0 new messages