Linking *.a static libraries with hxcpp

What I got is *.a library and *.h header. I want to use them in my application. Haw can I link them with my project.
C code in my app is already done but I have no idea how to attach .a lib.

#include <iostream>
#include <dlfcn.h>
#include <../../lib_hellgo.h>

using namespace std;

    cout << "CALLLLLLBACK" << endl;

void _init(){
    void (*fptr)() = THIS_IS_CALLLLLLBACK;

solved :slight_smile:

<target id="haxe">
    <flag value="-L.."/>
    <lib name="-l_hellgo" />

So you attach that in the cpp source code?

You can add @:buildXml to a class and will add those flags then compiling the source code with hxcpp

Docs hxcpp/

If you look in the cpp output folder there’s a file called Build.xml, that will contain your added flags. Hxcpp compiles source code by reading that file. It’s like a makefile and It’s a surprisingly decent cross-platform C++ build tool in its own right for something buried away in a haxe library!

So, is that file both created read in the compile?

Exactly, it’s a two step process:

  • haxe.exe generates the C++ code and that Build.xml ‘makefile’
  • hxcpp’s build tool is then called, reads that Build.xml file and runs native compiler commands

If you add -D HXCPP_VERBOSE, hxcpp will print all native commands as they’re executed. If you CD into the cpp output directory you can copy and paste those commands into a terminal and reproduce the build exactly (useful for debugging sometimes)

Another one that might be useful if you plan to read the C++ source, you can add -D no-debug to disable the debug macros and get cleaner output (more defines here: hxcpp/ at master · HaxeFoundation/hxcpp · GitHub)

I created this Haxe project:

package src;

class Main {
    public static function main() {

It creates some cpp files in the bin/cpp, with haxe build.hxml

Can I move the main.h and main.cpp created and place it in my src folders containing my haxe code…
Then place @:buildXml(something) attribute on the moved main.cpp in src. Now I can change these files and they will compile with cpp target?

I found this example which I could use: GitHub - Gaikov/HaxeWithCppIntegrationExample: C++ with Haxe integration example

That example look good, generally we don’t ever edit the generated C++ directly because if you need to inject C++ code there’s metadata you can use:

metadata description platforms
@:include include a local header file cpp
@:cppFileCode Code to be injected into generated cpp file. cpp
@:cppInclude File to be included in generated cpp file. cpp
@:cppNamespaceCode cpp
@:functionCode Used to inject platform-native code into a function. cpp, java, cs
@:functionTailCode cpp
@:headerClassCode Code to be injected into the generated class, in the header. cpp
@:headerCode Code to be injected into the generated header file. cpp
@:headerInclude File to be included in generated header file. cpp
@:headerNamespaceCode cpp

Or if you want to just write C++ mixed in with your haxe you can use the __cpp__ function

var haxeInt: Int = 5;
untyped __cpp__("int i = {0};", haxeInt); // emits int i = haxeInt; in C++

More details: C++ Magic by Hugh Sanderson |

1 Like