StackContext error in C++ static library

I have a program which I’m trying to compile in to a static library. During development, I had a small main function that simply parsed the command line arguments and called an appropriate function - that worked fine. I’m now trying to build the static library, and call in to it from a c++ file rather than a Haxe generated one. The function is essentially the same though - I’m even calling Sys.args() from the c++ code just to try to make it as similar as possible to what Haxe generated.

The problem that I’m having is that when using the static library, as soon as Sys.args() is called I’m getting an exception thrown in StackContext::pushFrame: this was nullptr. The call stack looks like this:

|>|CPPMTK.exe!hx::StackContext::pushFrame(hx::StackFrame * inFrame) Line 479|C++|
| |CPPMTK.exe!hx::StackFrame::StackFrame(const hx::StackPosition * inPosition) Line 653|C++|
| |CPPMTK.exe!Sys_obj::args() Line 42|C++|
| |CPPMTK.exe!main() Line 13|C++|

My program right now is pretty simple:

#include "stdafx.h"
#include "Sys.h"
int main()
    return 0;

The only thing that seems relevant in my .hxml for the library is that I have the flags NO_PRECOMPILED_HEADERS and static_link set.

It seems like my problem is the same as this one, but unfortunately there was no solution found. I am using threads within the static library, but at the point that the exception is thrown, nothing has been done with the threads. I don’t use C++ that often so I’m not that great at it, but since the executable is working, I would think that there’s just something that I’m missing when building either the static library or my test application. Any ideas what that could be?

You might be missing some initialization code?

A normal hxcpp app’s main include:


void __hxcpp_main()
    // haxe's main is called here

void __hxcpp_lib_main()

You might need to call some of those functions? (never used hxcpp to make a static lib, so I’m not sure)

Not having some initialization code was the cause - I think the critical thing that I was missing was to include HxcppMain.h and let it handle the main() entry point (that function eventually calls __hxcpp_main()). Here’s the program that I’ve got now which is working for me:

#include "stdafx.h"
#include <Sys.h>
void __hxcpp_main();
#include <hx/HxcppMain.h>

void __hxcpp_main()
	::Array< ::String > args = ::Sys_obj::args();

Thanks for the help!